Projectile Motion of an Object

ComputerLocus picture ComputerLocus · Jun 10, 2013 · Viewed 8.8k times · Source

My object is currently only going in a straight line at the set angle using the following code:

    this.time = this.time + deltaTime;

    // Vertical : Speed * Sine * Angle
    double vy = (this.speed * Math.sin(this.angle)) + this.ax*this.time ;
    // Horizontal : Speed * Cosine * Angle
    double vx = (this.speed * Math.cos(this.angle)) + this.ay*this.time;

    this.x = this.x + vx*this.time;
    this.y = this.y + vy*this.time + this.ay*(this.time*this.time); 

    vx += this.ax * this.time;
    vy += this.ay * this.time;

I'm assuming I have made some kind of math error in relation to the calculations as it seems the x value is correct, though the y value is not coming back down.

Here are my initial values in case you are wondering:

    this.time = 0.0;
    this.deltaTime = .0001;
    this.x = 1.0;
    this.y = 10;
    this.speed = 60.0;
    this.ay = -9.8;
    this.angle = 45;
    this.ax = 0.0;

Is this a stupid mistake I made causing this, or am I missing some key concept here?

GamePanel.java : https://gist.github.com/Fogest/7080df577d07bfe895b6

GameLogic : https://gist.github.com/Fogest/36aba3e1a7fc30984e4e

Answer

rgettman picture rgettman · Jun 10, 2013

You must convert your angle from degrees to radians before using the angle in trigonometric methods. From the Math.sin Javadocs:

Parameters:

a - an angle, in radians.

Multiply by Math.PI and then divide by 180 to convert degrees to radians.