- Home /
Find Opposite Quaternion
I have an object with a rotation being driven by a gyro, and I need the ability to recenter it. I haven't been able to figure out a way to reset the gyro so instead I've been trying to rotate a parent of that object to be the opposite rotation, such that if the child is rotated 90 degrees in the x, the parent will get rotated -90 degrees in the x, thus making it seem as though the child is facing the origin again. I can't quite seem to get this to math out though. Should I be doing something else?
I've tried the following methods but nothing seems to work quite the way i want it to.
parent.localEulerAngles = new Vector3(
-child.localEulerAngles.x,
-child.localEulerAngles.y,
-child.localEulerAngles.z);
parent.localEulerAngles = new Vector3(
360f-child.localEulerAngles.x,
360f-child.localEulerAngles.y,
360f-child.localEulerAngles.z);
parent.localRotation = Quaternion.Inverse(child.localRotation);
parent.localRotation= Quaternion.LerpUnclamped(child.localRotation, Quaternion.Identity, 2f);
Anyone have any thoughts on what else I should try to get this thing recentered?
Answer by TSI25 · Jun 05, 2017 at 09:02 PM
Well ultimately i got it working by deleting the child prefab and re instantiating it which is something i was hoping to avoid having to do but will work for now. using Quaternion.Inverse() as JC_SummitTech suggested above seems to work the first time but after that it gets wonky. This probably means that there is a problem deeper in the code elsewhere. I'll update this answer if I can figure out what that is.
Answer by JC_SummitTech · Jun 05, 2017 at 07:08 PM
Here is what I use
Quaternion qBase = Quaternion.identity;
//to recenter call
qBase = Quaternion.Inverse(Gyro.quat);
//updating my current quaternion
transform.localRotation = qBase * Gyro.quat;
You'll want to filter the gyro data and all but that's probably what you need to "recenter" :)
Do you mean something like this?
Quaternion qBase = Quaternion.identity;
qBase = Quaternion.Inverse(child.rotation);
parent.localRotation = qBase * child.rotation;
I can't get that working either, and i don't see anything called Gyro.quat anywhere unless you mean attitude?
I do not understand why you change your parent rotation based on your child rotation. Gyro.quat is just pseudo code. it's whatever your current orientation is, attitude would be an option, but like I said before you probably want to filter the results.
well the child's rotation is the current orientation that i want to recenter, but if i just set it's rotation directly itll get re-updated to whatever the gyro thinks it should be on the next frame and no longer be recentered.
Because of that my goal is to offset that child's parent to the inverse of the child's rotation so that they cancel and the child is effectively facing forward again
Your answer
Follow this Question
Related Questions
How to get Angle float of specific axis.(Turret clamping related). 2 Answers
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers
[OAFAT]Why does transform.rotation.x = 45 not work 3 Answers
Apply game object rotation using relative VR controller rotation? 2 Answers
How to check for NaN 4 Answers