1 registered members (AndrewAMD),
1,089
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
time_step and framerate independent code
#422620
05/13/13 19:20
05/13/13 19:20
|
Joined: Nov 2011
Posts: 274 de
lemming
OP
Member
|
OP
Member
Joined: Nov 2011
Posts: 274
de
|
Hi, so I have this jumping code in my player function (I ripped off the other stuff, as this is enough to show the problem). For some reason I don't understand I get differnt jumping heights at different framerates. Isn't time_step supposed to scale the values to fit the framerate or did I get it wrong? Tested at 30, 60 and 75 fps.
#define PL_GRAVITY (-0.35)
#define PL_MAXGRAVITY (-9)
#define PL_JUMPACC (8)
void pl_UpdatePlayer(PL_PLAYER* pl)
{
me = pl.unit;
VECTOR traceto; vec_set(traceto, my.x);
traceto.z -= 100;
var traceresult = c_trace(my.x, traceto, IGNORE_ME | IGNORE_PASSABLE | IGNORE_PUSH | USE_BOX);
DEBUG_VAR(traceresult, 100);
if ((traceresult == 0) || (traceresult > (9.8))) // Sturz
{
pl.move.gravity.z = maxv(pl.move.gravity.z + (PL_GRAVITY * time_step), PL_MAXGRAVITY * time_step);
}
else
{
pl.move.gravity.z = 0;
if (key_space)
pl.move.gravity.z = PL_JUMPACC*time_step;
}
c_move(me, pl.move.movedir, pl.move.gravity, GLIDE);
if ((traceresult < (9.8)) && (traceresult > 0)) my.z += (9.8) - traceresult;
}
Last edited by lemming; 05/13/13 19:21.
|
|
|
Re: time_step and framerate independent code
[Re: lemming]
#422629
05/13/13 21:20
05/13/13 21:20
|
Joined: Jul 2001
Posts: 6,904
HeelX
Senior Expert
|
Senior Expert
Joined: Jul 2001
Posts: 6,904
|
You are using pl.move.gravity in c_move, so that it is added to the player position, if the player is stürzing. That means, that the .z value of the sturz has to be scaled completely with time_step. But in your Sturz-branch, you just scale a constant (-0.35) with timestep. If pl.move.gravity is e.g. 64, pl.move.gravity.z will be always more or less ~64, because even on a low fps PL_GRAVITY * time_step will be near 0, and that means, ultimately, that your pl.move.gravity.z will be always framerate-dependent (more or less). In order to move with c_move downwards in a framerate independent fashion, you need to scale the value of pl.move.gravity with time_step completely (vec_scale). Since the result is framerate independent then and I guess you want to accelerate downwards, when the player is stürzing, you need to save and refresh the sturzspeed as before:
pl.move.gravity.z = maxv(pl.move.gravity.z + (PL_GRAVITY * time_step), PL_MAXGRAVITY * time_step);
and when you call c_move, you need to calculate the ***current*** framerate independent sturzrate:
VECTOR mySturz;
vec_set(&mySturz, pl.move.gravity);
vec_scale(&mySturz, time_step);
c_move(..., &mySturz, ...);
|
|
|
Re: time_step and framerate independent code
[Re: lemming]
#422685
05/14/13 22:19
05/14/13 22:19
|
Joined: Jul 2001
Posts: 6,904
HeelX
Senior Expert
|
Senior Expert
Joined: Jul 2001
Posts: 6,904
|
I scale now the movement vectors before using them and removed the time_step multiplication for PL_MAXGRAVITY and PL_JUMPACC. Yeah, this is a solution, too, although adding PL_GRAVITY per frame without scaling it with time_step onto pl.move.gravity.z is still wrong. On 15fps, time_step would be ~1,06 and on 60fps it would be ~0,26, so after one second, you get added 15*PL_GRAVITY = -5,25 for 15fps and 60*PL_GRAVITY = -21 for 60fps. That is why you have to multiply PL_GRAVITY with time_step before adding it.
|
|
|
|