I am writing a simulation in Java in which objects operate under Newtonian physics. An object can have a force applied to it, and the resulting speed makes it move around the screen. The nature of the simulation means that the objects move in discrete steps depending on the time elapsed between the current and previous iteration of the animation cycle; eg
public void animationLoop() { long prev = System.currentTimeMillis(); long now; while(true) { long now = System.currentTimeMillis(); long deltaMillis = now - prev; prev = now; if (deltaMillis > 0) {
There is a problem if the animation stream is delayed in some way, causing a large amount of time for the ellipse (the classic case is under Windows, in which pressing and holding to minimize / maximize prevents redrawing), which causes objects to move at an alarming speed. My question is: is there a way to determine the time spent in the animation stream and not the acceleration time, or can someone suggest a workaround to avoid this problem?
My only thought so far is to keep deltaMillis some upper bound.
source share