Rotating two bodies connected via a constraint

Post Reply
esak
Posts: 4
Joined: Sat Jan 28, 2017 12:30 pm

Rotating two bodies connected via a constraint

Post by esak »

I'm having some problem with rotating two bodies, connected via a constraint.
My scenario is as follows:

I have body A and diagonally up to the left on top of it I place body B, through a hinge-constraint.
Now I want to rotate both round the Y-axis:
If I rotate A I want B to rotate along with it, in it's position relative to A.
If I rotate B I want it to rotate round it's own position, without affecting A.

The problems I got when rotating A are:
It doesn't rotate around it's own position, instead it rotates around the constraint's position (where B is located).
Body B doesn't follow along A's rotation, instead it sit's still in it's original position.
When I rotate B it works as expected, it rotates around it's own position (without affecting A).

I would appreciate any tips on this matter.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Rotating two bodies connected via a constraint

Post by benelot »

esak wrote: If I rotate B I want it to rotate round it's own position, without affecting A.
This could only work if your hinge center goes through the center of mass of B.

here some layout for your code:
// let us say A and B are spheres which both radius 0.5
// position B at (0,0,0)
// position A at (-1,0,0)
btHingeConstraint(btRigidBody& rbA,btRigidBody& rbB,
(1,0,0), // pivotInA, has to be >= than the maximum radius of B + maximum radius of A
(0,0,0), // pivotInB, point directly in the center of mass of B
(0,1,0), // axisInA,
(0,1,0), // axisInB,
false //bool useReferenceFrameA = false);

Now A and B are nearly touching, if you rotate B (applyTorque), it rotates around its axis right in the center of mass, but only if you rotate it around its Y axis. If you rotate A around its center of mass, B has to move around it in the X-Z plane.

Does that help?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Rotating two bodies connected via a constraint

Post by drleviathan »

I think what you want can be done using the btPoint2PointConstraint with the body-local pivot points specified correctly. Below is a code snippet showing how I would expect it to work, but I did not test it.

Code: Select all

// pivotInB is assumed to be known
// in this example we assume it is at the center of mass of B
btVector3 pivotInB(0.0, 0.0, 0.0); 

// compute pivotInA
btTransform bToPivot;
bToPivot.setIdentity(); 
bToPivot.setOrigin(-pivotInB); // need to do this for the general case
btTransform worldToPivot = bodyB->getWorldTransform() * bToPivot;
btTransform aToPivot = bodyA->getWorldTransform().inverse() * pivotInWorld;
btVector3 pivotInA = aToPivot.getOrigin();

// create the constraint
btPoint2PointConstraint c = new btPoint2PointConstraint(bodyA, bodyB, pivotInA, pivotInB);
Edit: to fix obvious errors in code snippet.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Rotating two bodies connected via a constraint

Post by benelot »

That sounds like what I was describing and I thought that he might want to keep the hinge joint for a reason. The point to point gives the body A more degrees of freedom than just the motion within a plane. But the more solutions the better.
esak
Posts: 4
Joined: Sat Jan 28, 2017 12:30 pm

Re: Rotating two bodies connected via a constraint

Post by esak »

Thanks for your replies!

Regarding the hinge-constraint, I can now rotate body A and body B rotates along with it.
But when I rotate body B, both body A and B rotates (exactly as when rotating body A).
I tried setting the limit on the constraint to -180 and 180 degrees.
When doing this I can rotate body B without affecting body A.
But when I then rotate body A, body B is rotating round it's own local position (it still rotates along body A's rotation)!?
Am I missing something here?

Regarding the point-constraint, I can rotate body B (without affecting body A).
But when I rotate body A, body B is not rotating along with body A. It's absolutely still!?
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Rotating two bodies connected via a constraint

Post by benelot »

Do you have a simple example of what is not working? Usually code segments are easier to debug than descriptions.
Post Reply