• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

### #Actualaltich88

Posted 22 December 2012 - 11:01 AM

Thanks for your reply. I have changed the delta argument to a double but unfortunately the jerky movement persists.

When you suggested not mixing nanoseconds/milliseconds/seconds, how would I avoid this? Presumably I need to use some sort of milli/nanosecond timer at some point and convert the returned value to seconds?

As well as the new code I've also posted an example of how an object's position is updated and then drawn using the delta argument in case there is an error in the logic on that end too.

The object's position is stored and calculated using doubles, but when it comes to drawing it I am forced to give integer x and y arguments to specify the pixel position - could this rounding lead to jerky movement?

Many thanks for your help so far.

public void run() {
long desiredFPS = 60;
long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;

long beginLoopTime;
long endLoopTime;
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
long deltaLoop;
double deltaSec = 0;

while (isRunning) {
beginLoopTime = System.nanoTime();

window.panel.repaint();

lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();

onUpdateGame(deltaSec);

deltaSec = ((currentUpdateTime - lastUpdateTime) / 1000000000.0);

endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;

if (deltaLoop > desiredDeltaLoop) {
// Do nothing. We are already late
} else {
try {
} catch (InterruptedException e) {
// Do nothing
}
}
}
}

// position.x and position.y are doubles and 100 is the desired pixels/s to move
position.x += (GameApp.deltaMs * 100.0);
position.y += (GameApp.deltaMs * 100.0);

g2d.translate(position.x, position.y);
g2d.setColor(Color.GREEN);

g2d.fillRect(0, 0, 100, 100);

### #4altich88

Posted 22 December 2012 - 10:59 AM

Thanks for your reply. I have changed the delta argument to a double but unfortunately the jerky movement persists.

When you suggested not mixing nanoseconds/milliseconds/seconds, how would I avoid this? Presumably I need to use some sort of milli/nanosecond timer at some point and convert the returned value to seconds?

As well as the new code I've also posted an example of how an object's position is updated and then drawn using the delta argument in case there is an error in the logic on that end too.

The object's position is stored and calculated using doubles, but when it comes to drawing it I am forced to give integer x and y arguments to specify the pixel position - could this rounding lead to jerky movement?

Many thanks for your help so far.

public void run() {
long desiredFPS = 60;
long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;

long beginLoopTime;
long endLoopTime;
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
long deltaLoop;
double deltaSec = 0;

while (isRunning) {
beginLoopTime = System.nanoTime();

window.panel.repaint();

lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();

onUpdateGame(deltaSec);

deltaSec = ((currentUpdateTime - lastUpdateTime) / 1000000000.0);

endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;

if (deltaLoop > desiredDeltaLoop) {
// Do nothing. We are already late
} else {
try {
} catch (InterruptedException e) {
// Do nothing
}
}
}
}

// position.x is a double and 10 the desired pix/s to move
position.x += (GameApp.deltaMs * 100.0);
position.y += (GameApp.deltaMs * 100.0);

g2d.translate(position.x, position.y);
g2d.setColor(Color.GREEN);

g2d.fillRect(0, 0, 100, 100);

### #3altich88

Posted 22 December 2012 - 10:57 AM

Thanks for your reply. I have changed the delta argument to a double but unfortunately the jerky movement persists.

When you suggested not mixing nanoseconds/milliseconds/seconds, how would I avoid this? Presumably I need to use some sort of milli/nanosecond timer at some point and convert the returned value to seconds?

As well as the new code I've also posted an example of how an object's position is updated and then drawn using the delta argument in case there is an error in the logic on that end too.

The object's position is stored and calculated using doubles, but when it comes to drawing it I am forced to give integer x and y arguments to specify the pixel position - could this rounding lead to jerky movement?

