- Home /
configurable joint targetrotation: How to compute it
Hi Everyone,
I am having problems computing the targetrotation of a configurable joint.
START EDIT You can find the application resulting from this question here
http://www.fabrejean.net/projects/excavator
END EDIT
background: I am swapping the JointDriveMode from velocity to position drive when the targetAngularVelocity magnitude is 0. So I need to compute out of the blue the targetRotation of a joint. I do not know nor can maintain a variable holding this variable since it was driven using velocity and the joint unfortunatly can not be queried for this. Also, the joint is not configured in worldspace.
for convenience and to avoid misinterpretations, I have added the sources of this specific problem online and with a webplayer version:
http://www.fabrejean.net/projects/unity/velpos.html
I am using currently:
GetComponent(ConfigurableJoint).targetRotation
= Quaternion.Euler(joint.connectedBody.transform.localEulerAngles)
This is not working when the rigidbody implementing the joint is itself constraint by another configurable joint. Basically it doesn't work with a chain of joints. it jumps to weird angles instead. While this works very well when the rigidbody holding the joint is not attached to anything. I could be wrong in this assumption, but that's what I am experiencing so far.
So:
Is there actually a way to switch JointDriveMode from velocity to position and have the joint stay in its current position? Currently if you switch it jumps back to the default values.
Is there a proper way to find out about the targetRotation accuratly in all situations?
Thanks for your help. I can provide a small example if someone is interested to get a proper working case of this.
Jean,
Answer by Jean-Fabre · Aug 12, 2010 at 08:04 AM
Ok, found the solution.
The solution that I implemented is to compute the position from the "parent" space which is the rigid body implementing the joint, then use lookRotation, the resulting quaternion is properly define in the local space.
pos = transform.InverseTransformPoint(joint.connectedBody.transform.position);
GetComponent(ConfigurableJoint).targetRotation = Quaternion.LookRotation(pos);
One could probably do that directly with rotation component, if there is a better way I am all hears.
Jean
yes, also as I worked more with this script and other rigs, computing with the position has a flow when the connected body position doesn't change, it happens with some rigging strategies. So I am currently investigating how to compute the targetRotation based on the actuyl direction not the pos and provide a public switch so that the script is as generic and work in all cases as much as possible.
Answer by mstevenson · Feb 15, 2013 at 07:03 AM
I've created a set of extension methods for computing a ConfigurableJoint's targetRotation from either a local or world rotation. The solution is not trivial, and all solutions I've found online have failed to take into account arbitrary "axis" and "secondary axis" values. With some careful quaternion math it's possible to transform a local or world space rotation into a joint space rotation while supporting variable joint axes.
Answer by Magnus Wolffelt · Aug 11, 2010 at 01:16 PM
Interesting question. The only thing that strikes me is that you shouldn't have to go via euler angles. Just set the target rotation to transform.localRotation.
Anyway, not sure if that solves the problem. Probably not.
Hi, That's how I started originally but it doesn't give the wanted result neither unfortunately.