- Home /
Coordinates relative to parent (root)
Hey guys, I need the coordinates from child "TCP" relative to its parent "Kuka_iiwa" (root). At the Moment I am creating an object and taking the coordinates from TCP (Tool Center Point):
Trajektory.Add(new Trajektory(TCP.position.x, TCP.position.y, TCP.position.z, TCP.rotation.x, TCP.rotation.y, TCP.rotation.z,));
Afterwards I create an .txt-File with all coordinates:
public void CreateTextFile3()
{
string path = Application.dataPath + "/VRTeachIn_04.txt";
if (File.Exists(path))
{
File.Delete(path);
}
for (int i = 0; i < Trajektory.Count; i++)
{
if (Trajektory[i].Index == 0 && i == 0)
{
content = "setPositionXYZABC_" + (TransformList[i].localPosition.y * 1000) + "_" + (TransformList[i].localPosition.x * (-1000)) + "_" + (TransformList[i].localPosition.z * (-1000)) + "_lin \n";
}
//Text in das File einfügen
File.AppendAllText(path, content);
}
}
It should looks like this:
Problem is that I don´t have the right values... I think that´s because of child and parent connection. For a better understanding there are two pictures.
Does anybody has an idea how I get the right values?
Kind regards, Stefan
Answer by Bunny83 · Jan 14, 2020 at 02:25 PM
I think you have multiple issues here. First of all you just grab the world space position and rotation of the TCP object. So it's relative to the world origin and the world space orientation. If you want to get its position in the local space of the "basis frame" you have to use the transform component of that basis object and use its InverseTransformPoint method to convert from world to local space.
The next issue is that Unity is using a left handed coordinate system. The arrows you've drawn in your first image represent a right handed system. So you need to decide how you want / need to convert between the two coordinate systems if you need it in a right handed system.
Finally I you most likely did not realise that "TCP.rotation" does not represent euler angles. It's the rotation of the object as quaternion. You just grab the x,y and z component of the quaternion which doesn't make much sense since a quaternion is a 4 dimensional complex number so you're missing the "w" component. It's not clear what you need this data for. However if you wanted to store euler angles you have to use TCP.eulerAngles instead. Note that the eulerAngles are calculated from the quaternion representation and any sort of interpolation might not give you the desired results since eulerangles do not have a unique representation for the same orientation.
Finally keep in mind that Unity's horizontal plane is the X-Z plane and Y is pointing up. From the camera's perspective X goes right, Y goes up and Z goes into the scene.
I just noticed you used "localPosition" in your later code. $$anonymous$$eep in $$anonymous$$d that localPosition is the position relative to the direct parent object. So it's the position within the coordinate system of the direct parent.
Hello @Bunny83 ,
Thank´s for your answer! Regarding the coordinate system problem: as you can see I change the direction of z and also have a different order when write it to .txt-File. But Thanks for the hint.
Regarding "TCP.rotation": after I read your answer for the first time I changed it and also added the calculation for changing the coordinate system:
double X = System.$$anonymous$$ath.Round(TCP.position.x * (-1000), 2);
double Y = System.$$anonymous$$ath.Round(TCP.position.y * 1000, 2);
double Z = System.$$anonymous$$ath.Round(TCP.position.z * (-1000), 2);
double A = newObject.transform.eulerAngles.x;
A = System.$$anonymous$$ath.Round(A, 2);
double B = newObject.transform.eulerAngles.y;
B = System.$$anonymous$$ath.Round(B, 2);
double C = newObject.transform.eulerAngles.z;
C = System.$$anonymous$$ath.Round(C, 2);
Finally your first/last point: "you have to use the transform component of that basis object and use its InverseTransformPoint method to convert from world to local space" Could you please make an example of that?
I am very thankful for your help, Thanks!
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Getting variable data from Parent 1 Answer
How do I find a child object in a hierarchy of children? 1 Answer
Removing specific child objects 1 Answer