Skip to content

Commit

Permalink
Speed should not depend on frame rate
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiled committed Mar 10, 2014
1 parent 06ad9dc commit e82d46c
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public Point getCurrentPosition ()
return new Point (position);
}

private Point calculateNewPosition ()
private Point calculateNewPosition (long dTime)
{
Point newPos = new Point (position);
newPos.x += vx;
newPos.y += vy;
newPos.x += vx * dTime / 10;
newPos.y += vy * dTime / 10;
if (newPos.x < 0)
{
newPos.x = -newPos.x;
Expand All @@ -66,23 +66,26 @@ private Point calculateNewPosition ()
return newPos;
}

public synchronized void stepOver ()
public synchronized void stepOver (long dTime)
{
if (width == 0 || height == 0 || Math.abs (vx) >= width || Math.abs (vy) >= height)
{
return;
}

position = calculateNewPosition ();
position = calculateNewPosition (dTime);
component.repaint ();
}

@Override
public void run ()
{
long time = System.currentTimeMillis();
while (!Thread.interrupted ())
{
stepOver ();
long newTime = System.currentTimeMillis();
stepOver (newTime - time);
time = newTime;
try{
Thread.sleep (20L);
} catch (InterruptedException e)
Expand Down

0 comments on commit e82d46c

Please sign in to comment.