Pendulum in vacuum

User avatar
keym
Posts: 4
Joined: Fri Dec 03, 2010 12:02 am

Pendulum in vacuum

Post by keym »

Hello,
I'm trying to simulate ideal perpetual motion using Bullet. Something wich acts like a pendulum in vacuum with frictionless joints. I have two rigid bodies (boxes of size 1x1x1). One of them is placed in (0,0,0) with zero mass and second is in (3,0,0) and mass set to 1. What I do is simply make a revolute joint (hinge) in z axis between them and then start the simulation. The expected result should be that movable body starts to swing beneath static body and reaches values (-3,3) in x axis and (-3,0) in y axis. And here is the actual issue. At few first steps the dynamic body have some strange disruptions in motion. Actualy the starting point in x axis looks like is 2.4 not 3.0 and body reaches values >0 in y axis (why?). It also looks like there is some small motions in z axis which shouldn't be there. I believe I've switched off all damping and friction. I use fixed timestep and console interface. Can someone help me?


Here are results for 10 steps of simulation:
1: x=2.400000; y=0.000000; z=0.000000
2: x=1.920000; y=0.000000; z=0.000000
3: x=1.923805; y=0.019614; z=0.000000
4: x=1.808545; y=0.015989; z=0.002865
5: x=1.777316; y=-0.036467; z=0.007552
6: x=1.738067; y=-0.036059; z=-0.020248
7: x=1.574555; y=-0.035349; z=-0.012763
8: x=1.539199; y=-0.047324; z=-0.059271
9: x=1.687235; y=-0.031838; z=-0.054995
10: x=1.666887; y=-0.060554; z=-0.008543
Thanks
User avatar
keym
Posts: 4
Joined: Fri Dec 03, 2010 12:02 am

Re: Pendulum in vacuum

Post by keym »

I've managed to solve part of my problem. I don't have motion in z axis anymore, but the simulation still isn't like I want it to be. Moveable body swings all around fixed body and makes full circle but it should move beneath only. It looks like it has some extra velocity from the start but I don't know why. I've made some visualizations using BasicDemo. Here is code for creating both bodies and hinge joint:

Code: Select all

	//===========body1 (fixed)=============
	btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
	m_collisionShapes.push_back(colShape);

	btTransform startTransform;
	startTransform.setIdentity();

	btScalar mass(0.0);
	bool isDynamic = (mass != 0.f);	
	btVector3 localInertia(0,0,0);
	if (isDynamic) colShape->calculateLocalInertia(mass,localInertia);
	startTransform.setOrigin(btVector3(0,0,0));
	
	btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
	btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
	btRigidBody* body = new btRigidBody(rbInfo);
	m_dynamicsWorld->addRigidBody(body);
	body->setDamping(0,0);
	body->setRestitution(0);


	//==========body2 (moveable)===========
	btCollisionShape* colShape2 = new btBoxShape(btVector3(1,1,1));
	m_collisionShapes.push_back(colShape2);
	btTransform startTransform2;
	startTransform2.setIdentity();
	btScalar mass2(1.0);
	bool isDynamic2 = (mass2 != 0.f);
	btVector3 localInertia2(0,0,0);
	if (isDynamic2) colShape->calculateLocalInertia(mass2,localInertia2);
	startTransform2.setOrigin(btVector3(3,0,0));
	
	btDefaultMotionState* myMotionState2 = new btDefaultMotionState(startTransform2);
	btRigidBody::btRigidBodyConstructionInfo rbInfo2(mass2,myMotionState2,colShape2,localInertia2);
	btRigidBody* body2 = new btRigidBody(rbInfo2);
	m_dynamicsWorld->addRigidBody(body2);
	body2->setDamping(0,0);
	body2->setRestitution(0);
	body2->setFriction(0);
	body2->setSleepingThresholds(0,0);



	//=======joint========
	btVector3 axisInA(0,0,1);
	btVector3 axisInB(0,0,1);
	btVector3 pivotInA(0,0,0);
	btVector3 pivotInB(3,0,0);
	btHingeConstraint* hinge = new btHingeConstraint(*body,*body2,pivotInA,pivotInB,axisInA,axisInB,false);
	m_dynamicsWorld->addConstraint(hinge);

What am I missing?
You do not have the required permissions to view the files attached to this post.
User avatar
keym
Posts: 4
Joined: Fri Dec 03, 2010 12:02 am

Re: Pendulum in vacuum

Post by keym »

Nobody?
I've switched to 6DOF constraint to try the same thing and it behaves more like ideal pendulum (the motion is less damped which is great) but there is one small issue - starting point for body #2 is not that what I've set (3,0,0,). It changes in strange way when the engine steps into simulation loop (stepSimulation() method). While it should be x=3 (or something near to this) it accually changes to x=0.0 and in next steps engine tries to "fix" this. After 10th step starts the appropriate motion. Check out the graph below. I've printed positions for both bodies before simulation and they are correct. It starts to mess up when engine enters the simulation. I think this may be something to do with constraint but not sure. Can anyone look at this and write some suggestions?

Code for 6DOF:

Code: Select all

btGeneric6DofSpringConstraint* pGen6DOFSpring = new btGeneric6DofSpringConstraint(*body, *body2, startTransform, startTransform2, false);
dynamicsWorld->addConstraint(pGen6DOFSpring, true);
pGen6DOFSpring->setLimit(4,0,0);
pGen6DOFSpring->setLimit(3,0,0);
You may ask yourself why even bother with this - the bottom line is that i'm making a comparison of few physics engines (including Bullet, Havok and PhysX) so it's important for me to get desired results. Thank you for any help.
You do not have the required permissions to view the files attached to this post.
Dominik
Posts: 32
Joined: Fri Dec 19, 2008 2:51 pm

Re: Pendulum in vacuum

Post by Dominik »

You move your dynamic object to the position (3,0,0), so the static object is left of the dynamic one. Howeveer, you then also set the constraint's pivot to (3,0,0), which is to the right of the dynamic one. Therefore, the constraint initially tries to restore a valid position, moving the constraint to (-3,0,0), because now the pivots in both bodies' local coordinate systems match. If you set the pivot on the dynamic object to (-3,0,0), it should work.
User avatar
keym
Posts: 4
Joined: Fri Dec 03, 2010 12:02 am

Re: Pendulum in vacuum

Post by keym »

OMG. That is so true. I'm so embarresed now. Thank you Dominik!