Many thanks for your help so far.
public void run() {		long desiredFPS = 60;		long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;				long beginLoopTime;		long endLoopTime;		long currentUpdateTime = System.nanoTime();		long lastUpdateTime;		long deltaLoop;		double deltaSec = 0;				while (isRunning) {			beginLoopTime = System.nanoTime();			window.panel.repaint();			lastUpdateTime = currentUpdateTime;			currentUpdateTime = System.nanoTime();						onUpdateGame(deltaSec);			deltaSec = ((currentUpdateTime - lastUpdateTime) / 1000000000.0);						endLoopTime = System.nanoTime();			deltaLoop = endLoopTime - beginLoopTime;			if (deltaLoop > desiredDeltaLoop) {				// Do nothing. We are already late			} else {				try {					Thread.sleep((desiredDeltaLoop - deltaLoop) / 1000000);				} catch (InterruptedException e) {					// Do nothing				}			}		}}
// position.x is a double and 10 the desired pix/s to moveposition.x += (GameApp.deltaMs * 100.0);position.y += (GameApp.deltaMs * 100.0);g2d.translate(position.x, position.y);g2d.setColor(Color.GREEN);g2d.fillRect(0, 0, 100, 100);

### #2altich88

Posted 22 December 2012 - 10:57 AM

Thanks for your reply. I have changed the delta argument to a double but unfortunately the jerky movement persists.

When you suggested not mixing nanoseconds/milliseconds/seconds, how would I avoid this? Presumably I need to use some sort of milli/nanosecond timer at some point and convert the returned value to seconds?

As well as the new code I've also posted an example of how an object's position is updated and then drawn using the delta argument in case there is an error in the logic on that end too.

The object's position is stored and calculated using doubles, but when it comes to drawing it I am forced to give integer x and y arguments to specify the pixel position - could this rounding lead to jerky movement?

Many thanks for your help so far.
public void run() {		long desiredFPS = 60;		long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;				long beginLoopTime;		long endLoopTime;		long currentUpdateTime = System.nanoTime();		long lastUpdateTime;		long deltaLoop;		double deltaSec = 0;				while (isRunning) {			beginLoopTime = System.nanoTime();			window.panel.repaint();			lastUpdateTime = currentUpdateTime;			currentUpdateTime = System.nanoTime();						onUpdateGame(deltaSec);			deltaSec = ((currentUpdateTime - lastUpdateTime) / 1000000000.0);						endLoopTime = System.nanoTime();			deltaLoop = endLoopTime - beginLoopTime;			if (deltaLoop > desiredDeltaLoop) {				// Do nothing. We are already late			} else {				try {					Thread.sleep((desiredDeltaLoop - deltaLoop) / 1000000);				} catch (InterruptedException e) {					// Do nothing				}			}		}}
// position.x is a double and 10 the desired pix/s to moveposition.x += (GameApp.deltaMs * 100.0);position.y += (GameApp.deltaMs * 100.0);g2d.translate(position.x, position.y);g2d.setColor(Color.GREEN);g2d.fillRect(0, 0, 100, 100);

### #1altich88

Posted 22 December 2012 - 10:55 AM

Thanks for your reply. I have changed the delta argument to a double but unfortunately the jerky movement persists.

When you suggested not mixing nanoseconds/milliseconds/seconds, how would I avoid this? Presumably I need to use some sort of milli/nanosecond timer at some point and convert the returned value to seconds?

As well as the new code I've also posted an example of how an object's position is updated and then drawn using the delta argument in case there is an error in the logic on that end too.

The object's position is stored and calculated using doubles, but when it comes to drawing it I am forced to give integer x and y arguments to specify the pixel position - could this rounding lead to jerky movement?

Many thanks for your help so far.

public void run() {
long desiredFPS = 60;
long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;

long beginLoopTime;
long endLoopTime;
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
long deltaLoop;
double deltaSec = 0;

while (isRunning) {
beginLoopTime = System.nanoTime();

window.panel.repaint();

lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();

onUpdateGame(deltaSec);

deltaSec = ((currentUpdateTime - lastUpdateTime) / 1000000000.0f);

endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;

if (deltaLoop > desiredDeltaLoop) {
// Do nothing. We are already late
} else {
try {
} catch (InterruptedException e) {
// Do nothing
}
}
}
}
// position.x is a double and 10 the desired pix/s to move
position.x += (GameApp.deltaMs * 100.0);
position.y += (GameApp.deltaMs * 100.0);

g2d.translate(position.x, position.y);
g2d.setColor(Color.GREEN);

g2d.fillRect(0, 0, 100, 100);



PARTNERS