- Home /
Using Torque to kill Angular Velocity
I'm trying to write a method that adds a limited amount of torque to a rigidbody in order to slow its rotation. I tried to do it in a way that, apart from limiting the actual force to a lower value, basically amounted to the following:
rigidbody.AddRelativeTorque(-rigidbody.angularVelocity);
It was meant to be applied over multiple FixedUpdate() cycles until the angular velocity was completely cancelled. But of course, nothing goes as planned, and instead I just have rigidbodies rotating around their z axes a lot. There is no drag and no other forces affecting it, mind you.
Now, I tried converting the angularVelocity from radians to degrees, I tried AddRelativeTorque and AddTorque, and I also tried plenty of modifications to the exact amount of force applied - to no avail.
My instincts tell me that getting the angularVelocity vector, multiplying it with (-1) and applying that as force should slow rotation and cancel it eventually (the time needed depending on the objects mass)...but it doesn't work that way.
Any ideas, gentlemen?
Answer by robertbu · Jan 08, 2014 at 07:19 PM
Untested, but I'm pretty sure this will work. It should be done in FixedUpdate(), and the slowdown will not be linear. You may need to fiddle with the constant.
rigidbody.angularVelocity *= 0.985;
Depending on how you want to motion to slow, you can use Lerp() or MoveTowards, or others to scale the components of angularVelocity to 0 over time. Based on your question, I'm think you already realize you could play with the 'angularDrag' setting...upping it to get the object to slow then returning it to some value after the angularVelocity falls below some threshold.
Thanks, but that's really not quite what I'm looking for. The possibility of "Faking" it by directly manipulating the angularVelocity or temporarily adding angularDrag is obviously handy, and I'll resort to it if nothing else works out, but I'd really prefer a torque-based solution :)
I just ran a quick test, and adding torque in the opposite of Rigidbody.angularVelocity() worked fine to slow/stop an object.
#pragma strict
function Start() {
rigidbody.AddTorque(Random.insideUnitSphere * 200.0);
}
function FixedUpdate() {
rigidbody.AddTorque(-rigidbody.angularVelocity * 0.1);
}
Then I guess I really must be doing something else wrong. I'll have to do more testing. Thanks for confir$$anonymous$$g this!
Answer by deneb3525 · May 06, 2019 at 02:33 AM
Vector3 rotation = transform.InverseTransformDirection(rb.angularVelocity);
if (rotation.y <= .1 && rotation.y >= -0.1)
{
rotation.y = 0;
Debug.Log("cancel y");
}
else if (rotation.y > .1)
{
rb.AddRelativeTorque(Vector3.up * yaw * -1);
}
else if (rotation.y < -0.1)
{
rb.AddRelativeTorque(Vector3.up * yaw);
}
rb.angularVelocity = transform.TransformVector(rotation);
I know this is a fairly old question, but it took me a bit to solve a similar question. I cut the sections for X and Z, but they work the same as Y. This code was part of a greater fragment that allowed me to selectively damped various axis of a spaceships rotation. The "if (rotation.y <= .1 && rotation.y >= -0.1)" section is because this code would otherwise vibrate as the thrust would push rotation from one direction to the other every frame.
Hey, it's very late indeed and I've since solved the problem in various ways, but your answer is still much appreciated. Thanks!
Your answer
Follow this Question
Related Questions
AddRelativeTorque results in incorrect angular acceleration (Maybe centrifugal force affects) 0 Answers
rigid body Clone Instance not behaving like original 1 Answer
How to rotate with a Pid controller 0 Answers
AddTorque is spinning faster in one direction than the other.. 1 Answer
Aligning 2 Quaternions Using Torque 1 Answer