Original Post
I have a problem with the logic of my program executing slowly for some reason. I can't get my simple Pong game to run smoothly. It's obviously not too graphics intensive as I've seen far more powerful JOGL applications run just fine. I keep examining source code from other applications, comparing, and trying to fine-tune mine, but nothing seems to work. My problem is that the only way to control the ball's speed is to have the ball's thread (which doesn't do anything except move the ball) sleep for shorter periods of time. If I increment the ball's position anymore than 1 pixel at a time (i.e. the magnitude is greater than 1), the game looks choppy. Should I be going about this a different way? This is the section of code that moves the ball.
private void move()
{
if((x >= TOP_BOUNDARY) ||
(y >= LEFT_BOUNDARY) ||
(y <= 0) ||
(x <= 0))
{
angle += Math.PI / 2;
if(angle > Math.PI * 2)
{
// Keep the angle between 0 and 360 degrees
angle -= Math.PI * 2;
}
}
x += magnitude * Math.sin(angle);
y += magnitude * Math.cos(angle);
}
public void run() {
while(Thread.currentThread() == ballThread)
{
start_time = System.currentTimeMillis();
move();
try {
delay += (System.currentTimeMillis() - start_time);
Thread.sleep(16 - delay);
} catch(InterruptedException e) {}
}