Page 1 of 1
How to convert from world coordinate
Posted: Wed Feb 20, 2013 6:18 am
by henokyen
I have some objects in the physics simulation and i want to know the local coordinates of these objects ?
How can i do that ?
Re: How to convert from world coordinate
Posted: Thu Feb 21, 2013 1:39 am
by c0der
Converting from world to local just involves multiplying the world transform matrix of the object by the inverse to get the identity matrix.
Normally you want to transform a different object B into the local frame of object A, which just involves multiplying B's world transform matrix by A's inverse world transform matrix, meaning that A will now be at the origin and B's position will be relative to this origin
Re: How to convert from world coordinate
Posted: Thu Feb 21, 2013 4:20 am
by henokyen
Thanks ,
basically i want to create a joint between two legs for my robot.
and i am using the hinge constraint. and this class requires the local coordinates of the legs to be joined.
I do not know who to get the local coordinated now.
can just provide a piece of codes that do this ?
thank
you
Re: How to convert from world coordinate
Posted: Thu Feb 21, 2013 4:54 am
by c0der
For my hinge joint, I have one anchor given in world coords and a world hinge axis.
From the world anchor, you build two local anchors as follows for each body
Code: Select all
wt1T.transposeOf(m_pRigidBody1->m_matWorldTransform);
wt2T.transposeOf(m_pRigidBody2->m_matWorldTransform);
m_vLocalAnchor1 = (vAnchor - m_pRigidBody1->m_vPosition)*wt1T;
m_vLocalAnchor2 = (vAnchor - m_pRigidBody2->m_vPosition)*wt2T;
Basically these are vectors ra and rb (of the relative position) in local coords of each body. Perhaps you can paste some code or show your approach
Re: How to convert from world coordinate
Posted: Wed Oct 02, 2013 10:22 am
by shogun22
I am sorry for reviving this thread but I am completely lost and already wasted countless hours on this simple function.
So I know I have to provide the HingeConstraint constructor with local coordinates for axis and anchor point. So I wanted to create two little helper functions to translate a vector from world to local and to translate a point from world to local (since both of them are needed for the axis and the anchor).
What I came up with so far is this:
Code: Select all
btVector3 pointWorldToLocal(int bodyIndex, btVector3 point)
{
btVector3 localPoint;
btTransform xform = arrayOfBtRigBodies[bodyIndex]->getCenterOfMassTransform();
// The local coordinates should be [(worldCoords - localOrigin) * inverse(localRotation)] or am I wrong?
localPoint = (point - xform.getOrigin()) * xform.getBasis().inverse();
return localPoint;
}
btVector3 axisWorldToLocal(int bodyIndex, btVector3 axis)
{
btTransform xform = arrayOfBtRigBodies[bodyIndex]->getCenterOfMassTransform();
// This works quite well but only if I do not inverse the result of getBasis()... ?
btVector3 localAxis = axis * xform.getBasis();
return localAxis;
}
Which should be equivalent to c0der's suggestion:
wt1T.transposeOf(arrayOfBtRigBodies[bodyIndex]->m_matWorldTransform);
m_vLocalAnchor1 = (point - arrayOfBtRigBodies[bodyIndex]->m_vPosition)*wt1T;
return m_vLocalAnchor1
But when I run it, the axis is translated from world to local correctly while pointWorldToLocal produces strange results.
I basically wasted two complete days of my life on this so please release me from my pain

Re: How to convert from world coordinate
Posted: Thu Oct 17, 2013 9:00 am
by shogun22
Alright folks,
after I froze my project for a week I finally found the solution for my coordinate translation problem today. Might be trivial for most of you but for those who are stuck like I was, this is my final function:
Code: Select all
btVector3 RagdollDemo::pointWorldToLocal(int bodyIndex, btVector3 worldPoint)
{
return body[bodyIndex]->getCenterOfMassTransform().inverse()(worldPoint);
}
yes guys it's that easy. Don't know why it took me so long to understand how getCenterOfMassTransform can be used to translate points. Maybe it's not in the manual or I completely overlooked it. Hope this helps someone else.
Best
Re: How to convert from world coordinate
Posted: Thu Dec 26, 2013 8:20 am
by benfocus55
Shogun! Thank you! Saved me countless hours and actually clarified these concepts.