I’m writing a simple app to simulate gravity on an object for a Physics project
The problem occurs in the Maths when the ball is at the top of the fall… because I have to cast to (int) for movement, it stalls until the value is greater than 0.5 or less than -0.5… So it just hangs there.
Here is the code for the movement of the ball…
// Variables
private final long DELAY = 20; // Timer delay (10 ms)
private float a = 9.8f; // Default acceleration 9.80 m/s/s
private float t = DELAY / 1000.0f;
public void cycle(){
// Increase velocity based on rate of acceration
myBall.setY((int)(myBall.getY() + (t * (myBall.getVY() + a * t))));
myBall.setVY(myBall.getVY() + a * t);
//System.out.println(myBall.getY());
//System.out.println(myBall.getVY());
if(myBall.getY() > getHeight() - myBall.getRadius() * 2){
myBall.setVY(-myBall.getVY()*myBall.getElast());
}
/Edit - Added the variables… note getVY returns a float (myball current velocity)