Small code suggestion to btRaycastVehicle.cpp

ola
Posts: 169
Joined: Sun Jan 14, 2007 7:56 pm
Location: Norway

Small code suggestion to btRaycastVehicle.cpp

Post by ola »

Hi,

while linking graphics to the physics of the btRaycastVehicle, I ended up having wheels not turning properly, as my graphics wheel nodes rotate with an "angle" between 0 and 1 for a full rotation. I found that the wheel.m_rotation variable counts up the radians without any checks if a full rotation has been reached. I guess that could result in some bad accuracy after a while.

My suggestion is just to add something like this:

Code: Select all

      if(wheel.m_rotation > 2.0*M_PI)
         wheel.m_rotation -= 2.0*M_PI;
      else if(wheel.m_rotation < 0.0)
         wheel.m_rotation += 2.0*M_PI;
in

Code: Select all

void btRaycastVehicle::updateVehicle( btScalar step )
at line 356 or so, it'd look something like this:

Code: Select all

	for (i=0;i<m_wheelInfo.size();i++)
	{
		btWheelInfo& wheel = m_wheelInfo[i];
		btVector3 relpos = wheel.m_raycastInfo.m_hardPointWS - getRigidBody()->getCenterOfMassPosition();
		btVector3 vel = getRigidBody()->getVelocityInLocalPoint( relpos );

		if (wheel.m_raycastInfo.m_isInContact)
		{
			const btTransform&	chassisWorldTransform = getChassisWorldTransform();

			btVector3 fwd (
				chassisWorldTransform.getBasis()[0][m_indexForwardAxis],
				chassisWorldTransform.getBasis()[1][m_indexForwardAxis],
				chassisWorldTransform.getBasis()[2][m_indexForwardAxis]);
			
         btScalar proj = fwd.dot(wheel.m_raycastInfo.m_contactNormalWS);
			
         fwd -= wheel.m_raycastInfo.m_contactNormalWS * proj;

			btScalar proj2 = fwd.dot(vel);
			
			wheel.m_deltaRotation = (proj2 * step) / (wheel.m_wheelsRadius);
			wheel.m_rotation += wheel.m_deltaRotation;

		} else
		{
			wheel.m_rotation += wheel.m_deltaRotation;
		}
		
      
      
		wheel.m_deltaRotation *= btScalar(0.99);//damping of rotation when not in contact

      if(wheel.m_rotation > 2.0*M_PI)
         wheel.m_rotation -= 2.0*M_PI;
      else if(wheel.m_rotation < 0.0)
         wheel.m_rotation += 2.0*M_PI;

	}
At least that worked well for me.

I'll take a look at the brakes now... :-)

Best regards,
Ola