Spring between two dynamic objects

Post Reply
random
Posts: 5
Joined: Fri Jan 25, 2019 12:28 pm

Spring between two dynamic objects

Post by random »

Greetings,

I'm trying to create a spring using either btGeneric6DofSpring2Constraint or btGeneric6DofSpringConstraint and I'm failing miserably.
In all the examples I could find one body is always static, and that seems to work fine. As soon as I make both bodies dynamic the whole thing just spins like crazy and shoots off to outer space.

In fact I couldn't even get btGeneric6DofConstraint to act like a slider. Eg, this works as expected:

Code: Select all

	auto spring = new btSliderConstraint(*disk1, *disk2, t1, t2,true);
	spring->setLowerLinLimit(3);
	spring->setUpperLinLimit(4);
while this doesn't:

Code: Select all

	auto spring = new btGeneric6DofConstraint(*disk1, *disk2, t1, t2, true);
	spring->setLimit(0, 3, 4);
	spring->setLimit(1, 0, 0);
	spring->setLimit(2, 0, 0);
	spring->setLimit(3, 0, 0);
	spring->setLimit(4, 0, 0);
	spring->setLimit(5, 0, 0);
Interestingly, rotation works fine, it is only linear motion that causes problems.
I must be missing something obvious.Can someone point me to a working example?

Another question is how do I set the parameters? What are the units?
For example I would like to have two disks (say cylinders R=0.5, h = 0.2, mass 1) constrained to move slide in the direction of cylinder axis within the range (say say 2 to 4 between CM) and with the spring between them with equilibrium point at max range and parameters set so that when the bottom disk is laying on the ground the top one sags by 1 under the force of gravity and oscillates with a freq of say 2 Hz and the damping set so that it stops oscillating after 3..5 periods.

Thanks,
R
random
Posts: 5
Joined: Fri Jan 25, 2019 12:28 pm

Re: Spring between two dynamic objects

Post by random »

UPDATE:

Apparently it was not the spring that was causing the issue. It was the setting of linear limits on 6DOF constraint.
It only happens with some combinations of initial position / pivot point offset / linear range.
Once I changed them it was quite difficult to reproduce the problem again. Anyway the code below shows the problem.
Note how the issue does not happen with btSliderConstraint, only with 6DOF constraint.

Code: Select all

btTransform Tr(double x, double y, double z)
{
	return btTransform( btQuaternion(0,0,0,1), btVector3(x,y,z) );
}

btTransform Rot(int axis)
{
	btVector3 a(0,0,0);
	a.m_floats[axis] = 1;
	return btTransform( btQuaternion(a,M_PI/2), btVector3(0,0,0) );
}

void SpringTest::initPhysics()
{
	m_guiHelper->setUpAxis(1);
	createEmptyDynamicsWorld();

	btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
	m_collisionShapes.push_back(groundShape);
	createRigidBody(0.0f, Tr(0,-50,0), groundShape, btVector4(0, 0, 1, 1));

	auto shape1 = new btBoxShape(btVector3(0.5, 0.25,0.75));
	auto shape2 = new btBoxShape(btVector3(0.4, 0.2,0.6));
	m_collisionShapes.push_back(shape1);
	m_collisionShapes.push_back(shape2);
	auto b1 = createRigidBody(1, Tr(0, 2, 0), shape1);
	auto b2 = createRigidBody(1, Tr(0, 4, 0), shape2);
	auto t1 = Rot(2)*Tr(0,0,0);
	auto t2 = Rot(2)*Tr(2,0,0);
#if 0
	auto spring = new btSliderConstraint(*b1, *b2, t1, t2,true);
	spring->setLowerLinLimit(3);
	spring->setUpperLinLimit(4);
#else
	auto spring = new btGeneric6DofConstraint(*b1, *b2, t1, t2, true);
	spring->setOverrideNumSolverIterations(30);
	spring->setLimit(0, 3, 4);
	spring->setLimit(1, 0, 0);
	spring->setLimit(2, 0, 0);
	spring->setLimit(3, 0, 0);
	spring->setLimit(4, 0, 0);
	spring->setLimit(5, 0, 0);
#endif
	m_dynamicsWorld->addConstraint(spring, true);
	m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
}

PS: I realized what I really need is a point-to-point spring constraint (where both ends of sprint are allowed to rotate). Is there such a thing in bullet or do I have to make it myself?

Regards,
R
Ehsanizadi
Posts: 73
Joined: Mon Dec 02, 2013 4:13 pm

Re: Spring between two dynamic objects

Post by Ehsanizadi »

Hi there,

I am exactly looking for the same thing.
I did not find a point to point spring feature, however, a workaround might be to dynamically adjust the stiffness and damping of the point to point object at each time step when the orientation of the two dynamic bodies are altered.

BTW if you found or wrote a snippet for handling this, please let me know.

Thanks
E30
Post Reply