# 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.

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.