How to get the forces acting on the body

How to get all the forces acting on the body

There isn’t currently an API that will provide this for you. However, you have some options.

The easy way:

If you don’t care what the source of the forces are, you could probably just estimate the forces by differentiating the body’s velocities. The accumulated forces would be proportional to mass*((newLinVel - prevLinVel)/dt). Similarly, the accumulated torque would be inertia * ((newAngVel - prevAngVel)/dt).

However, if you care about where the forces come from, you have the following forces/torques acting on the system:

There is gravitational acceleration, which you should be able to get from the scene. It’s an acceleration, but f = ma so you can easily convert to a force if needed.

There are external forces/torques, which you will have applied through addForce, addTorque. There isn’t a “get” method that corresponds with this, because the behaviour is different depending on whether the force is flagged as an acceleration, so this is a case where you might need to keep track of any forces/torques you applied yourself.

There are the forces applied by the contacts and joints acting on the body. You can get the contact forces through contact notification - you request to be notified about these events in your filterShader and provide an onContact callback to get those forces reported. Contacts apply an equal and opposing force on both bodies. However, this is a scalar value, you would need to convert it into a force/torque vector as follows:

force += normal * f

torque += (contact.position - body.com).cross(normal) * f;

Similarly, PxConstraint has a getForce method, which returns the force and torque that the joint applied. Joints apply an equal and opposing force on both bodies.

Beyond that, there may be some damping applied, which you can get from the body’s linear and angular damping values. This is technically an acceleration rather than a force, and it is proportional to the body’s unconstrained velocity so it may be tricky to guesstimate exactly what this was. You could compute the damping accelerations (and therefore forces) for a given frame using the velocity before the simulation runs, knowing the time-step, the initial velocity and the damping values.