btKineticCharacterController movement framerate dependant

Post Reply
Athos06
Posts: 7
Joined: Tue Apr 15, 2014 8:39 pm

btKineticCharacterController movement framerate dependant

Post by Athos06 »

Hello,

I ve been trying to use the btKinematicController, and I have a weird problem I cant figure out by myself. The thing is, apart from all the known issues I ve read about the kinematic cc, it seems Im doing something wrong because when I try to move it its framerate dependant. Im making a game with Ogre and bullet and when I set the vsync option on the character moves much faster, what tells me it isnt framerate independant I guess.

The code I use to move it is something like this, I ve simplified it a lot since a have more stuff in there, but the base is that:

Code: Select all

//the loop
void GameState::update(double timeSinceLastFrame)
{
	//get the player input
	getInput();
	//update the Player
	Player->update(timeSinceLastFrame);
	//update bullet world (the physics manager is just a wrapper to initialize bullet and keep
	//some variables and deal with collision detections
	m_pPhysicsMgr->m_pWorld->stepSimulation(timeSinceLastFrame*0.001, 2);	//timeSincelastframe is in ms
}


void GameState::getInput()
{
	float deltaTime = m_FrameEvent.timeSinceLastFrame/1000;
	Ogre::Vector3 movementDirection = Ogre::Vector3::ZERO;

	if(OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_A))
		movementDirection = movementDirection + Ogre::Vector3::NEGATIVE_UNIT_X ;

    //... I do the same with each key that moves the character
	
	Player()->setMovementVector(movementDirection);
}


void Player::setMovementVector(Ogre::Vector3 movementDir){
	Ogre::Vector3 goalDirection = Ogre::Vector3::ZERO;
	
	//I do stuff to get the direcciont depending the camera position
	goalDirection += movementDir.z * m_pCamera->m_pCameraNode->_getDerivedOrientation().zAxis();
	goalDirection += movementDir.x * m_pCamera->m_pCameraNode->_getDerivedOrientation().xAxis();
	goalDirection.normalise(); // and then normalise that vector
	
	float characterMovSpeed = 25;
	m_MovementVector = goalDirection * characterMovSpeed; //vector3 member, keeps the movemente vector
 
	//this is a Player member that keeps a pointer to the kinematicController
	m_pCharController->setMovementVector(movementVector);
}
//called each frame
void Player::update(double timeSinceLastFrame){
	//timeSinceLastFrame is in miliseconds
	Ogre::Vector3 moveVector = m_MovementVector*(timeSinceLastFrame/1000)
	//member pointer to the kinematicCharController
	m_pCharacterC->setWalkDirection(BtOgre::Convert::toBullet(moveVector)); 
	
	//I update the character node (the mesh and so forth) according to the characterController position
	m_pCharacterNode->setPosition( (BtOgre::Convert::toOgre
		( m_ghostObject->getWorldTransform().getOrigin() ) ) );
	
}
Any idea of what Im doing wrong? I dont know if I should pass the vector multiplied by the deltaTime to setWalkDirection or that would be also a mistake. I ve tried that and then even passing just the normalised vector is too fast, I have to multiply it for 0.1 or something like that, and I dont know if that fixes the issue or not. Thats the only thing I could think so any help will be preciated.

Thanks guys, you re the best.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: btKineticCharacterController movement framerate dependan

Post by Basroil »

Looks like you're updating by position rather than velocity? If so then your moveSpeed should be 25* ratio between simulation rate and elapsed rate, which keeps your movement in line with simulation rate despite updating only with elapsed rate.

Though from the look of it it could also be an input clearing issue, where you clean out the input between frames but call the movement, meaning you walk one frame then stop the next, then walk again. Hard to be 100% sure, but it can happen. I know some older N64 games ran into similar issues, and some did funny things to overcome framerate induced slowdowns (and speedrunners love those games now because the "solutions" let you clip though walls :D )
Athos06
Posts: 7
Joined: Tue Apr 15, 2014 8:39 pm

Re: btKineticCharacterController movement framerate dependan

Post by Athos06 »

thanks for the answer, actually I found why its working like that and its supposed to be like that. setWalkingDirection increment the position per simulator step so the lower the framerate more the number of steps and the character moves more, so faster.

I found the answer here

http://www.bulletphysics.org/Bullet/php ... +collision

anyway, when I didnt multiply the MovementVector by deltaTime it worked at 60fps by default (or whatever the bullet framerate is) thats why I experienced some improvement and it was working well with vsync on because this cap the framerate to 60fps on my app too. The problem should be when I ll try the game in a slower machine running at 30 fps.. so thats not a good idea either.
The thing is even the internal jump methond in the kinematicCharacter uses setwalkingdirection...

I didnt understand what you told me about updating the position rather than velocity, you mean I should use setVelocityForTimeInterval, right? and I didnt understand what you mean by the ratio between simulation rate and elapsed rate.

Thanks for the help!
Post Reply