•      Sign In
• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

### #ActualKhatharr

Posted 16 February 2013 - 03:11 AM

If you're basing on time then you need to use time instead of frames as your metric for the fraction in the interpolation. For tiled 2D RPGs you can get usually away with just v-syncing and per-pixel movement, though I may get yelled at for saying that.

In the case you'd want to decide how many milliseconds you want it to take to walk one tile and then:

start process:
motion_end_time = now + TIME_IT_TAKES_TO_WALK_ONE_FRAME;
is_moving = true;
then have:
if(is_moving) {
if(now > motion_end_time) {
position = destination_position;
is_moving = false;
}
else {
time_elapsed = motion_end_time - now;
motion_completion_fraction = time_elapsed / TIME_IT_TAKES_TO_WALK_ONE_FRAME;
position = start_position + (PIXELS_PER_TILE * motion_completion_fraction);
}
}
If you're basing it off a timer don't reset the timer. At the start of the logical frame (the 'logical frame' being a single 'tick' of your simulation, not an animation frame) get the current time, subtract the previous frame's time from it and use that as your elapsed time for everything that gets updated in the frame. Definitely do not allow things within the logical frame to do this independently or you'll lose sync between your entities and get weird behavior, since they'll all have different values for elapsed time.

In other words, if you have 3 things that need updated:

Do:
get elapsed time
pass it to thing1's update
pass it to thing2's update
pass it to thing3's update
[everything is synchronized]

Do not:
update thing1 (thing1 determines elapsed time)
update thing2 (thing2 determines elapsed time)
update thing3 (thing3 determines elapsed time)
[time passes while things update - synchronization was lost!]

### #2Khatharr

Posted 16 February 2013 - 03:01 AM

If you're basing on time then you need to use time instead of frames as your metric for the fraction in the interpolation. For tiled 2D RPGs you can get usually away with just v-syncing and per-pixel movement, though I may get yelled at for saying that.

If you're basing it off a timer don't reset the timer. Get the current time, subtract the previous frame's time from it and use that as your elapsed time for the frame. In that case you'd want to decide how many milliseconds you want it to take to walk one tile and then:

start process:
motion_end_time = now + TIME_IT_TAKES_TO_WALK_ONE_FRAME;
is_moving = true;
then have:
if(is_moving) {
if(now > motion_end_time) {
position = destination_position;
is_moving = false;
}
else {
time_elapsed = motion_end_time - now;
motion_completion_fraction = time_elapsed / TIME_IT_TAKES_TO_WALK_ONE_FRAME;
position = start_position + (PIXELS_PER_TILE * motion_completion_fraction);
}
}

### #1Khatharr

Posted 16 February 2013 - 03:00 AM

If you're basing on time then you need to use time instead of frames as your metric for the fraction in the interpolation. For tiled 2D RPGs you can get usually away with just vsyncing, though I may get yelled at for saying that.

If you're basing it off a timer don't reset the timer. Get the current time, subtract the previous frame's time from it and use that as your elapsed time for the frame. In that case you'd want to decide how many milliseconds you want it to take to walk one tile and then:

start process:
motion_end_time = now + TIME_IT_TAKES_TO_WALK_ONE_FRAME;
is_moving = true;
then have:
if(is_moving) {
if(now > motion_end_time) {
position = destination_position;
is_moving = false;
}
else {
time_elapsed = motion_end_time - now;
motion_completion_fraction = time_elapsed / TIME_IT_TAKES_TO_WALK_ONE_FRAME;
position = start_position + (PIXELS_PER_TILE * motion_completion_fraction);
}
}

PARTNERS