Followers 0

# Projectile Motion (Parabola)

## 4 posts in this topic

So here is the thing Im working on a small game which it looks like this

there are 2 tanks shooting at each other in turns and you are supposed to change the angle (moving arrow up or arrow down) and the power with which the projectile is flying (holding down the space the longer you hold the more power the projectile gets).

So far managed to make a small figure moving around the screen and shooting but until this point I made it shoot only in a straight line
searched a lot of links and videos about this problem I found the formulas needed for calculating the curve but still have problems applying it in my code :/ Im open to any suggestions or criticisms Im sure Im doing something wrong all advices and code examples will be gladly appreciated looking forward for your posts I really need help with the parabola problem .

Here is the shoot method which makes the projectile move its a rectangle :

(sorry for not putting the code in tags for some reason when I do it does not print the whole code)

public void shoot()
{
if (shot)
{
bullet.x -= 5;
bullet.y--;

System.out.println(bullet.y);
if (bullet.x <= 0)
{
System.out.println("Boom");
boom = true;
//power = 0;
}

}
}

I also attached a rar with the whole code so far its only 2 classes it`s not much Edited by PsionicTransvection
0

##### Share on other sites
You should have a look at some 2D physics references. I found this cute tutorial ([url="http://www.rodedev.com/tutorials/gamephysics/"]http://www.rodedev.com/tutorials/gamephysics[/url]), it seems to explain what you're trying to do. You should be able to find a lot of material on 2D game physics on the web.
0

##### Share on other sites
your best bet is to use vectors.
Your bullet come out of the gun at a certain velocity, so for instance:
[code]
Vector2 bulletVelocity = Vector2.Transform(new Vector2(-bulletPower),0), Matrix.CreateRotationZ(turretRotation));
//rotates the bullet coming out of the turret.
[/code]

this will make the bullet zip across the screen in a linear fashion, to make that parabolic effect, addGravity:

[code]
void updateBullet() {
Gravity = new Vector2(0, -9.837f);
bulletVelocity += Gravity;
}
[/code]

[url="http://www.riemers.net/eng/Tutorials/XNA/Csharp/series2d.php"]http://www.riemers.n...rp/series2d.php[/url] this article talks about and the type of game your making, thats c# using XNA, same principal can be applied to java though. Edited by Andy474
0

##### Share on other sites
You don't need to calculate the parabola, you can just use Newtonian mechanics, and it will naturally follow a parabolic path. Andy's answer is pretty much correct, but you will have to integrate your calculations with respect to your timestep.

[url="http://gafferongames.com/game-physics/integration-basics/"]http://gafferongames...gration-basics/[/url]

If you want to make something very simple, without actual physics, you [i]can[/i] easily calculate the parabola, and just solve the equation f(x) for some increasing/decreasing x on each timestep, and then move your projectile to the point (x,y) given by the solution. Edited by nox_pp
0

##### Share on other sites
a parabolic throw consists of 2 movements: vertical (accelerated because of gravity) and horizontal (constant). That way, if you're working with pixels, you could simulate the action of gravity by adding some value to the vertical velocity in every frame, wich should point upwards at the begining.
0

## Create an account

Register a new account