- Home /
Positions to rotations of a regular GO
Hi, how could I transfer a hierarchy defined by positions to a hierarchy defined by rotations?
In other words, I have 4 objects defined as follows:
GoA
GoB
GoC
GoD
Each with independent coordinates, no rotations.
I would like to get here using rotations so the correction propagate itself using rotations through the hierarchy.
GoA
> GoB
> > GoC
> > > GoD
Actually, it is the same thing as bones in 3D models. So, how do I calculate this?
It should be fairly simple math but I can't figure it out.
To be honest, i have absolutely no idea what you wanna do here.... Do you have your 4 GOs in a hierarchy or are they all at the root level, independently, somewhere in the scene?
Rotations doesn't move any object unless they already have a local translation. A GameObject-tree actually represents a bone structure. If you import a skinned, bone-animated model Unity will create an empty gameobject hierarchy which represents the bones. All bones have it's local offset relative to the parent bone. If you rotate the parent all childs will follow the rotation.
A sample screenshot of your hierarchy and scene view would help i guess. Your "position to rotation conversion" seems a bit strange since bone rotations usually expect fix local offsets to avoid unnatural stretching.
I have this funny feeling that you want inverse kinematics. If that's the case, forget about it...
The locomotion system(written by Rune Skovbo Johansen) contains a 2-limb I$$anonymous$$-solver which works together with the characters walk / run animation. A 4-axis I$$anonymous$$ solver is anything but simple mathematics. It's actually opposite.
hey and thanks. I don't want I$$anonymous$$. What I want is to move from 4 independent objects (I have 4 world coordinates for these) to 4 objects that are in hierarchy under each other which are in offset from their parent defined by some local position and rotation.
Imagine that I have a 3D model where all the bones are defined by world coordinates. What I need is to take a single root bone and set a local offset and rotation to its child. Then take a new bone and attach it to he previous and rotate / position accordingly. And then another and another ... until I the whole bone structure looks the same as with the world coordinates.
What exactly I need is the local angle between these bones. I guess it is extremely simple to do, I just can't get it right. I'm probably calculating the angle in a wrong way.
I hope this makes it more clear.
Answer by aldonaletto · Dec 13, 2011 at 11:40 PM
If you just want to copy "parent" rotations to the "child" objects without modifying their positions, you could add the following script to each child, then drag the parent object to the parent field in the Inspector:
var parent: Transform; private var myRot: Quaternion; private var baseRot: Quaternion;
function Start(){ baseRot = parent.rotation; myRot = transform.rotation; }
function Update(){ if (baseRot != parent.rotation){ transform.rotation = Quaternion.Inverse(baseRot) parent.rotation myRot; } } This script respects the original rotations, and just copies the changes occurred in the parent rotation. Objects can be childed to any level; the only problem is that the Update order may not respect the hierarchy, thus some children may not be rotated in the same frame as their parents - but this only creates a 1 frame lag for each hierarchy level in the worst case. If the hierarchy is A->B->C->D (D is the deepest child), for instance, and the Update order is the exact reverse (D, C, B, A), D will follow A rotation 3 frames after.
Answer by Bunny83 · Dec 14, 2011 at 02:04 AM
Yes that is very simple :D
Unity does this already for you ;)
If you have 4 GOs in the scene and they already have their desired positions / rotations in the world, just parent them together. Unity will keep the objects world position and orientation. That means .position and .rotation will stay the same but the actual .localPosition and .localRotation will be changed automatically to match the new parent.
A simple example with two object:
GoA: position(10,0,0) rotation(0,90,0) localPosition(10,0,0) localRotation(0,90,0)
GoB: position(10,0,5) rotation(0,0,0) localPosition(10,0,5) localRotation(0,0,0)
// When you make GoB a child of GoA, like this:
GoB.transform.parent = GoA.transform;
// this happens to the coordinates:
GoA: position(10,0,0) rotation(0,90,0) localPosition(10,0,0) localRotation(0,90,0)
GoB: position(10,0,5) rotation(0,0,0) localPosition(-5,0,0) localRotation(0,-90,0)
As you can see, the world position will stay the same, but the local position will change. GoB was offset by 5 on the z axis but when it's a child of GoA it has to be offset by -5 on x because GoA is rotated by 90° and the local x-axis of A points into the negative z-world-axis. Also the rotation of B has changed to revert the rotation it inherits from A.
That's all a bit messy, just try it yourself. It's the same when you parent GameObjects in the hierarchy view. The position / rotation you see in the Inspector is the localPosition / localEulerAngles
Btw, as i said if you parent them every object will keep their world rotation so they childs are only offset by it's localPosition. If you want that all bones / objects have only a local position offset on one axis, for example +z-axis (aka forward), just rotate the parent towards the child before you parent the child.
Just use LookAt on the parent with the child as target. When parenting the child, the child's localPosition will be (0,0,distanceToParent).
Note: this will of course only work when each parent only have one child. But you can't deter$$anonymous$$e a logical rotation for a parent that has two or more childs. Usually the local offset doesn't really matter. For animations it's only important that your "bones" are in the "default" rotation state (T-pose or something like that for human-chars) before you parent them together.
As long as all animations are based on this base-rotations, everything should be fine ;) But i guess you don't really use external created animations because it doesn't make sense to build the rig in Unity ;)
I understand the local -> world coordinates relation.
Let me put it another way, I'm reading a file with animations where every bone is stored by world coordinates.
I need to transfer this to a hierarchical model where every bone is defined by a constant position offset (if it wouldn't be constant the model would stretch) and a local rotation which is the one that actually animates the model. The root is somewhere in the center of the model and all bones are children of this root.
So I need code that would every frame translate the world coordinates to the relational "rotational" ones. Of course, without any dynamic changes in the hierarchy.
is there any answer to this question? i am having exactly the same problem right now. animating the position / rotation properties of a transform directly does not work as it seems.
Your answer
Follow this Question
Related Questions
Find intersection (with Picture) 2 Answers
Calcultate spawn position based on spawn volume's rotation 2 Answers
Objects not always Spawning At Correct Location 2 Answers
how to ignore transform.position.y 3 Answers
Directional booster 1 Answer