andrew2110

15-08-2006, 09:09 PM

Hello everyone! I have begun creating a MMORPG side scrolling game and have run in to a bit of a problem.

My problem is that when run on different computers, the sprites move at different speeds. First off this was because I was basing the movement rates on the dxtimer component but now thanks to these forums I've learnt this is very bad. I've now come up with the function below to generate xspeeds and yspeeds based on timegettime() but obviously i've gone horribly wrong somewhere as this does not work as sprites move at different rates still.

Passed into the function below are variables:

now:=timegettime();

last:=timegettime from previous frame

xspeed:=speed of sprite which is between -3 and +3.

My thinking behind this was that I could get the framerate by dividing 1000 by the difference between the two times. I could get the target distance (distance that the sprite should move in around 1 second) by multiplying xspeed by 50. Finally I could work out the actual speed by dividing framerate by target distance to give how many pixels the sprite needs to move that frame to reach the target distance...

function getxs(now,last:cardinal;xspeed:single):single;

var

framerate:smallint;

targetdistance:single;

begin

result:=0;

if now-last>0 then

begin

framerate:=1000 div (now-last);

if xspeed<>0 then

begin

targetdistance:=xspeed*50;

result:=targetdistance / framerate;

end;

end;

end;

Obviously im doing something pretty wrong here so any advice would be more than welcome!

Thanks!

My problem is that when run on different computers, the sprites move at different speeds. First off this was because I was basing the movement rates on the dxtimer component but now thanks to these forums I've learnt this is very bad. I've now come up with the function below to generate xspeeds and yspeeds based on timegettime() but obviously i've gone horribly wrong somewhere as this does not work as sprites move at different rates still.

Passed into the function below are variables:

now:=timegettime();

last:=timegettime from previous frame

xspeed:=speed of sprite which is between -3 and +3.

My thinking behind this was that I could get the framerate by dividing 1000 by the difference between the two times. I could get the target distance (distance that the sprite should move in around 1 second) by multiplying xspeed by 50. Finally I could work out the actual speed by dividing framerate by target distance to give how many pixels the sprite needs to move that frame to reach the target distance...

function getxs(now,last:cardinal;xspeed:single):single;

var

framerate:smallint;

targetdistance:single;

begin

result:=0;

if now-last>0 then

begin

framerate:=1000 div (now-last);

if xspeed<>0 then

begin

targetdistance:=xspeed*50;

result:=targetdistance / framerate;

end;

end;

end;

Obviously im doing something pretty wrong here so any advice would be more than welcome!

Thanks!