Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 1,089 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
time_step and framerate independent code #422620
05/13/13 19:20
05/13/13 19:20
Joined: Nov 2011
Posts: 274
de
lemming Offline OP
Member
lemming  Offline 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.

Code:
#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
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

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:

Code:
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:

Code:
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] #422630
05/13/13 21:21
05/13/13 21:21
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Try to remove the 'time_step' after PL_JUMPACC.


Greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: time_step and framerate independent code [Re: 3run] #422674
05/14/13 17:00
05/14/13 17:00
Joined: Nov 2011
Posts: 274
de
lemming Offline OP
Member
lemming  Offline OP
Member

Joined: Nov 2011
Posts: 274
de
Thanks you two!

I scale now the movement vectors before using them and removed the time_step multiplication for PL_MAXGRAVITY and PL_JUMPACC. It seems to work now. But I still don't understand why I had to keep it for PL_GRAVITY.

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
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
Originally Posted By: lemming
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.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1