Computer Science Canada

Missile Shooting

Author:  evildaddy911 [ Tue Dec 06, 2011 4:58 pm ]
Post subject:  Missile Shooting

A while ago I was making a top-down game, and I kept thinking, "Is there a way that I can use x and y velocities and still keep a constant speed no matter what angle I am shooting at?"

So I did some tinkering and came up with this:

Turing:
xv := (x2 - x1) / (Math.Distance (x1, y1, x2, y2) / speed)
yv := (y2 - y1) / (Math.Distance (x1, y1, x2, y2) / speed)


(x1 and y1 are the coordinates of where the bullet is being shot from, and x2 and y2 are the coordinates to where the bullet is headed)

Then you simply add the current x/y coordinates to their respective velocities to determine where the bullet will be next frame.

PLEASE NOTE:
In order to ensure accurate results, use real values for the current x, y, xv and yv values and use the round() function

Here's a simple example of how this can be used:

Author:  Raknarg [ Tue Dec 06, 2011 7:19 pm ]
Post subject:  RE:Missile Shooting

That does work. Another way I found to be useful was to use trigonometry instead.


: