Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

I am trying to design a java game with realistic physics?

Like the title says, I am writing a Java game and trying to include realistic trajectory physics. This sounded pretty easy to me but I think I have managed to make it way more complicated than it needs to be, and it's not working. Basically at this point, I have a guy throwing a ball, and the ball velocity and angle are based on the distance and angle from the guy that the mouse is clicked. The x,y of the mouse click are sent to the ball and the position is updated. This doesn't work *at all*. I can't figure out why not.

Really any pointers in the right direction would help. At this point I click on the screen and the ball simply disappears though it should be bounded by upper and lower max y.

This is the move method.

synchronized public void move(){

if(climbing){

if(newY == upperMax){ // test if can no longer move upwards / hit an obstacle. If hit, change to falling

shootXApex = newX;

falling = true;

climbing = false;

}

if(launchX > newX && climbing){ // shoot target to the right

if(newX< (deltaX) && newX< pWidth - missileHeight) // hasn't reached maximum shot trajectory or hit wall

newX = newX + (deltaX/tempo); // shoot right

newY = newY + yInFlight; // shoot up

}

else if(climbing){

newX = newX - deltaX/tempo; // shoot left

newY = newY - yInFlight; // shoot up

}

if(newX == shootXApex ||newY< upperMax || newY== upperMax){

climbing = false;

falling = true;

}

} // end if shooting

if(falling && newY> lowerMax){

// add line to check for platform landing

if(0<launchVectorAngle && launchVectorAngle<90){

newX = newX + deltaX/tempo; // fall right

newY = newY + yInFlight; // fall downwards

}

else{

newX = newX - deltaX/tempo; // fall left

newY = newY + yInFlight; // fall downwards

}

if(newY> lowerMax|| landed){// player has landed or gone off screen

falling = false;

}

maxHeight = false;

}

}

1 Answer

Relevance
  • 8 years ago
    Favorite Answer

    Thank you for posting your code, but java physics can get very complicated very quickly. The solution is to utilize a proven 3rd party framework to do the heavy lifting when it comes to java physics. JBox2D is a possible solution. http://www.jbox2d.org/

Still have questions? Get your answers by asking now.