views:

365

answers:

2

This is for a game in a flash AS3 only project.

the player controls a character with a gun. By clicking on the screen the gun fires a missile in an arc to the point clicked.

Whats the best way to calculate the x and y co-ordinates of the missile for each frame?

+3  A: 

Well, an arc is a piece of a circle. The general formulas for a circle are

x = r * cos(a) + cx
y = r * sin(a) + cy

Where r is the radius of the circle, a is the angle along the circle (in radians), and cx and cy are the coordinates of the center of the circle.

So each frame you would increment the angle (a) and recalculate the position with those formulas.

The trick will be determining the appropriate radius and center points. You could probably figure out an algorithm that would find a center point based on a fixed radius.

Edit: To get the same velocity at different radii.

To get velocity (pixels/sec) from angular velocity (rad/sec)

v = Δa * r

So if we pick some v, then Δa = v / r where v is some constant and r is the radius of the circle.

Mike Cooper
How do you get a constant speed with different distances. As I increase the angle at a constant rate the missile speeds up the further away it is?
Nathan Smith
...it _shouldn't_ do that... Are you sure that your angle is increasing constantly? and that your angle is the only thing changing in the code?
Mike Cooper
If the circle diameter (distance) is bigger doesn't the missile have to travel further around the circumference for each angle increment?
Nathan Smith
Oh, I thought you meant a different angular speed at different points around the circle. I will add that info to my answer.
Mike Cooper
+1  A: 

I think the missiles should fly in projectile rather than an arc path...

If you really care having a realistic motion, study some physic equations.

If you just want to have some simple and cartoon-like motion, using TweenMax's bezier/bezierThrough will be extremely easy. See the examples in its plug-in explorer.


Update: For simple path tweening, Grape Animation Library seems great too!

Andy Li