Projectile logic

Started by
2 comments, last by C0lumbo 9 years, 4 months ago

How does the logic of a projectile as in DDTank? Excluding the wind, with an angle and a force as input, how we calculate the trajectory of the projectile until it collides with something?

Ps.:Using Python and Pygame

Advertisement

Math.

What will you make?

Found by googling "projectile motion equations" (give that a try to look at some 500,000 hits): projectile trajectory equations are discussed well here. Those equations are based on initial velocity, rather than "force as input." Assuming by "force" you mean something related to the initial propulsion for the projectile, just convert the initial impulse (force x some-short-time) on the projectile mass to initial velocity.

That link discusses the angle to hit particular coordinates if you know your target. To determine if the projectile hits something as it travels, use small increments of time and do your collision detection using calculated x(t) and y(t) values.

I'm not familiar with DDTank, and can't help you with Python, I'm afraid.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I think googling "projectile motion equations" is going to direct you more toward equations that will help you answer 'where will it land' or 'what launch angle should I use' kind of questions, they'll be useful for AI aiming no doubt.

If you want the sort of projectile movement you see in DDTank (which from a cursory googling looks like a worms clone to me), then you're probably more interested in frame by frame movement. Which is even simpler.

You're going to have 3 vectors:

Acceleration (gravity)

Velocity

Position

Each update you need to know the duration of the frame/tick (dt = delta time).

void UpdateProjectile(float dt)

{

Position += Velocity*dt;

Velocity += Acceleration*dt;

if (CollisionTest())

{

Kaboom();

}

}

Updating the position/velocities of physics objects is known as integration, and that function is the simplest most intuitive approach and should work fine for your needs. There are better approaches which reduce calculation error slightly (verlet integration for example), but that's probably not necessary at this point.

This topic is closed to new replies.

Advertisement