- Home /
Trying to replicate Rigidbody.AddForceAtPosition
I'm working on a pretty specialized small physics simulation. It's extremely narrow and only really needs to handle gravity and execute one behavior, the Rigidbody.AddForceAtPosition() function. And since it's going to simulate a lot of identical bodies in the same way, I pretty sure I'm will gain a lot of performance by writing my own version i C which only does what I want super hard-coded.
The behavior works perfectly with a normal Rigidbody in Unity. So I'm trying to replicate my exact behavior (in C# first) without Unity's (good, but bloated for my uses) physics engine.
I've started to replicate the AddForceAtPosition(), but I'm not getting the same behavior. So TL;DR do you think this code does what AddForceAtPosition() does? Or are my physics off?
float mass = 1;
float inertia = 1;
// - Obv. not the real values -
Vector3 position = Vector3.zero;
Quaternion rotation = Quaternion.identity;
Vector3 force = Vector3.one;
Vector3 force_point = Vector3.one;
// --
linear_velocity += gravity * delta_time;
Vector3 linear_acc = Vector3.zero;
linear_acc = force / mass;
Vector3 angular_acc = Vector3.zero;
angular_acc += Vector3.Cross(position - force_point, force) / inertia;
linear_velocity += linear_acc * delta_time;
angular_velocity += angular_acc * delta_time;
position = position + linear_velocity * delta_time;
rotation = rotation * Quaternion.Euler(angular_velocity);
Answer by nt314p · Aug 09, 2017 at 09:53 PM
Looks alright but the real test would be to simulate two identical objects by adding the same force, one with AddForceAtPosition, and one with your custom script.
Your answer
Follow this Question
Related Questions
Solar System Simulator Orbits Not Working 2 Answers
buttonNames for GetButton 1 Answer
How can I add a reaction force? 0 Answers
take control over particles 0 Answers
Move rigidbody to position and still have forces act on it 0 Answers