Frame rate independent movement doesn't work?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Frame rate independent movement doesn't work?

Post 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
Elfinitiy
Posts: 21
Joined: Thu May 26, 2011 10:23 am

Re: Frame rate independent movement doesn't work?

Post by Elfinitiy »

Didn't your compiler give an error because you assign values to a "const" at run-time ?
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Re: Frame rate independent movement doesn't work?

Post by diho »

No, it builds it just perfect. no errors
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Frame rate independent movement doesn't work?

Post 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.
Working on game: Marrbles (Currently stopped).
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Frame rate independent movement doesn't work?

Post by hendu »

It looks pretty clear to me, you have no cap for the height. Thus the slower comp can jump to 50 meters.
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Re: Frame rate independent movement doesn't work?

Post 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. :?
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Frame rate independent movement doesn't work?

Post 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.
Working on game: Marrbles (Currently stopped).
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Re: Frame rate independent movement doesn't work?

Post by diho »

@serengeor
thanks, no error this time. But just like u said, the problem isn't solved with this. :(
g0bl1n
Posts: 63
Joined: Thu Feb 21, 2008 8:45 am

Re: Frame rate independent movement doesn't work?

Post 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
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Frame rate independent movement doesn't work?

Post 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.
Working on game: Marrbles (Currently stopped).
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Re: Frame rate independent movement doesn't work?

Post 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
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Frame rate independent movement doesn't work?

Post by serengeor »

How in the world is gravity related to your camera's position??
Working on game: Marrbles (Currently stopped).
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Re: Frame rate independent movement doesn't work?

Post 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.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Frame rate independent movement doesn't work?

Post by serengeor »

I don't get it :|
Is your camera atached to any rigid body?
Working on game: Marrbles (Currently stopped).
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Re: Frame rate independent movement doesn't work?

Post 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 :?
Post Reply