I need to find angular velocity of a game object as I can’t use rigidbody forces.I have searched every possible reference in the web.Most of that are formulas but I can’t figure out how to convert it into code.
For example here.
I need to find angular velocity of a game object as I can’t use rigidbody forces.I have searched every possible reference in the web.Most of that are formulas but I can’t figure out how to convert it into code.
For example here.
What are you attempting to do? And why can’t you use Rigidbodies?
I need to get an angular velocity of FPS controller.I can’t say why but I can’t use the rigid body forces in my scenario.
The formula seems understandable in general but how do I find quat derivatives and how can I cross product between inverses product and the division of the derivatives…Have no idea how to implement it…
using UnityEngine;
public class RotationTracker : MonoBehaviour {
//First we'll need a couple of variables to do the calculation.
Quaternion rotationLast; //The value of the rotation at the previous update
Quaternion rotationDelta; //The difference in rotation between now and the previous update
//Initialize rotationLast in start, or it will cause an error
void Start() {
rotationLast = transform.rotation;
}
void Update () {
//Update both variables, so they're accurate every frame.
rotationDelta = transform.rotation - rotationLast;
rotationLast = transform.rotation;
}
//Ta daa!
public Vector3 angularVelocity {
get {
return rotationDelta.eulerAngles;
}
}
}
Are you kidding me??!?!?! That’s it?? What about inverse of quaternion and cross product and division by 2 .Is this really correct?
No, actually it isn’t. I pasted before testing it. You can’t subtract quaternions. My bad:
using UnityEngine;
public class RotationTracker : MonoBehaviour {
//First we'll need a couple of variables to do the calculation.
Vector3 rotationLast; //The value of the rotation at the previous update
Vector3 rotationDelta; //The difference in rotation between now and the previous update
//Initialize rotationLast in start, or it will cause an error
void Start() {
rotationLast = transform.rotation.eulerAngles;
}
void Update () {
//Update both variables, so they're accurate every frame.
rotationDelta = transform.rotation.eulerAngles - rotationLast;
rotationLast = transform.rotation.eulerAngles;
}
//Ta daa!
public Vector3 angularVelocity {
get {
return rotationDelta;
}
}
}
Actually you can but it doesn’t seem to work fine.There is a jump in avelocity values.
I have a function called ‘FromToRotation’:
/// <summary>
/// Get the rotation that would be applied to 'start' to end up at 'end'.
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
public static Quaternion FromToRotation(Quaternion start,Quaternion end)
{
return Quaternion.Inverse(start)* end;
}
That’s basically how you subtract quaternions.
You’d say:
rotationDelta = QuaternionUtil.FromToRotation(rotationLast, transform.rotation);
rotationLast = transform.rotation
Note, rotationDelta isn’t the angular velocity though, because the delta is over some arbitrary amount of time. You need to scale those values up to some unit range (per second usually).
//still in the Update call
angularVelocity = rotationDelta.euler / Time.deltaTime;
If you want to store the velocity as a quaternion as well you could do:
Vector3 axis;
float angle;
QuaternionUtil.GetAngleAxis(rotationDelta, out axis, out angle);
angularVelocity = Quaternion.AxisAngle(axis, angle / Time.deltaTime);
Note though, this can’t store a speed greater than 1 full rotation per second, because the quaternion will loop around and store the short angle.
Delta time maybe?The values don’t looks so well.Something is missing here.
I’m not sure if I edited before or after you posted that.
I am not sure your code is right.The values are too huge and jumpy.You can try it.But thanks for the direction.
The size of the values are accurate when I test.
The values don’t land on the accurate number though, and instead hover within a short range of it. And I believe this is the result of float error and the fluctuation of framerate.
This is the script measuring:
using UnityEngine;
using System.Collections;
using com.spacepuppy.Utils;
public class QMeasureAngularVelocity : MonoBehaviour {
private Quaternion _lastRot;
private Quaternion _deltaRot;
private Vector3 _angularVel;
private void Start()
{
_lastRot = this.transform.rotation;
_deltaRot = Quaternion.identity;
_angularVel = Vector3.zero;
}
// Update is called once per frame
void Update()
{
var delta = this.transform.rotation.eulerAngles - _lastRot.eulerAngles;
var vel = delta / Time.deltaTime;
_deltaRot = QuaternionUtil.FromToRotation(_lastRot, this.transform.rotation);
_lastRot = this.transform.rotation;
_angularVel = _deltaRot.eulerAngles / Time.deltaTime;
Debug.Log(vel.ToDetailedString() + " --- " + _angularVel.ToDetailedString());
}
}
And the script rotating:
using UnityEngine;
using System.Collections;
using com.spacepuppy.Utils;
public class QRotator : MonoBehaviour {
public float Speed = 100f;
private void Update()
{
var v = this.transform.rotation.eulerAngles;
v.y += this.Speed * Time.deltaTime;
this.transform.rotation = Quaternion.Euler(v);
}
}
And the print out:

