Division by zero in btConeTwistConstraint::calcAngleInfo2

Post Reply
Aardwolf
Posts: 13
Joined: Thu Jul 07, 2011 2:57 am

Division by zero in btConeTwistConstraint::calcAngleInfo2

Post by Aardwolf »

When the current orientation matches the target orientation, trDeltaAB is the identity transform, qDeltaAB is the identity quaternion, and m_swingAxis is a zero vector. Normalizing it causes my game universe to be devoured by a black hole of NaN.

This code snippet is lines 630-649 of btConeTwistConstraint.cpp (version 2.76). I suspect it could be fixed by moving m_swingAxis.normalize(); inside the if { } below it, but m_swingAxis is used in a lot of places and I don't know if that change would actually work.

Code: Select all

if (m_bMotorEnabled && (!m_useSolveConstraintObsolete))
{	// it is assumed that setMotorTarget() was alredy called 
	// and motor target m_qTarget is within constraint limits
	// TODO : split rotation to pure swing and pure twist
	// compute desired transforms in world
	btTransform trPose(m_qTarget);
	btTransform trA = transA * m_rbAFrame;
	btTransform trB = transB * m_rbBFrame;
	btTransform trDeltaAB = trB * trPose * trA.inverse();
	btQuaternion qDeltaAB = trDeltaAB.getRotation();
	btVector3 swingAxis = 	btVector3(qDeltaAB.x(), qDeltaAB.y(), qDeltaAB.z());
	m_swingAxis = swingAxis;
	m_swingAxis.normalize();
	m_swingCorrection = qDeltaAB.getAngle();
	if(!btFuzzyZero(m_swingCorrection))
	{
		m_solveSwingLimit = true;
	}
	return;
}
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Division by zero in btConeTwistConstraint::calcAngleInfo

Post by Erwin Coumans »

Let me first double check this with Roman, he is more familiar with the implementation.

Thanks,
Erwin
User avatar
rponomarev
Posts: 56
Joined: Sat Mar 08, 2008 12:37 am

Re: Division by zero in btConeTwistConstraint::calcAngleInfo

Post by rponomarev »

Hello,

Looks like there is no need to solve the swing limit in this case.
So I added an additional check to btConeTwistConstraint::calcAngleInfo2()
I tested it, it seems OK

Here is the change (marked with //------------- )

Code: Select all

btVector3 swingAxis = 	btVector3(qDeltaAB.x(), qDeltaAB.y(), qDeltaAB.z());
//-------------------------------------
float swingAxisLen2 = swingAxis.length2();
if(btFuzzyZero(swingAxisLen2))
{
	return;
}
//-------------------------------------
m_swingAxis = swingAxis;
Erwin, could you add it to the next release?

Thanks,
Roman
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Division by zero in btConeTwistConstraint::calcAngleInfo

Post by Erwin Coumans »

Great, I committed your fix Roman, http://code.google.com/p/bullet/source/detail?r=2486

Can you try if that works for you, Aardwolf?

Thanks,
Erwin
Post Reply