2

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

ComputerLocus
  • 3,448
  • 10
  • 47
  • 96

3 Answers3

3

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
3

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)
Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
  • Using this the object no longer moves. I excluded the time checking for this test, but that should not matter. I'm updating the original post to have links to the two small code classes as that may help. – ComputerLocus Jun 10 '13 at 22:24
  • Nevermind sir, this works perfectly! My delta time was way too low for the changes to be visible. I checked out some System outs and the values are correct and upon increasing the delta time I was able to see that the projectile motion is occurring! Thanks for the help, this solved it! I was having a hard time understanding how to implement the physics concept programmatically. – ComputerLocus Jun 10 '13 at 23:26
  • 1
    Glad to hear it! Yea, physics is usually taught in a much more complicated than needed way. Velocity is just the change in position, and acceleration is just the change in velocity. Simple as that :) Also, don't forget to accept my answer if it was helpful to you :) – Slater Victoroff Jun 10 '13 at 23:28
  • Yeah I realized I had forget to hit the accept button, I only had hit the up arrow! My problem was actually integrating the physics into a program. I'm so used to doing it on pen and paper that I had no idea how to implement it into a real dynamic moving situation. – ComputerLocus Jun 11 '13 at 01:10
1

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.

Jacob
  • 995
  • 4
  • 12
  • This did not fix the issue, still have the same issue, though this would have probably caused issues down the road, therefore thank you! – ComputerLocus Jun 10 '13 at 22:25