# Projectile Motion (Parabola)

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

## Recommended Posts

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 its not much Edited by PsionicTransvection

##### Share on other sites
You should have a look at some 2D physics references. I found this cute tutorial (http://www.rodedev.com/tutorials/gamephysics), 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.

##### 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:
 Vector2 bulletVelocity = Vector2.Transform(new Vector2(-bulletPower),0), Matrix.CreateRotationZ(turretRotation)); //rotates the bullet coming out of the turret. 

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

 void updateBullet() { Gravity = new Vector2(0, -9.837f); bulletVelocity += Gravity; } `

http://www.riemers.n...rp/series2d.php 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

##### 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.

http://gafferongames...gration-basics/

If you want to make something very simple, without actual physics, you can 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

##### 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.