Page 1 of 1

Division by zero in btConeTwistConstraint::calcAngleInfo2

Posted: Mon Feb 13, 2012 9:36 pm
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;
}

Re: Division by zero in btConeTwistConstraint::calcAngleInfo

Posted: Mon Feb 13, 2012 11:21 pm
by Erwin Coumans
Let me first double check this with Roman, he is more familiar with the implementation.

Thanks,
Erwin

Re: Division by zero in btConeTwistConstraint::calcAngleInfo

Posted: Thu Feb 16, 2012 10:09 pm
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

Re: Division by zero in btConeTwistConstraint::calcAngleInfo

Posted: Thu Feb 16, 2012 11:45 pm
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