Sign in to follow this  
BenDrummin58

Projectile Math -- Need Help!

Recommended Posts

I've searched google...I've even searched these forums, and I cannot find solid math that can get this done. The thing is, I'm still a highschool student, and I have not yet learned about SIN and COS and TAN and all that fun stuff.. yet I am still in the process of making my own game :P Anyways, I just need to know how to get a projectile from point A to point B. I have speed, I have where it's coming from, I know where it's going to, I just don't know how to get it there. I know it has something to do with angles and something to do with using trig. Can anybody help out a noob in math?

Share this post


Link to post
Share on other sites
You can use trig for this, but you don't have to. The unit-length direction vector for the projectile is simply:
direction = normalize(B-A);
If you're not sure what normalize() does, just google 'vector normalization'. Once you have the direction vector, you can update the position of the projectile each frame like this:
position += direction * speed * time_step;
Where time_step is the amount of time that has elapsed since the last frame.

There are other ways to perform the update (such as using a fixed time step and/or decoupling physics and rendering), but the above should get you started.

Share this post


Link to post
Share on other sites
thanks for this new way of doing it! I have a question though...what is normalize() part of? Is it something I need to create myself, or is it hidden in some include file. I've actually never heard of it before O.O . Google doesn't seem to find a whole lot, other than a thing that computes the vectors..which doesn't really help..lol. So, sorry if I sound a bit noobish :D

Share this post


Link to post
Share on other sites
'normalize()' is a function that you must supply. It and the syntax (B-A) assume a basic vector math library, which you may not have yet. It's a must have for 2D or 3D game programming of any complexity, so it'd be a good thing to get started on. Meanwhile, here it is longhand:
// A and B are assumed not to be coincident or nearly coincident
float directionX = Bx - Ax;
float directionY = By - Ay;
float length = sqrt(directionX*directionX+directionY*directionY);
directionX /= length;
directionY /= length;
To normalize a vector simply means to make it unit length, which is accomplished by dividing it (that is, dividing each component) by its original length.

Share this post


Link to post
Share on other sites
thanks a lot! makes much more sense now. I'm already implementing it into my code and will reply back if I got it to work or not.

BTW..just for informational purposes, because this is really bugging me, how exactly WOULD you go about doing this in trig? The way I had worked in theory from what I've read on various articles, but it didn't work when I ran the game. It's going to be stuck in my head till I figure it out!

Share this post


Link to post
Share on other sites
it works! I had to look up a few things, but I got it! And, you don't have to answer that math question, but it would help settle some things in my head..lol. It bugs me when I can't figure out something and then I just leave it..

Share this post


Link to post
Share on other sites
by projectile do you mean like a cannon ball?

well anyways, for general projectile motion one will assume no air resistance (but that can be done if really desired). to solve this problem you will usually have initial velocity (speed and direction of movement), and acceleration due to gravity. that is all you need. velocity means that you have how fast the projectile is moving and what direction. in general physics texts they usually give you the speed and the angle from the ground, so i'll assume that is what you have.

assumptions:
- no air resistance, therefore the initial horizontal speed will be equal to the final horizontal speed
- the direction that acceleration due to gravity is in doesn't change
- projectile isn't loosing or gaining mass

so you have the initial speed and the angle (from the horizontal) that the projectile was fired at. to find the vertical component of the velocity (the part of the speed and direction that makes it go up) you would do:

vy = speed * sin(angle)

and for the horizontal component (the part of the speed and directon that makes the projectile move away from the initial location) you would do:

vx = speed * cos(angle)

now, that just tells you the components of the velocity. so to know how far up the projectile goes into the air, you can find out where the vertical component of the velocity becomes zero using:

vf2 = vi2 + 2 * a * d

this says "the final velocity squared is equal to the initial velocity squared plus two times acceleration times distance". we are going to be looking at the vertical component only, so it becomes:

vfy2 = viy2 + 2 * ay * dy

now, we know the initial speed (from above) and we actually know the final speed, because at the top of the projectile's path it will have a vertical speed of zero (otherwise it would never stop going up) and we know the vertical acceleration (it is just gravity). so then the equation becomes:

0 = viy2 + 2 * (-g) * d

and the only thing we don't know is "d". so solve for "d" to get:

d = (viy2) / (2 * g)

so now you know the distance the projectile traveled vertically. you can use the same formula to find how fast the projectile will be moving (vertically) when it gets back to the same vertical level as it initially was fired, and you will find that it will be the opposite of the initial velocity (just a little note).

so now you know how high the projectile went. now you may want to know how long it took to get there, so you can use the equation:

vf = vi + a * t

this says "the final velocity is equal to the initial velocity plus acceleration times the time". again, only using vertical elements, and so we can solve for time because we know "vf", "vi" and "a". we will assume that gravity is negative (as in down the y axis).

t = (vf - vi) / a

so now you know how high the projectile went and you know how long it took to get the there. however, is that how long it took the projectile to get back to its initial height? no, it actually takes 2*t ammount of time (because it has to go up and then come back down, which takes equal ammount of time).

so, you have vertical height, and time, now you need to know how far it went. that is pretty trivial when you use:

df = di + vi * t + (1/2) * a * t2

this says "the final displacement is equal to the initial displacement plus the initial velocity times the time plus one half the acceleration times the time squared". as i said above in the assumptions, there is no air resistance (or other forms of friction/energy loss) so the initial horizontal speed is equal to the final horizontal speed:

vxi = vxf

so therefore "a" in this case is equal to zero. lets also say we set our zero point at the location the projectile was fired from ("di = 0"), we get:

df = vi * t

and t is actually the 2*t value from above (two times the time it took to get to the top of the path).

now, that is how to do it with basic math and physics. if you want air resistance or any other form of drag, then it becomes a much more complicated problem.

hope that helps! :)

Share this post


Link to post
Share on other sites
If you consider a triangle:

/|o
/ |p
/ |p
/ |o
/ |s
/ |i
/ |t
/ |e
/ theta |
/_\_______|
adjacent


Where:
tan(theta) = (length of opposite side) / (length of adjacent side)

isolating theta:

theta = arctan((length of opposite side) / (length of adjacent side))

so to get the directions:

xdirection = cos(theta) * speed * timestep
ydirection = sin(theta) * speed * timestep

EDIT: xdirection and ydirection should be deltaX and deltaY instead since speed and the timestep were already multiplied

Share this post


Link to post
Share on other sites
eek..this is my bad. I should've said this was a 2D top-down game. I don't even know why I forgot to mention that. :( sorry bout that. But thankyou for the information. Definately going to save it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this