Hey guys, I'm making a little pokemon game in java and i've come across a problem.
The basic algorithm i use for movement is as follows:
- if the player has pressed a particular key set the speed(float) accordingly (if key stroke was up, X speed should drop (since i'm going up), etc)
the function update() is in charge of updating the current location of the sprite (the player), and i do it as follows:
(assume the location of a sprite is stored in 2 floats called x and y, also assume the velocity is stored in 2 floats called dx and dy).
/**
Updates this Sprite's Animation and its position based
on the Sprite's current velocity.
*/
@Override
public void update(final long elapsedTime) {
x += dx;
y += dy;
resetVelocity();
anim.update(elapsedTime);
}
now, when i run the game on my desktop it looks flawlessly, everything moves smooth as hell and at a constant 60fps, however, when i run it on my laptop, it looks a little choppy (which is to be expected, it's an old laptop), however, my issue is that whenever i move, i move at a MUCH faster rate than on my desktop, (specially when i move diagonally).
So.. i decided to use the update() function's parameter called elapsedTime, this variable holds the time it took the grand game loop to call this function a second time, (varies from 5ms to 30ms), the updated version of the update() function looks as follows:
/**
Updates this Sprite's Animation and its position based
on the velocity.
*/
@Override
public void update(long elapsedTime) {
x += dx * elapsedTime;
y += dy * elapsedTime;
resetVelocity();
anim.update(elapsedTime);
}
it works!.. not really, you see, now the speed is pretty much the same on both machines, which is good, but on my desktop movement looks choppy as hell (even though i'm still running at a constant 60fps) it is playable sure, but it looks pretty choppy, and at 60fps it kind of defeats the purpose.
with the information i've given so far, does anyone have an idea as to why this occurs? or do you have an even better way to deal with movement?
Also, no, this is not homework.
Thanks in advance!