• Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 Edited by PsionicTransvection

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement