Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by makaka · Dec 13, 2011 at 10:07 PM · rotationpositionmathhierarchyrotations

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 14, 2011 at 12:15 AM 0
Share

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.

avatar image makaka · Dec 14, 2011 at 12:44 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 14, 2011 at 02:56 AM 0
Share

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 ;)

avatar image makaka · Dec 14, 2011 at 11:00 AM 0
Share

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.

avatar image makaka · Dec 14, 2011 at 11:01 AM 0
Share

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.

avatar image rolandha · Mar 07, 2012 at 12:22 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges