Does anybody have a simple ballistic trajectory algorithm I can use?

Started by
21 comments, last by JohnBolton 18 years, 9 months ago
Me and a friend are trying to make a Scorched Earth type of game and we can't figure out a ballistic trajectory algorithm. Does anybody have one that I can use? Ideally, I'd like to have four variables: angle, power, weight (some projectiles will "weigh" more), and wind. However, if I could get anything that simply used power + angle that would be great :) Thanks in advance
Advertisement
just learn some basic physics. It's pretty easy. This page seems to describe it nicely. Make sure to start from the beginning of the page or the Projectile Trajectory section won't make any sense:

http://www.atarimagazines.com/startv5n2/physics.html

don't worry about wind to start with. But once you get there, wind in those games is just a horizontal acceleration.

-me
try this;

assuming all floats

BulletPosX += cosf(angle) * power;
BulletPosY += sinf(angle) * power;
BulletPosX += cosf(windAngle) * windPower;
BulletPosY += sinf(windAngle) * windPower;
Palidine: Thanks for the link! Using the formulas in that page I've been able to create an algorithm that draws a parabolic curve but it doesn't factor in wind and I don't think gravity works properly.

Raymond_Porter420: Thanks for the help but I'm not sure how to use that. Don't I have to give it a time or something? I'm very confused :(

Ideally I'm planning to have an object for each projectile (missle, bullet, laser, etc). Inside that object I'm going to keep track of the x,y coordinates as well as the velocity. When the projectile is created it will generate a trajectory but every frame the trajectory will be checked for any collisions. If there is a collision with a specific object (magnetic field, etc) then I'd like to recalculate a new trajectory.

Am I going about this whole thing wrong?
using the accelerated motion formula from the link above:
pos = vi + v*t + .5*a*t^2
you can calculate the tajectory once you know your accelerations (and inital v)

F=ma can calculate the acceleration you need. Supply the power as a force(F),
and devide by mass(m) to get the acceleration. The wind can also be represented as a force.

since the componets of a vector can be broken up, the sin/cos formula above
allows you to split up your power by the angle supplied.
forcex = cos(angle);
forcey = sin(angle);
First, "power" is not appropriate in this situation. I think a better parameter is "kinetic energy". A gun can give a projectile a fixed amount of kinetic energy, so heavier projectiles will travel more slowly than lighter projectiles. The formula for kinetic energy is E = ms2/2, so the initial speed is this:
    s0 = sqrt( 2E/m )    m is the weight (actually mass) of the projectile 
Next, the initial direction of travel, given an angle, is this:
    D0 = [ cos(angle), sin(angle) ] 
Now with the initial speed and initial direction, you have the initial velocity vector:
    V0 = s * D0 = [ s*cos(angle), s*sin(angle) ] 
Finally, ignoring wind and air resistance, the position of the projectile at any time is given by this formula:
    P = P0 + V0t + Agt2/2    P0 is the initial position    Ag is acceleration due to gravity [0, -9.8 ] 
Now, dealing with air resistance and wind makes this much more complicated (if done accurately). You can simplify things by ignoring air resistance and treating wind as horizontal force. Since, f = ma, and thus a = f/m, you can treat wind as a simple acceleration that is included in the equation. The acceleration due to wind is this:
    Aw = [ fw/m, 0 ] 
The final equation looks like this:
    P = P0 + V0t + (Ag + Aw)t2/2 


So, given the energy of the gun (E), the position of the gun (P0), the angle of the gun (angle), the mass of the projectile (m), and the force of the wind (fw), you can calculate the position of the projectile with the above equation.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
First, "power" is not appropriate in this situation. I think a better parameter is "kinetic energy". A gun can give a projectile a fixed amount of kinetic energy, so heavier projectiles will travel more slowly than lighter projectiles. The formula for kinetic energy is E = ms2/2, so the initial speed is this:
    s0 = sqrt( 2E/m )    m is the weight (actually mass) of the projectile 

Thanks for the detailed reply!

I'm confused on that first equation though. s0 = sqrt( 2E/m )

What does E represent?

Also, in this equation: P = P0 + V0t + Agt2/2

I'm assuming P0, V0, and Ag are all vectors right? What does t represent?

Finally, in your last equation, you have Aw did you mean Fw?

Sorry for all of the questions but I really think I'm beginning to understand all of this!
Quote:Original post by sofakng
I'm confused on that first equation though. s0 = sqrt( 2E/m )
What does E represent?
Also, in this equation: P = P0 + V0t + Agt2/2
I'm assuming P0, V0, and Ag are all vectors right? What does t represent?
Finally, in your last equation, you have Aw did you mean Fw?


E is the kinetic energy output of the gun. s0 is the muzzle velocity. The equation is simply a rearrangment of the standard kinetic energy equation E = mv2/2. This doesn't match reality but it should be good enough.

P0, V0, Ag, and Aw are vectors. t is elapsed time.

Aw is the acceleration due to the wind. It comes from the force of the wind parameter and the equation a = f/m.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Wouldn't I need to know the value for E and m to calculate s0 = sqrt( 2E/m )? Does that mean I calculate E = mv2/2 first (by giving it a mass and a velocity [angle?]) and then using the E value for the other equation?
Quote:Original post by sofakng
Wouldn't I need to know the value for E and m to calculate s0 = sqrt( 2E/m )? Does that mean I calculate E = mv2/2 first (by giving it a mass and a velocity [angle?]) and then using the E value for the other equation?


E here represents how much energy is used to launch the projectile, which is a variable you should supply.

This topic is closed to new replies.

Advertisement