- Home /
Calculate differential Euler angles from two Transform.Forward/Up/Right?
I have been having a really bad time trying to find a way to torque a rigidbody around to a target rotation based on its current rotation; the Euler angles are flipping randomly because they're being extracted from a quat.
Anyway, it occurred to me that I may be able to find the difference in rotation between the two objects (the rigidbody and the target bone) using the directional vectors stored in Transform
. In my mind this should be theoretically possible.
What I want to do, specifically, is take the Transform.Forward/Up/Right
vectors of one object and compare the angles between the points to the Transform.Forward/Up/Right
vectors of the other object.
I'm not sure how this would be achieved, but I'm sure it must be possible. I'm no maths guru by any standard, ha, and I'm struggling to visualise how it would work. If it turns out this is not possible for some reason...well, I may just have to cry a little as this may be my last hope for being able to complete my game in Unity... :'(
Any help would be much appreciated, and if my question is worded badly or missing information, please don't hesitate to tell me :)
One last thing: I can't use Quaternions during any stage of this process, because if I do, I have no way of getting Euler angles out again without them being mucked up.
ADDITIONAL INFORMATION
Ok, so I've worked out how it would work (for all of the following, assume 0,0,0
is the origin of the angles):
- To calculate the rotation of the X axis, you calculate the angle between either the Y and Z values of T1.Forward
or those of T1.Up
, and the Y and Z values of T2.Forward
or T2.Up
.
- To calculate the rotation of the Y axis, you calculate the angle between either the X and Z values of T1.Forward
or those of T1.Right
, and the X and Z values of T2.Forward
or T2.Right
.
- To calculate the rotation of the Z axis, you calculate the angle between either the X and Y values of T1.Up
or those of T1.Right
, and the Y and X values of T2.Up
or T2.Right
.
I THINK those are correct, but I really don't know, and it just occurred to me that I don't know what would happen if any of those coordinates ended up 0,0
... Now the only thing I need to know (aside from what to do about the coords being 0,0
) is how to actually calculate angles between to points with 0,0,0
as the origin.
Answer by Clonkex · Jun 15, 2014 at 01:13 AM
After all this time, some awesome guy from Stack Overflow solved the very base problem for me :D I posted an answer, because he worked it out entirely in the comments:
Answer by robertbu · Apr 30, 2014 at 04:38 PM
This might help you. The code is only lightly tested, but for a question about a HUD for play, I wrote some code to get Heading (yaw), pitch, and roll. You might get what you want by calculating these values for both directions and then subtracting them. I don't have time today to test/work out if these values would work for angularVelocity.
http://answers.unity3d.com/questions/696271/how-create-modern-aircrafts-hud.html
@robertbu Actually that sounds like it will be perfect! It gives me the base code to work from, and for any special cases that I find, if any, I can add extra code to handle them. Thankyou very much! If you change your comment to an answer I'll accept it :)
@robertbu Question: Why does the heading get calculated with Atan2
and the other two with ProjectPointOnPlane
and SignedAngle
? Is the heading being calculated as a global angle and the other two as local angles or something? I only need to calculate global angles, so can you give me some tips to get started?
All the angles are world angles. Put this code on a block and play with the rotation in the Inspector. I'm not sure this code will give you want you want, but the angles are world.
I was designing this code for a aircraft. So I picked a point of reference...a horizontal plane. So all the calculations are done with respect to a horizontal plane. So looking down at that aircraft's forward vector, if I eli$$anonymous$$ate the 'y' of the forward vector, I get the heading like the hand of a clock, on the horizontal plane. Using Atan2() I can get an angle relative to Vector3.right. So for a HUD, I have code that can calculate compass settings.
For the other two readings, I'm looking at the angle between forward/right vectors and that same vector projected on that plane. So for those I have to project the point and then get a signed angle. In thinking about it, I did not really need the ProjectPointOnPlane() code. I could simply eli$$anonymous$$ated the 'y' component to project these vectors on a horizontal plane.
Your answer
Follow this Question
Related Questions
Object not rotating when the associated trigger is triggered 1 Answer
transform.right to vector3 1 Answer
Imported model is rotated 90 degrees off of straight, can't use any transforms 1 Answer
How to make Y-Axis face away from a position? 2 Answers
Rotate an object around another object at an angle from the X axis? 1 Answer