Execute direct kinematics with btHingeConstraint

Post Reply
yakir
Posts: 4
Joined: Fri Jul 05, 2019 6:27 am

Execute direct kinematics with btHingeConstraint

Post by yakir » Fri Jul 05, 2019 6:44 am

Hello Experts,
I am having trouble simulate direct kinematics with the hinge constraints with my data.
I have two cubes which their center of mass is (50,50,50) and (50,50,150) the size of each edge is 100 .
In addition I want to define a hinge constraints on (0,0,100) with the Y axis.
All the coordinates are in the WCS.
When I use the bullet I am creating two rigid bodies with the center of the cubes and a hinge constraint .
The rigid body that represent the (50,50,50) cube is static.
Then I define an angular motor on the hinge so it will move 45 degrees.
My problem is that when I step simulation the object seems to move but the point of the hinge is incorrect and it moves instead of only rotates.
I have tried setting the angular only and it did not move at all and as well I have tried to use the hinge with the offsetframes as false and it didnt work.
As well using a btEmptyshape in the rigid body.
I cannot use the MultiBody capabilities.
Also I have tried to use as I saw in the form the btSliderJoint with an angular but it didnt work.
When I am creating the btHingeConstraint it ask for 2 dots and 2 vectors or a transform. I think that the problem is that I am not supplying the correct information .
I have tried to look on code examples and the documentation and with no luck .
My question is what should I supply in those parameters ? is it relative to the center of mass ? how do I calculate it?
Thanks in advance for the help.

User avatar
drleviathan
Posts: 545
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Execute direct kinematics with btHingeConstraint

Post by drleviathan » Sun Jul 07, 2019 3:52 pm

It would be helpful if you were to supply source code. It is hard to make sense of what you are doing wrong from your description, however I will hazard some advice:

From your description it sounds like you are using world-frame coordinates. However the transforms in the btHingeConstriant constructors are expected to be in the local-frames of the bodies involved, because those values remain invariant during the hinge simulation.

In other words: if you want rigidBodyB to only spin and not translate then you would want the rbBFrame argument to have a translation component of zero: <x,y,z> = <0, 0, 0>. That would put the hinge pivot at rigidBodyB's center of mass. Alternatively, if you are using the pivotInB + axisInB format then the pivotInB should be the zero vector.

yakir
Posts: 4
Joined: Fri Jul 05, 2019 6:27 am

Re: Execute direct kinematics with btHingeConstraint

Post by yakir » Tue Jul 09, 2019 3:56 am

Thanks for the answer and the explanation.
I have added the code in the attachment.
I did a little change and it seems now when I am applying force it is rotating correctly.
I have multiply by 2 the last coordinate of the point so instead (0,0,100) it is (0,0,200).
The only problem I see now that is when I do not apply any force the rigidgroupB in the location (50,50,150).
Changes his center of mass to (50,50,50).
It is caused by the hinge constraint not sure why.
Thanks in advance for the help.
Attachments
codeBulletHinge.txt
(1.9 KiB) Downloaded 12 times

User avatar
drleviathan
Posts: 545
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Execute direct kinematics with btHingeConstraint

Post by drleviathan » Tue Jul 09, 2019 4:51 am

After a bit of cleanup for readability your old code looks like this:

Code: Select all

btVector3 pivotAnchor(0,0,200);
btVector3 pivotSpinner(0,0,200);
btVector3 axisAnchor(0,1,0);
btVector3 axisSpinner(0,1,0);
btHingeConstraint joint = new btHingeConstraint(anchor, spinner pivotAnchor, pivotSpinner, axisAnchor, axisSpinner);
The pivots and axes are are supposed to be in the local-frame of their respective objects. I dunno what frame you'r using. Maybe world-frame? but

If the following are true:

(1) anchor is at <50,50,50>
(2) spinner is at <50,50,150>
(3) initial world-frame rotations of anchor and spinner are identity
(4) hinge pivot passes through center of spinner
(5) hinge axis is along y-axis in both anchor and spinner's local-frame

Then the local-frame vectors would be:

Code: Select all

anchorPivot = positionSpinner - positionAnchor;
pivotSpinner = btVector3(0.0, 0.0, 0.0);
axisAnchor = btVector(0.0, 1.0, 0.0);
axisSpinner = btVector(0.0, 1.0, 0.0);
However, if you want to specify the hinge pivot and axis in the world-frame then you could use a general solution which works no matter the transforms of the bodies:

Code: Select all

// assuming the world-frame hinge pivot and axis are known...
btVector3 pivotWorld = btVector3(50.0, 50.0, 150.0); // or whatever
btVector3 axisWorld = btVector3(0.0, 1.0, 0.0); // or whatever

// get transforms of objects
btTransform transAnchor = anchor->getWorldTransform();
btTransform transSpinor = spinner->getWorldTransform();

// compute the inverse transforms
btTransform transAnchorInv = transAnchor.inverse;
btTransform transSpinnerInv = transSpinor.inverse;

// compute pivot in local-frames
btVector3 pivotAnchor = transAnchorInv * pivotWorld;
btVector3 pivotSpinner = transSpinnerInv * pivotWorld;

// compute axis in local-frames
// (axis only gets the rotation part, so we zero translation)
transAnchorInv.setOrigin(btVector3(0.0, 0.0, 0.0));
transSpinnerInv.setOrigin(btVector3(0.0, 0.0, 0.0));
btVector3 axisAnchor = transAnchorInv * axisWorld;
btVector3 axisSpinner = transSpinnerInv * axisWorld;

// create the hinge
btHingeConstraint joint = new btHingeConstraint(anchor, spinner, pivotAnchor, pivotSpinner, axisAnchor, axisSpinner);

yakir
Posts: 4
Joined: Fri Jul 05, 2019 6:27 am

Re: Execute direct kinematics with btHingeConstraint

Post by yakir » Tue Jul 09, 2019 7:36 am

Thanks for the explanation.
The code :

Code: Select all

anchorPivot = positionSpinner - positionAnchor;
pivotSpinner = btVector3(0.0, 0.0, 0.0);
axisAnchor = btVector(0.0, 1.0, 0.0);
axisSpinner = btVector(0.0, 1.0, 0.0);
Means that the spinner is rotating around its center of mass.
In my case the rotation is around another point (0,0,100) .
All the coordinates I give are world coordinate systems sorry for not clarify it before.
I have tried to switch between the pivots but it did not work. I am getting that the body is moving although no gravity and no motor is activating .
Thanks in advance for the help

Post Reply