Page 1 of 2

Frame rate independent movement doesn't work?

Posted: Thu Jun 30, 2011 2:18 pm
by diho
Hi everyone,

Some time ago I started a topic here because I had a problem with the basic movement of the fps camera. F.e. when I play the game on two different computers, on the first one I will jump 1meter, and on the other one 5.
I needed to use frame rate independent movement, so I did. But still my jump height isn't the same on every computer.

I have this outside the loop:

Code: Select all

   u32 then = device->getTimer()->getTime();

   // This is the movemen speed in units per second.
   const f32 MOVEMENT_SPEED = 50.0f;  // how fast camera moves
   const f32 ROTATION_SPEED   = 50.0f;   // how fast camera rotates
and this inside the loop:

Code: Select all

const u32 now = device->getTimer()->getTime();
      const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
      then = now;
world->stepSimulation(frameDeltaTime, 120);
            world->debugDrawWorld(true);
            world->debugDrawProperties(true);

            if ( receiver.keyDown(KEY_SPACE))
            {
                camPos.Y += MOVEMENT_SPEED * frameDeltaTime;
                camera->setPosition(camPos);
            }
What am I doing wrong, or better, what have I forgot?

thanks in advance,

Diho

Re: Frame rate independent movement doesn't work?

Posted: Thu Jun 30, 2011 3:14 pm
by Elfinitiy
Didn't your compiler give an error because you assign values to a "const" at run-time ?

Re: Frame rate independent movement doesn't work?

Posted: Thu Jun 30, 2011 3:44 pm
by diho
No, it builds it just perfect. no errors

Re: Frame rate independent movement doesn't work?

Posted: Thu Jun 30, 2011 6:30 pm
by serengeor
Elfinitiy wrote:Didn't your compiler give an error because you assign values to a "const" at run-time ?
He assigns the initial value and doesn't modify it later, so I think this is perfectly legal.

Your code looks kind of ok, but why do you use another vector for cam position (camPos)?
if the cam is a fps cam this could cause troubles, when you move the cam and jump.

Re: Frame rate independent movement doesn't work?

Posted: Fri Jul 01, 2011 12:52 pm
by hendu
It looks pretty clear to me, you have no cap for the height. Thus the slower comp can jump to 50 meters.

Re: Frame rate independent movement doesn't work?

Posted: Fri Jul 01, 2011 2:30 pm
by diho
@serengeor:
because when I try to change the camera's Y directly, I get the error:

Code: Select all

error: assignment of data-member 'irr::core::vector3d<float>::Y' in read-only structure|
@hendu:
I tried to set a max height differents, by taking the start height, and compare it with that height + a var which contains the max jump height. But it became a bit messy. :?

Re: Frame rate independent movement doesn't work?

Posted: Fri Jul 01, 2011 8:27 pm
by serengeor
diho wrote:@serengeor:
because when I try to change the camera's Y directly, I get the error:

Code: Select all

error: assignment of data-member 'irr::core::vector3d<float>::Y' in read-only structure|
Hm, then you are doing it wrong.
I assume you tried it something like:

Code: Select all

cam->getAbsolutePosition().Y+=jumpspeed;
And that is wrong, because 'getAbsolutePosition' returns a 'const irr::core::vector3df' thus it is read only value.
What you can do instead is:

Code: Select all

 
cam->setPosition(cam->getAbsolutePosition()+irr::core::vector3df(0, jumpspeed,0));
 
Don't know if that will do anything about your problem.

Re: Frame rate independent movement doesn't work?

Posted: Fri Jul 01, 2011 8:39 pm
by diho
@serengeor
thanks, no error this time. But just like u said, the problem isn't solved with this. :(

Re: Frame rate independent movement doesn't work?

Posted: Fri Jul 01, 2011 11:46 pm
by g0bl1n
Why are you using a u32 for the now time? Try using an f32 and see if there is any difference, I'm pretty sure a(n?) u32 is like an int no?

Because I do this:

Code: Select all

//Initialization
f32 irrdt=0.0;
f32 irrdtOld=0.0;

Code: Select all

//Loop
irrdt=irrDevice->getTimer()->getTime()-irrdtOld;
irrdtOld+=irrdt;
And everything works, and that u32 is the only thing that's different (that I can see).

Goblin

Re: Frame rate independent movement doesn't work?

Posted: Sat Jul 02, 2011 9:44 am
by serengeor
g0bl1n wrote:Why are you using a u32 for the now time? Try using an f32 and see if there is any difference, I'm pretty sure a(n?) u32 is like an int no?

Because I do this:

Code: Select all

//Initialization
f32 irrdt=0.0;
f32 irrdtOld=0.0;

Code: Select all

//Loop
irrdt=irrDevice->getTimer()->getTime()-irrdtOld;
irrdtOld+=irrdt;
And everything works, and that u32 is the only thing that's different (that I can see).

Goblin
hm, the u32 is an unsigned integer and it his code should work since getTime returns time in miliseconds and the type is also u32.

Re: Frame rate independent movement doesn't work?

Posted: Sun Jul 03, 2011 8:31 am
by diho
Tried to change it both in f32, but there's still the same problem :(.
Could it have something to do with the gravity I set with irrbullet?

Code: Select all

world->setGravity(vector3df(0,-70,0));
 
Why -70, well cause -10 didn't seem to be effecting anything. :?

Diho

Re: Frame rate independent movement doesn't work?

Posted: Sun Jul 03, 2011 8:51 am
by serengeor
How in the world is gravity related to your camera's position??

Re: Frame rate independent movement doesn't work?

Posted: Sun Jul 03, 2011 11:33 am
by diho
It needs to be, otherwise my camera would get higher and higher. right? if I keep pressing my spacebar the camera wouldn't stop at some point. Or do I misunderstand something here?
I understand what you mean. (half xD) But what else should effect the camera when jumping if it isn't the gravity? I mean I'm not using the Skeymap for this part of the movement.

Re: Frame rate independent movement doesn't work?

Posted: Sun Jul 03, 2011 11:36 am
by serengeor
I don't get it :|
Is your camera atached to any rigid body?

Re: Frame rate independent movement doesn't work?

Posted: Sun Jul 03, 2011 12:36 pm
by diho
Yes/No, my rigidbody (charactermodel) has the camera as parent. Maybe that explains a lot xD, should have said that earlier I guess. Sorry :?