I have custom physics simulation that uses verlet.
I have a car made from nodes and joints. When collision response applied ,it start to turn constantly. There is damping and friction force. Probably joints pushed node into collider and it cause high force.
How can I prevent this?
Here is my joint code:
public override void ApplyJoint()
{
Vector3 delta = _connector.FirstNode.transform.position - _connector.SecondNode.transform.position;
float deltalength = delta.magnitude;
float joint_length = RestDistance;
// Calculate strain
float strain = (deltalength - joint_length) / joint_length;
float diff = (deltalength - joint_length) / deltalength;
_connector.SecondNode.transform.position += delta * 0.5f * diff;
_connector.FirstNode.transform.position -= delta * 0.5f * diff;
}
Here is my verlet code:
private Vector3 GetForce()
{
Vector3 totalForce = Vector3.zero;
// Additional forces (e.g., user input, springs, etc.)
totalForce += AdditionalForces;
// Drag force
Vector3 relativeVelocity = Velocity;
float speed = relativeVelocity.magnitude;
Vector3 dragForce = -0.5f * 20f * speed * speed * relativeVelocity.normalized;
totalForce += dragForce;
totalForce += Physics.gravity * GetMass();
AdditionalForces = Vector3.zero;
return totalForce;
}
public void UpdateVerletIntegration()
{
Velocity = (transform.position - prevPosition) / Time.fixedDeltaTime;
Velocity *= (1 - Damping);
Vector3 force = GetForce();
Vector3 newPosition = transform.position + Velocity * Time.fixedDeltaTime + ((force) / Mass) * Mathf.Pow(Time.fixedDeltaTime, 2);
prevPosition = transform.position;
transform.position = newPosition;
}