Projectile Motion (Parabola)

Started by
3 comments, last by germangb 11 years, 8 months ago
So here is the thing I`m working on a small game which it looks like this

post-195425-0-37618500-1345207703_thumb.png


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 :/ I`m open to any suggestions or criticisms I`m sure I`m 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 it`s 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)
{
readyToFire = true;
System.out.println("Boom");
boom = true;
//power = 0;
}

}
}

I also attached a rar with the whole code so far it`s only 2 classes it`s not much
Advertisement
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.
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.
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.

Between Scylla and Charybdis: First Look <-- The game I'm working on

Object-Oriented Programming Sucks <-- The kind of thing I say

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.

This topic is closed to new replies.

Advertisement