Advertisement
Guest User

Physics_Helpers

a guest
Mar 31st, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1.         /// <summary>
  2.         /// Calculates the force vector required to be applied to a rigidbody through AddForce to achieve the desired position. Works with the Force ForceMode.
  3.         /// </summary>
  4.         /// <param name="rigidbody">The rigidbody that the force will be applied to.</param>
  5.         /// <param name="desiredPosition">The position that you'd like the rigidbody to have.</param>
  6.         /// <param name="timestep">The delta time between frames.</param>
  7.         /// <param name="accountForGravity">Oppose gravity force?</param>
  8.         /// <param name="maxForce">The max force the result can have.</param>
  9.         /// <returns>The force value to be applied to the rigidbody.</returns>
  10.         public static Vector3 CalculateRequiredForceForPosition(this Rigidbody rigidbody, Vector3 desiredPosition, float timestep = 0.02f, bool accountForGravity = false, float maxForce = float.MaxValue)
  11.         {
  12.             Vector3 nakedForce = (desiredPosition - rigidbody.position) / (timestep * timestep);
  13.             nakedForce *= rigidbody.mass;
  14.  
  15.             Vector3 gravityForce = Vector3.zero;
  16.             if (accountForGravity)
  17.                 gravityForce = rigidbody.CalculateAntiGravityForce();
  18.  
  19.             Vector3 deltaForce = nakedForce - (((rigidbody.velocity / timestep) * rigidbody.mass) + gravityForce);
  20.  
  21.             if (deltaForce.sqrMagnitude > maxForce * maxForce)
  22.                 deltaForce = deltaForce.normalized * maxForce;
  23.  
  24.             return deltaForce;
  25.         }
  26.  
  27.         /// <summary>
  28.         /// <para>Source: https://answers.unity.com/questions/48836/determining-the-torque-needed-to-rotate-an-object.html</para>
  29.         /// <para>Calculates the torque required to be applied to a rigidbody to achieve the desired rotation. Works with Force ForceMode.</para>
  30.         /// </summary>
  31.         /// <param name="rigidbody">The rigidbody that the torque will be applied to</param>
  32.         /// <param name="desiredRotation">The rotation that you'd like the rigidbody to have</param>
  33.         /// <param name="timestep">Time to achieve change in position.</param>
  34.         /// <param name="maxTorque">The max torque the result can have.</param>
  35.         /// <returns>The torque value to be applied to the rigidbody.</returns>
  36.         public static Vector3 CalculateRequiredTorqueForRotation(this Rigidbody rigidbody, Quaternion desiredRotation, float timestep = 0.02f, float maxTorque = float.MaxValue)
  37.         {
  38.             Vector3 axis;
  39.             float angle;
  40.             Quaternion rotDiff = desiredRotation * Quaternion.Inverse(rigidbody.transform.rotation);
  41.             rotDiff = rotDiff.Shorten();
  42.             rotDiff.ToAngleAxis(out angle, out axis);
  43.             axis.Normalize();
  44.  
  45.             angle *= Mathf.Deg2Rad;
  46.             Vector3 desiredAngularAcceleration = (axis * angle) / (timestep * timestep);
  47.            
  48.             Quaternion q = rigidbody.rotation * rigidbody.inertiaTensorRotation;
  49.             Vector3 T = q * Vector3.Scale(rigidbody.inertiaTensor, (Quaternion.Inverse(q) * desiredAngularAcceleration));
  50.             Vector3 prevT = q * Vector3.Scale(rigidbody.inertiaTensor, (Quaternion.Inverse(q) * (rigidbody.angularVelocity / timestep)));
  51.  
  52.             var deltaT = T - prevT;
  53.             if (deltaT.sqrMagnitude > maxTorque * maxTorque)
  54.                 deltaT = deltaT.normalized * maxTorque;
  55.  
  56.             return deltaT;
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement