Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Calculates the force vector required to be applied to a rigidbody through AddForce to achieve the desired position. Works with the Force ForceMode.
- /// </summary>
- /// <param name="rigidbody">The rigidbody that the force will be applied to.</param>
- /// <param name="desiredPosition">The position that you'd like the rigidbody to have.</param>
- /// <param name="timestep">The delta time between frames.</param>
- /// <param name="accountForGravity">Oppose gravity force?</param>
- /// <param name="maxForce">The max force the result can have.</param>
- /// <returns>The force value to be applied to the rigidbody.</returns>
- public static Vector3 CalculateRequiredForceForPosition(this Rigidbody rigidbody, Vector3 desiredPosition, float timestep = 0.02f, bool accountForGravity = false, float maxForce = float.MaxValue)
- {
- Vector3 nakedForce = (desiredPosition - rigidbody.position) / (timestep * timestep);
- nakedForce *= rigidbody.mass;
- Vector3 gravityForce = Vector3.zero;
- if (accountForGravity)
- gravityForce = rigidbody.CalculateAntiGravityForce();
- Vector3 deltaForce = nakedForce - (((rigidbody.velocity / timestep) * rigidbody.mass) + gravityForce);
- if (deltaForce.sqrMagnitude > maxForce * maxForce)
- deltaForce = deltaForce.normalized * maxForce;
- return deltaForce;
- }
- /// <summary>
- /// <para>Source: https://answers.unity.com/questions/48836/determining-the-torque-needed-to-rotate-an-object.html</para>
- /// <para>Calculates the torque required to be applied to a rigidbody to achieve the desired rotation. Works with Force ForceMode.</para>
- /// </summary>
- /// <param name="rigidbody">The rigidbody that the torque will be applied to</param>
- /// <param name="desiredRotation">The rotation that you'd like the rigidbody to have</param>
- /// <param name="timestep">Time to achieve change in position.</param>
- /// <param name="maxTorque">The max torque the result can have.</param>
- /// <returns>The torque value to be applied to the rigidbody.</returns>
- public static Vector3 CalculateRequiredTorqueForRotation(this Rigidbody rigidbody, Quaternion desiredRotation, float timestep = 0.02f, float maxTorque = float.MaxValue)
- {
- Vector3 axis;
- float angle;
- Quaternion rotDiff = desiredRotation * Quaternion.Inverse(rigidbody.transform.rotation);
- rotDiff = rotDiff.Shorten();
- rotDiff.ToAngleAxis(out angle, out axis);
- axis.Normalize();
- angle *= Mathf.Deg2Rad;
- Vector3 desiredAngularAcceleration = (axis * angle) / (timestep * timestep);
- Quaternion q = rigidbody.rotation * rigidbody.inertiaTensorRotation;
- Vector3 T = q * Vector3.Scale(rigidbody.inertiaTensor, (Quaternion.Inverse(q) * desiredAngularAcceleration));
- Vector3 prevT = q * Vector3.Scale(rigidbody.inertiaTensor, (Quaternion.Inverse(q) * (rigidbody.angularVelocity / timestep)));
- var deltaT = T - prevT;
- if (deltaT.sqrMagnitude > maxTorque * maxTorque)
- deltaT = deltaT.normalized * maxTorque;
- return deltaT;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement