Sign in to follow this  
SnowJim

Simple projectile path?

Recommended Posts

Hay! I am doing a simple version of the game worms (in 2D) and need a math fomular for calculate the bullet path. Parameteras that I need to use are power, angle, weight and gravity. unfortunatly it was to long since I did som math so it would be much appreciated if someone could provide a simple formula and explain how it works. BestRegards SnowJim

Share this post


Link to post
Share on other sites
There are several ways to go about this sort of thing. Precalculating the path goes like this:

Let
Uo = initial velocity in X direction.
Vo = initial velocity in Y direction.
Xo = initial position in X direction.
Yo = initial position in Y direction.
t = time (where t = 0 is the time of launch)
g = acceleration due to gravity

Use equations of motion:
X(t) = Xo + (Uo * t)
Y(t) = Yo + (Vo * t) + ((g * t^2) / 2)

To find a vector position as a function of time:
R(t) = (X(t), Y(t))

I've assumed +Y to be 'up' throughout, so the value of g should be negative (i.e. 'down')
You can use basic trigonometry to calculate Uo and Vo from the angle of launch and power of the shot.
Bullet weight plays no part in the calculation once the projectile is in the air, though it could affect the maximum power of the shot or some other factor.


Since games operate on a frame-by-frame basis it may not make sense to precalculate entire paths. You could look at integrating forward by the timestep for one frame - essentially a more robust physics model that doesn't rely on this sort of fixed precalculation, so allows you to use much more sophisticated physics (potentially wind, air resistance in your case) without adding a lot of complexity.

Incidentally, in a game like Worms I think you should maybe take some liberties with the realism if it makes the game more fun. Don't be a slave to physics!

Share this post


Link to post
Share on other sites
Those are initial velocities, so you need to break your stuff into components. Say you have a "power" of 50 at an angle of 30 degrees, then:

PowerX = 50*cos(30) = 43.3
PowerY = 50*sin(30) = 25

Then apply these however you want in the formulas. For a simple usage, these could just be the initial velocities instead of a true "power".

Keep in mind that most trig functions take radians by default. To convert degrees to radians, multiply by pi over 180.

Share this post


Link to post
Share on other sites
Thanks!

I now have the following:

> xAngleModifier = Math.Cos(50);
> yAngleModifier = Math.Sin(50);

This sets the x and y modifier for the angle 50 degree.

> xModifier = ((powerControl.Power + xAngleModifier) * 0.000001) * tiks;

This is the X = ((Power + AngleModifier) * speedModification) * t). The tiks are calculated in every frame and the Power can be 0-100.

> yModifier = (((powerControl.Power + yAngleModifier) * 0.000001) * tiks) + (0.000000000001 * Math.Pow(tiks, 2) / 2);

This is the ((Power + AngleModifier) * speedModification) * t) + (gravitationModifier * g).

> x = projectile.StartPosition.Value.X + xModifier;
> y = projectile.StartPosition.Value.Y + yModifier;

Here we add the x and y modifiers to the startposition (Xo + (modifier)/(Yo + (modifier).

The projectile will shoot from left to right and down in a ca 150 degree (if up/top) is 0). The speed is fast and even if the power is set to low (30) the projectile will hit the bottom. What am I missing?

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