- Home /
Does anyone know the code behind the AddForceAtPosition function?
I am trying to do a little mini-project that involves air friction and the AddForceAtPosition function is exactly the function I need but I don't really like just using something I don't understand. I don't know how this function works in terms of applying torque correctly and I would like to know so I can better understand how torque is represented in code in general. If anyone knows the code behind it or has any idea as to what it may be, please let me know.
@particlesmash I don't know the code, but most of unitys methods implementation is avaliable on GitHub: https://github.com/Unity-Technologies/UnityCsReference, i won't say all because i never read it all, but usually i find what i'm looking for.
Unfortunately, Unity rarely includes raw function implementations in the Git repository; ins$$anonymous$$d it includes references to hidden external implementations as to protect their intellectual property.
That is for performance, not intellectual property since its only the physics engine. Also, it's probably adding force to force and cross(position - transform.position, force) to torque.
Answer by Namey5 · Jun 24, 2019 at 06:47 AM
https://www.enchantedage.com/node/68
This website has a pretty good summary of how a basic rigidbody works, including a basic implementation of the AddForceAtPosition method;
public void ApplyForce (Vector3 forcePosition, Vector3 directionMagnitude)
{
float lengthSquared = directionMagnitude.LengthSquared();
if (lengthSquared < 1e-8f) return; // or use your own favorite epsilon
Force += directionMagnitude;
Vector3 distance = forcePosition - Position;
Torque += Vector3.Cross(directionMagnitude, distance);
}
So it essentially adds force to the centre of mass in the specified direction, and torque is calculated by finding the vector perpendicular to both the direction of the force and the direction of the point the force is being applied to.
Your answer
Follow this Question
Related Questions
Nose Diving Plane 1 Answer
Moving a rigid body on a plane applying forces 1 Answer
How to make 2d Vehicle/car physics in unity 3d 4.3 2d physics? 1 Answer
using torque to alighn an object with surface 0 Answers
Wheel Collider Stops Accelerating 0 Answers