I’m not 100% sure why it happens though. And I honestly don’t have the time to figure out why.
Hahaha this dude always cracks me up in the forums Doofy is fucking awesome don’t ever change your pic man…anyways lol
Haha, thanks. Officer doofy is on the case.
Well, you shouldnt have to multiply by delta time, because the rotation is already measured in delta time, since youre doing the calculation every update.
I’m not near my machine right now, i’ll take another look at er tomorrow.
Well dudes,so far all the proposed code above doesn’t seem to be exact simulation of rigidbody’s angularVelocity.
lordofduct’s example spits really shitty numbers,most of which are really huge or even infinity.Also based ,on the math formulas smth is missing here…Anyone has an idea? God damn ,this should be one of the simplest things to calc yet I can’t find a single example in the web!
…what? Delta time is used to make movements framerate independent. Update is called per frame. There could only be 10 Update calls in a second if your game is badly optimized rather than a desired 60.
Right, and if your object is spinning one rotation a second, and you’re measuring angular velocity between frames, and in your example weve been slowed to 10fps than this solution should evaluate to: 36 degrees spun since last frame.
Right?
…okay, i see what youre saying. Yes, to make it framerate independent, you’d have to multiply by deltatime to get the number of degrees per second.
Thanks for unconfusing me!
Okay, well it’s obvious to me now why this code isn’t working:
If the last rotation is the euler equivalent of 359 degrees on any given axis, and the current rotation is the euler equivalent to 1 degrees on the same axis, the magnitude is going to appear to be over 360 degrees since the last frame. So that’s not going to work.
I did some reading, and came up with the following which worked after testing:
using UnityEngine;
public class RotationTracker : MonoBehaviour {
//Holds the previous frames rotation
Quaternion lastRotation;
//References to the relevent axis angle variables
float magnitude;
Vector3 axis;
public Vector3 angularVelocity {
get {
//DIVDED by Time.deltaTime to give you the degrees of rotation per axis per second
return (axis * magnitude) / Time.deltaTime;
}
}
void Start() {
lastRotation = transform.rotation;
}
void Update() {
//The fancy, relevent math
Quaternion deltaRotation = transform.rotation * Quaternion.Inverse (lastRotation);
deltaRotation.ToAngleAxis(out magnitude, out axis);
lastRotation = transform.rotation;
}
}
I placed this code on an object which also had a script that was forcing it to make 1 full rotation per second. This script correctly gave the Vector3 ‘0,0,360’, mind you it does seem to fluctuate somewhat.
Working scene enclosed:
1911815–123313–RotateTest.unitypackage (5.38 KB)
I’d also like to add that you’d have to tweak this code to get radians or some other usable value. A vector of 0’0’360 is needlessly large.
Old thread but im posting for other. I was trying to calculate the angular velocity when I throw something in the HTC vive. I’m using RigidBodies that are kinematic while grabbed, meaning I need to calculate it when released.
Following is in FixedUpdate:
var deltaRot = transform.rotation * Quaternion.Inverse( lastRot );
var eulerRot = new Vector3( Mathf.DeltaAngle( 0, deltaRot.eulerAngles.x ), Mathf.DeltaAngle( 0, deltaRot.eulerAngles.y ),Mathf.DeltaAngle( 0, deltaRot.eulerAngles.z ) );
angularVelocity = eulerRot / Time.fixedDeltaTime;
The DeltaAngle is required as it will turn (350, 0,0) into (-10, 0,0), but wont work if you have very high angular velocities.