0

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/

Zakukashi
  • 566
  • 1
  • 13
  • 29
  • 1
    I would start with Bresenham's algorithm: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm – Woodrow Douglass Apr 30 '12 at 16:28
  • @WoodrowDouglass I wouldn't - it doesn't help unless you know the final end point. – Alnitak Apr 30 '12 at 16:33
  • If the tragectory is a streight line, you could easily calculate an endpoint with a start point and an angle. x=x0 + d * cos(theta), y = y0 + d * sin(theta), where d is some arbitrary large distance if you want the line to go off of the edge of the buffer. – Woodrow Douglass Apr 30 '12 at 17:20
  • @WoodrowDouglass I tried your method just now but it moves at an incorrect location. For instance setting the theta to 45 Degrees should fire it NorthEast correct? but instead it goes to SouthEast – Zakukashi Apr 30 '12 at 17:40
  • Here is the current jsfiddle. http://jsfiddle.net/vbk4Z/1/ – Zakukashi Apr 30 '12 at 17:58

1 Answers1

2

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)
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • It seemed to work a bit but its a little off. For instance when I use 0 as the theta my object is pointing straight up north but when I fire the projectile using your formula it shoots towards 180(East) – Zakukashi Apr 30 '12 at 17:27
  • 1
    Standard math convention is that angle 0 is to the right. If you're using up as your angle 0, add 90 degrees, or in this case pi/2 radians to theta. ex: `xn = x0 + v * t * cos(theta + pi / 2)` – Thomas Apr 30 '12 at 18:10
  • @Thomas just tried it but still something seems wrong: http://jsfiddle.net/vbk4Z/4/ – Zakukashi Apr 30 '12 at 18:22
  • 3
    As Alnitak said in the answer, theta is the angle in radians. Your get direction is giving degrees, thus the problem. You either need to convert to radians for the `xn` step, or have your get direction method return radians. Also, due to the differences between 'math' and 'graphics' axis, you'll need to make that - v *t... instead of +. – Thomas Apr 30 '12 at 18:42
  • Yep that was the problem! Thank you Thomas and Alnitak – Zakukashi Apr 30 '12 at 19:00
  • @Thomas thanks for answering the questions I just _knew_ he'd come up with while I wasn't watching... ;-) – Alnitak Apr 30 '12 at 21:47
  • @Thomas actually, for up = 0' it's easier to simply change the `sin` and `cos` around to get the phase shift. Wow, this takes me back about 30 years to writing my first "clock" program on the BBC micro... – Alnitak Apr 30 '12 at 22:01