Question

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

Was it helpful?

Solution

You are misapplying the fundamental equations of motion here. In the example above you are in effect, cancelling out the effect of gravity by adding it back in.

The entirety of the effect of gravity is already taken into account by updating your velocity. Additionally, it doesn't make sense to be using absolute time in your incremental calculations and pretty much every instance of this.time should be replaced by deltaTime.

Also on a conceptual level you are evaluating a differential equation, and so this should all be in a loop. Additionally, you are way overcomplicating things. Fixed example:

this.speed = 60.0;
this.deltaTime = 0.0001;
this.endTime = 10.0;
this.x = 1.0;
this.y = 10;
this.ay = -9.8;
this.ax = 0;
this.angle = 45;
this.vx = this.speed*Math.cos(this.angle*(Math.PI/180.0))
this.vy = this.speed*Math.sin(this.angle*(Math.PI/180.0))

double time = 0.0;
while (time < this.endTime){
    time += this.deltaTime;
    this.x += this.vx*this.deltaTime;
    this.y += this.vy*this.deltaTime;

    this.vx += this.ax*this.deltaTime;
    this.vy += this.ay*this.deltaTime;
}

Over time that will be a projectile in motion. Let me know if you have any questions.

Oh, looks like you implemented the code mostly right, the error is now in the way you're making your 2d Point and returning from your method. I'm not sure what IDE you're using, but it should really be giving you a compile error.

Point2D p = new Point2D.Double(x,y);
return p;

Should be replaced with:

return new Point2d.Double(this.x, this.y)

OTHER TIPS

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.

In these lines, I think you accidentally swapped ax and ay

// 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;

Since ax is zero (due to objects not gaining horizontal velocity in projectile motion), the typo makes vy constant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top