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.

This question already has an answer here:

I have a 2D side-view game with object attributes such as x, y, speedX, etc. Now I want to throw a given object from any place on the screen onto a defined goal place, in such a way that its trajectory looks somewhat normal (with gravity, it making a curve and such). Imagine an enemy is destroyed and what he stole flies from him back towards the stolen object's origin. (I do have physics via Box2D in this game, but for this particular need, it seems I require a manual approach; for one thing, the object is allowed to pass other bridges and such for the time it flies back to its main bridge.)

How would I go about this? I've tried moving manually by adjusting towards a target speed and such, but the trajectory and timing looks very unnatural. Thanks!

enter image description here

share|improve this question

marked as duplicate by Byte56, Anko, Josh Petrie, Tetrad Apr 11 '13 at 16:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 1 down vote accepted

Wiki has article about projectile trajectory here.

Maybe also this will help you (it helped me). It's brief explanation of two approaches to your problem - variable strength and angle of projectile being shoot. At the end of this post, there's short "Try it yourself!" section, where small JavaScript implementation exists (which may, again, help you).

Good luck!

share|improve this answer
    
Thank you! The JavaScript at freewebs.com/batiazul3627/castlestory/ballistics.html seems to cover it all! –  Philipp Lenssen Apr 11 '13 at 12:33

One approach would be to use a parabolic trajectory, which is the shape that normal projectiles take.

We can use three points to define the parabola:

  • Initial object position
  • Final object position
  • Turning point

The turning point can be a constant horizontal and vertical distance away from the target, it just needs to flip slides to suit the initial position.

enter image description here

The parabola equation we are using is

enter image description here

Solve the following simultaneous equations to find a and b.

enter image description here

Now we might be interested in finding the launch angle and a velocity. First we manipulate the projectile motion equations to obtain the following

enter image description here

The above equation takes the parabolic form we outlined above. To find the launch angle theta we can simply use

enter image description here

If we know the animation time t we can find the launch velocity v_0 with

enter image description here

share|improve this answer
    
Thanks you very much! –  Philipp Lenssen Apr 11 '13 at 12:34
    
I waited because I wasn't sure which of the two replies to mark as answers. Yours is extensive and great but I should have clarified that I was looking more for code or pseudo code. –  Philipp Lenssen Apr 12 '13 at 7:35

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