Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

At the moment I'm improvising a slingshot, the user can click and drag the projectile and let go. The force on the object is calculated by getting the distance between the vector of the slingshots two forks and the vector between where the user pulled it. However this will always result in a positive number and will not take into account the angle of the object relative to that of the slingshot. How can I make it fly out of the slingshot correctly?

My attempt:

float distance = mousePos.dst(pouchPos.x, pouchPos.y, 0);
Vector2 d = new Vector2(pouchPos.x - mousePos.x, pouchPos.y - mousePos.y); 
object.setVelocity(d.nor().x * FIRE_SPEED, d.nor().y * FIRE_SPEED);
share|improve this question
2  
Learn and love the equations of motion. –  Byte56 Mar 20 '12 at 17:36
add comment

1 Answer

up vote 6 down vote accepted

Don't use the distance. Instead, subtract the fork position vector from the pull position vector and use (some constant multiple of) that as the projectile's velocity. This will automatically scale appropriately for the pull distance.

(Actually, it should possibly be squared — I'm not sure what the exact physics of a projectile accelerated by a slingshot band would be.) Never mind: relating the energy, 1/2 kx² = 1/2 mv² ⇒ v = √(k/m)x, tells us that velocity is proportional to stretch distance (assuming the slingshot relaxes neatly and so doesn't waste energy, which is probably a good enough assumption).

Based on the code you posted, and assuming you have overloads for vectors in your language (I don't know what you're working in):

Vector2 d = (mousePos - pouchPos) * FIRE_SPEED;
object.setVelocity(d.x, d.y);

The code you posted in your answer will fire at the same speed no matter how far the slingshot is stretched, which is physically incorrect. Using vector operations also greatly simplifies the code.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.