#### Archived

This topic is now archived and is closed to further replies.

# Physics: Cannon Fodder (Projectile)

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

## Recommended Posts

Hi, I was wondering if anyone could point out some physics code for a Cannon Fodder game (where two cannons are placed on either side of the screen and the user enters the power and angle to shoot and hit the other tank). I dont really understand it so some psuedo code or real code would be very helpful. I know the user enters the power of the shot and the angle but after that I get lost. Any help would be greatly appreciated. Thanks

##### Share on other sites
first off, they are either refferred to tank games or games like scorched earth. they are NOT reffered to caddon fodder games since cannon fodder style games do not involve tanks. you probally never heard of or played the game which is quite old. you should since its a quite fun action/puzzle game with solders.

now to yoru physics question. its trival to implemenet the physics suing high school math. first use a little trigometry to to figure out the direction of the shot from the angle of the tturret. simply put (dx and dy are the changes in direction in those axis):
dx = power*sin(angle);
dy = power*cos(angle);

now its a matter of
xcurr+=dx;
ycurr+=dy;
dy-=gravity;

this does not include wind, but thats just modifying dx like doing gravity. though you may wish to place speed restraints on wind and gravity.

##### Share on other sites
Ok explain to me if I understand this correctly;
say power=100;
angle=45;
gravity=9;

dx and dy are calculated only once when the shot is taken
dx = power*sin(angle);
dy = power*cos(angle);

and this is put in a loop until ycurr hits the ground
xcurr+=dx;
ycurr+=dy;
dy-=gravity;

Is that correct?

##### Share on other sites
Well, not exactly

The problem is that gravity _accelerates_ an object, instead of only moving it at one velocity. To solve the problem, it is best to split up the movement into two distances, along the x and y-axis.

Let''s say the player launches a projectile, with 100m/s and the angle of the shot is 50 degrees (where a shot horizontal is 0 degrees). First, split up the velocity of the projectile into two other velocities:

vx = cos(50) * 100
vy = sin(50) * 100

Then calulate the influence of gravity on the situation. Obviously, gravity will only affect the vy. dt is (delta-time, which means difference of time) the time to redraw the game screen for example (with a game running 60fps, you might want to set this value to 1/60).

1: vy = vy - 9.8 * dt

Now you know these velocities, the distance that the projectile has travelled in dt seconds can be calculated, by:

2: x = x + vx * dt
3: y = y + vy * dt

And you''ll have coordinates where you can draw the projectile. To update it''s position, just recalculate steps 1 to 3 with a correct dt value.

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631423
• Total Posts
3000005
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!