Zakukashi
Zakukashi

Reputation: 586

2d projectile trajectory?

My object has a point x,y this always changes as the mouse drags it around inside a 800x600 box. My object also rotates to an angle set by the mouse it can be any 360 degree. My issue is now how to shoot a projectile from that angle and of course from that XY position. The projectile only needs to go in a straight line but It need to be moving in the correct angle as the Main object was. I have the angle and I have the XY position all I need is the formula to make it move across the grid from the appropriate angle.

EDIT - Current JSFIDDLE with projectile not shooting at the correct angle. http://jsfiddle.net/vbk4Z/16/

Upvotes: 0

Views: 3574

Answers (1)

Alnitak
Alnitak

Reputation: 339816

The standard formulae are:

xn = x0 + v * t * cos(theta)
yn = y0 + v * t * sin(theta)

Where (x0, y0) are the original position, v is the velocity required, theta is the angle (in radians) and t is the time since the projectile was fired.

Note that this uses the classic cartisian convention of angles being measured anticlockwise relative to the positive x axis, and with the y axis facing up.

If you're using browser coordinates, and happen to have chosen to use bearings (clockwise relative to "up" / "north") then you need to swap some signs and trig functions:

xn = x0 + v * t * sin(theta)
yn = y0 - v * t * cos(theta)

Upvotes: 2

Related Questions