Jump to content
• ### What is your GameDev Story?

• Advertisement

Public Group

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

This topic is 4931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
try this;

assuming all floats

BulletPosX += cosf(angle) * power;
BulletPosY += sinf(angle) * power;
BulletPosX += cosf(windAngle) * windPower;
BulletPosY += sinf(windAngle) * windPower;

#### Share this post

##### Share on other sites
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?

#### Share this post

##### Share on other sites
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);

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
Quote:
 Original post by JohnBoltonFirst, "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!

#### Share this post

##### Share on other sites
Quote:
 Original post by sofakngI'm confused on that first equation though. s0 = sqrt( 2E/m )What does E represent?Also, in this equation: P = P0 + V0t + Agt2/2I'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.

#### Share this post

##### Share on other sites
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?

#### Share this post

##### Share on other sites
Quote:
 Original post by sofakngWouldn'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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 27
• 16
• 10
• 10
• 11
• Advertisement
• ### Forum Statistics

• Total Topics
634101
• Total Posts
3015528
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!