Distance constraint

Please don't post Bullet support questions here, use the above forums instead.
Morfo
Posts: 5
Joined: Thu Mar 18, 2010 4:09 pm

Distance constraint

Post by Morfo »

Hi. Is it possible to have a distance constraint in 3D with bullet without having to code a new constraint? Thanks very much. Any help would be appreciated.
User avatar
rponomarev
Posts: 56
Joined: Sat Mar 08, 2008 12:37 am

Re: Distance constraint

Post by rponomarev »

Hello,

It seems that you could use btPoint2PointConstraint.
For example, If pivotInA is located in the center of mass of bodyA (i.e. it is (0, 0, 0)) and pivotInB is located in the center of mass of bodyB (i.e. it is (0, 0, 0)) , then this joint will keep the distance between pivotInA and pivotInB that was at the constraint creation time.

Note, that pivotInA and pivotInB are in local space, not in world space

Hope this will help,
Roman
bone
Posts: 231
Joined: Tue Feb 20, 2007 4:56 pm

Re: Distance constraint

Post by bone »

Are you sure about that? From the header, it looks like btPoint2PointConstraint is a ball joint, which is a 3-DOF constraint. A distance constraint is a 1-DOF constraint (the only constraint is that two points have to stay some distance X away from each other).

It's easy to confuse them with the ambiguous name 'Point2Point', but ball joints and distance constraints are completely different in behavior.
User avatar
rponomarev
Posts: 56
Joined: Sat Mar 08, 2008 12:37 am

Re: Distance constraint

Post by rponomarev »

Hello,

You are right, btPoint2PointConstraint is a ball joint
So there is no distance constraint in Bullet for now.
Fortunately it is easy to implement, like this:

Code: Select all

class btDistanceConstraint : public btPoint2PointConstraint
{
protected:
	btScalar	m_distance;
public:
	btDistanceConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB, btScalar dist) 
		: btPoint2PointConstraint(rbA, rbB, pivotInA, pivotInB)
	{
		m_distance = dist;
	}
	virtual void getInfo1 (btConstraintInfo1* info)
	{
		info->m_numConstraintRows = 1;
		info->nub = 5;
	}
	virtual void getInfo2 (btConstraintInfo2* info)
	{
		btVector3 relA = m_rbA.getCenterOfMassTransform().getBasis() * getPivotInA();
		btVector3 relB = m_rbB.getCenterOfMassTransform().getBasis() * getPivotInB();
		btVector3 posA = m_rbA.getCenterOfMassTransform().getOrigin() + relA;
		btVector3 posB = m_rbB.getCenterOfMassTransform().getOrigin() + relB;
		btVector3 del = posB - posA;
		btScalar currDist = btSqrt(del.dot(del));
		btVector3 ortho = del / currDist;
		info->m_J1linearAxis[0] = ortho[0];
		info->m_J1linearAxis[1] = ortho[1];
		info->m_J1linearAxis[2] = ortho[2];
		btVector3 p, q;
		p = relA.cross(ortho);
		q = relB.cross(ortho);
		info->m_J1angularAxis[0] = p[0];
		info->m_J1angularAxis[1] = p[1];
		info->m_J1angularAxis[2] = p[2];
		info->m_J2angularAxis[0] = -q[0];
		info->m_J2angularAxis[1] = -q[1];
		info->m_J2angularAxis[2] = -q[2];
		btScalar rhs = (currDist - m_distance) * info->fps * info->erp;
		info->m_constraintError[0] = rhs;
		info->cfm[0] = btScalar(0.f);
		info->m_lowerLimit[0] = -SIMD_INFINITY;
		info->m_upperLimit[0] = SIMD_INFINITY;
	}
};
A quick test that could be placed to ConstraintDemo along with setting :

Code: Select all

#define ENABLE_ALL_DEMOS 0
...
{
	mass = 1.f;
	btTransform trans;
	trans.setIdentity();
	trans.setOrigin(btVector3(0,20,0));
	btRigidBody* body0 = localCreateRigidBody( mass,trans,shape);
	trans.setOrigin(btVector3(20,20,0));
	btRigidBody* body1 = localCreateRigidBody( mass,trans,shape);
	btVector3 pivotInA(5,0,0);
	btVector3 pivotInB(-5, 0, 0);
	btDistanceConstraint* pdc = new btDistanceConstraint(*body0,*body1,pivotInA,pivotInB, 10);
	m_dynamicsWorld->addConstraint(pdc);
	pdc->setDbgDrawSize(btScalar(5.f));
}
We will think about adding constraint like this to the library in future.

Thanks,
Roman
Morfo
Posts: 5
Joined: Thu Mar 18, 2010 4:09 pm

Re: Distance constraint

Post by Morfo »

Thanks very much for that code. I've checked it, but the points on the rope were vibrating too much. At the end, we are using the soft body ropes, because they behave more properly even when we apply a big force to one of the rigid body objects attached to the rope.