Skip to content

Instantly share code, notes, and snippets.

@maxattack
Last active April 5, 2024 01:50
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save maxattack/4c7b4de00f5c1b95a33b to your computer and use it in GitHub Desktop.
Save maxattack/4c7b4de00f5c1b95a33b to your computer and use it in GitHub Desktop.
Some Helper Methods for Quaternions in Unity3D
using UnityEngine;
/*
Copyright 2016 Max Kaufmann (max.kaufmann@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public static class QuaternionUtil {
public static Quaternion AngVelToDeriv(Quaternion Current, Vector3 AngVel) {
var Spin = new Quaternion(AngVel.x, AngVel.y, AngVel.z, 0f);
var Result = Spin * Current;
return new Quaternion(0.5f * Result.x, 0.5f * Result.y, 0.5f * Result.z, 0.5f * Result.w);
}
public static Vector3 DerivToAngVel(Quaternion Current, Quaternion Deriv) {
var Result = Deriv * Quaternion.Inverse(Current);
return new Vector3(2f * Result.x, 2f * Result.y, 2f * Result.z);
}
public static Quaternion IntegrateRotation(Quaternion Rotation, Vector3 AngularVelocity, float DeltaTime) {
if (DeltaTime < Mathf.Epsilon) return Rotation;
var Deriv = AngVelToDeriv(Rotation, AngularVelocity);
var Pred = new Vector4(
Rotation.x + Deriv.x * DeltaTime,
Rotation.y + Deriv.y * DeltaTime,
Rotation.z + Deriv.z * DeltaTime,
Rotation.w + Deriv.w * DeltaTime
).normalized;
return new Quaternion(Pred.x, Pred.y, Pred.z, Pred.w);
}
public static Quaternion SmoothDamp(Quaternion rot, Quaternion target, ref Quaternion deriv, float time) {
if (Time.deltaTime < Mathf.Epsilon) return rot;
// account for double-cover
var Dot = Quaternion.Dot(rot, target);
var Multi = Dot > 0f ? 1f : -1f;
target.x *= Multi;
target.y *= Multi;
target.z *= Multi;
target.w *= Multi;
// smooth damp (nlerp approx)
var Result = new Vector4(
Mathf.SmoothDamp(rot.x, target.x, ref deriv.x, time),
Mathf.SmoothDamp(rot.y, target.y, ref deriv.y, time),
Mathf.SmoothDamp(rot.z, target.z, ref deriv.z, time),
Mathf.SmoothDamp(rot.w, target.w, ref deriv.w, time)
).normalized;
// ensure deriv is tangent
var derivError = Vector4.Project(new Vector4(deriv.x, deriv.y, deriv.z, deriv.w), Result);
deriv.x -= derivError.x;
deriv.y -= derivError.y;
deriv.z -= derivError.z;
deriv.w -= derivError.w;
return new Quaternion(Result.x, Result.y, Result.z, Result.w);
}
}
@cycadacka
Copy link

Thanks dud.

@johanhelsing
Copy link

Hey, works great :) What kind of license is it under? Can I use it in a closed-source project?

@StromKuo
Copy link

StromKuo commented Jun 4, 2020

Thanks.
But Time.deltaTime is 0 when Time.timeScale is 0.
var dtInv = 1f / Time.deltaTime; Is it OK?

@maxattack
Copy link
Author

@johanhelsing added an MIT blurb
@StromKuo If dt = 0 then it's a NOOP, so I added early-outs.

@maxattack
Copy link
Author

also, the handling of deriv in SmoothDamp was comically bad, so I fixed that, too

@webmonch
Copy link

Hello,
thanks a lot for this script. One question - I tried to SmoothDump to Quaternion.LookRotation() and it won't look up or down, only looks slightly up or down. Why so?

@Lance2185
Copy link

This is an awesome script. It has already been extremely helpful in setting up rotations for one of my projects. I am curious to know if the Quaternion SmoothDamp function can be set up for an alternative maximum velocity for a given rotation. Can we specify a maximum value for degrees per second or anything along those lines?

@Enliven26
Copy link

Hello, thanks a lot for this script. One question - I tried to SmoothDump to Quaternion.LookRotation() and it won't look up or down, only looks slightly up or down. Why so?

Probably because the line only run once (smoothdamp line needs to run in more than one cycle of a function like fixedUpdate or else to achieve the target position/orientation). Check the conditional (if/else) line that use the smoothdamp. Sorry for the late reply because probably you had already solved it

@Enliven26
Copy link

May I ask if the AngVelToDeriv and DerivToAngVel algorithm is correct? I am not sure about why you scale individual component by 0.5f and 2f despite it has no influences since it will get normalized anyway in rotation. Also, I am not sure the Spin variable in AngVelToDeriv really represent the AngVel argument in quaternion form.

@Enliven26
Copy link

May I ask if the AngVelToDeriv and DerivToAngVel algorithm is correct? I am not sure about why you scale individual component by 0.5f and 2f despite it has no influences since it will get normalized anyway in rotation. Also, I am not sure the Spin variable in AngVelToDeriv really represent the AngVel argument in quaternion form.

I recommend using Quaternion.ToEulerAngles and Quaternion.EulerAngles to convert between AngVel and Deriv. I tested your code and the SmoothDamp works fine, but the other functions make wrong rotation. Thanks for the code btw

@p1ckLL
Copy link

p1ckLL commented Feb 17, 2023

whats deriv

@Chunky-Dreams
Copy link

@p1ckLL "deriv" is equivalent to "ref Vector3 currentVelocity" in the Vector3.SmoothDamp() method.
It is used to store the current velocity of the interpolated value, so that we can use it in the next interpolation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment