#Accelerating rotation by difference in direction

1 messages · Page 1 of 1 (latest)

pure coral
#

I am attempting to rotate an entity with physics via adjustment of PhysicsVelocity.Angular in order to gradually point the entity in a direction (in this case the direction it is wanting to accelerate in) but cant figure out how to get the amount i wish to adjust angular velocity by to rotate it in the right direction, all i have managed to do thus far is make a spinner. (i know barely anything about vectors and quaternions)

cyan crown
#

I wonder if a PID solver will help you? I've done something similar in my project to get the exact angular velocity I need. You might also be able to lerp between current rotation and a target rotation if you only need someting simple

pure coral
#

sadly, lerp is not enough
but im just not sure how to get it rotating the correct way

mighty marten
#

~~are you using a spherical lerp?~~nvm im hallucinating

pure coral
#
   Vector3 unitEuler = unitRotation.eulerAngles;

   float3 differenceR = (unitEuler-new Vector3(180, 180, 180)) + (targetRotation.eulerAngles - new Vector3(180, 180, 180));

   float3 rotForce = math.clamp(differenceR, -maxRot, maxRot);

   angularVelocity += rotForce; //rotationalThrust;

   physicsVelocity.Angular = angularVelocity;

this is what my latest attempt looks like (but it just turns into a infinitely accelerating spinner)
i know using euler isint optimal, but i have even less of an idea on how to properly use quaternions for this

mighty marten
#

what about something like this?

var diffRotation = Time.fixedDeltaTime * math.mul(math.inverse(unitRotation), targetRotation); // maybe flip the multiplication order, I always forget
physicsVelocity.Angular = math.Euler(diffRotation);
pure coral
#

yeah, thats works, though it looks like i will need a predictive element aswell

mighty marten
#

predictive?

pure coral
#

just something to stop it from rotating too much and overshooting (because i want the rotational force to be small but additive)

mighty marten
#

ah, i didnt expect it to overshoot actually, but I'm sure you can figure it out
you are changing this physics angular velocity in the fixed update right?

pure coral
#

Yes, its just overshooting because im making it add an amount to velocity each update

mighty marten
#

yeah though my example will add less if the difference is less, so I didn't expect it to really overshoot

#

anyway soudns like you got it working 👍