I need to apply my custom position based constraints (for elastic rod) on a chain of rigidbodies in Unity, which uses PhysX underneath (but the problem concerns any physics engine really).
To keep things simple let say that I want to apply a simple distance constraint between positions of two rigid bodies governed by PhysX.
In my PBD framework I predict positions using Explicit Euler integration, then apply constraints to these predicted positions, I update velocity (predictedPositions - positions) / dt, followed by positions = predictedPositions.
The question is how to do the positions and velocity update when the numerical integration is held by PhysX or Bullet or any other physics engine?
Just to add that the PhysX API is not fully exposed. I can modify things only between full physics update.
I have tried something like this. The constraints are very sloppy, as in my opinion, the momentum is not considered (there is no prediction step)
Code: Select all
//copy positions from the physics engine
for (int i = 0; i < pointsCount; i++)
m_predPositions[i] = m_rigidbodies[i].position;
//modify predicted position
for (int si = 0; si < solverIterations; si++)
ProjectDistanceConstraints(m_predPositions[i]);
//apply changes back to the engine
for (int i = 0; i < pointsCount; i++)
{
m_rigidbodies[i].velocity = (m_predPositions[i] - m_rigidbodies[i].position) * dtInv;
m_rigidbodies[i].position = m_predPositions[i];
}
Code: Select all
m_rigidbodies[i].AddForce((m_predPositions[i] - m_rigidbodies[i].position) * dtInv, ForceMode.VelocityChange);
m_rigidbodies[i].position = m_predPositions[i];