How can i determine how much torque do i need to rotate a rigidbody 90 degrees on Y Axis?
There is no drag no friction and torque forcetype is acceleration.
There is a method, that involves Torque and counter Torque.
So say you want to rotate the object locally until the localEulerAngles.y=90 degrees and you want to do it by torque. First, you need to add torque in the direction you need to go in. So we figure the acutal angle that we are at…
var speed=10.0;
var desired=90.0;
var damping=3.0;
var y=localEulerAngles.y;
if(y>180) y=y-360; // this gives you a rotation + or - instead of from 0 to 360.
var direction=(y==0) ? 0.0 : 1.0;
if(y<0) direction=-1.0;
So lets say that we are at angle zero currently. We need to calculate the original force based on the direction that we need to go.
force=speed * direction;
Next, we need to figure out the current local angular velocity in the y direction and estimate how far we are from our desired angle.
var ySpeed = transform.InverseTransformDirection(rigidbody.angularVelocity).y;
var framesTilAtCurrent=ySpeed * Time.smoothDeltaTime / [COLOR="red"](desired -y)[/COLOR];
Now we correct the force needed to stop it.
if(framesTilAtCurrent<=damping)
force=-speed * (damping - framesTilAtCurrent) * 2.0;//play with it and see what the 2.0 value should be. ;)
now simply add the force.
rigidbody.AddRelativeTorque(Vector3.up * force);
OR…
just use math and a Lerp to get you to that angle.
Hehe, this.
It sounds like what you’re doing isn’t really an accurate physics simulation, so I don’t see any reason to use the torque functions for it.
Thank you for the example BigMister, however i cant see where you use the “desired” variable to check if we are there or not.
Although i didnt try it yet, from my experience with adding the force of inverse angularVelocity to a rigidbody continuously is unstable sometimes.
Often it works to stop the rotation with the exact opposite force, rarely it goes nuts and starts rotating to insanity.
Anyways, you are right. I dont need such physics simulation so i rather decided to go with lerp.
But i would like to learn how to do the above though.
Fixed and marked in red, evidently writing code in pieces is not my strong suit. it made sense as I went though, honest.
The theory behind what I did was to give an inverse force up to twice the force applied to the object. I say “up to” because there is a math formula that gives an amount of dampening needed to correct the rotation. All this is on a per application basis, so some objects may respond differently.
It should work as it gives a force based on the angular momentum, not specific degree of rotation.
So if you wrote a program that said, if I am above this angle, force it this way, and if I am below this force it that way it would wobble trying to match the angles. It would also never really stop unless you had so much angular drag that it would just slowly stop on it’s own. (This would be called over correcting)
If you turned around and wrote a program based on the angle left to go, and the amount of speed you are currently carrying, then you could apply counter force before the angle is reached, slowing down the momentum before the angle was reached. Once you slowed it down enough, the angular momentum would change, thus allowing you to apply less force on the rotating object while it is slower, thus preventing over correcting.
It’s all theory though. That is what makes programming great.
Well, what if i need to do this in all 3 angles?
that is called Slerp
With this script the object just keeps rotating forever. Can anyone fix this?