Object turning infinitely on custom physics simulation

78 Views Asked by At

Video Link

I have custom physics simulation that uses verlet. And I have a car made from nodes and joints. It turns constantly when car turn up down. There is damping and friction force.

Here is my collision response:

private void ApplyCollisionResponse(Vector3 contact_point,Vector3 contact_force)
{
    foreach (Deformable deformable in Deformables)
    {
        foreach (Node node in deformable.Nodes)
        {
            if (Vector3.Distance(node.transform.position, contact_point) < 1f)
            {
                //node.transform.position += Vector3.Lerp(node.transform.position,node.transform.position+contact_force,5f*Time.fixedDeltaTime);
                
                float velocity = Vector3.Dot(contact_force, node.GetVelocity());
                Vector3 current_projection = velocity*contact_force.normalized;
                Vector3 diff = (- current_projection);
                Vector3 impulse = Vector3.Lerp(current_projection,diff*node.GetMass(),0.5f);
                node.AddForce(impulse);

                // Move the node slightly along the collision force direction
                Vector3 collision_fix = 1.01f * contact_force   ;
                node.transform.position = Vector3.Lerp(node.transform.position, node.transform.position +  collision_fix, 0.5f);
            }
        }
    }
}

Here is my node code:

private Vector3 GetForce()
{
    Vector3 totalForce = Vector3.zero;

    // Additional forces (e.g., user input, springs, etc.)
    totalForce += AdditionalForces;
    // Drag force
    totalForce += -0.05f * Velocity.magnitude * Velocity.normalized * GetMass();

    AdditionalForces = Vector3.zero;

    return totalForce;
}

public void UpdateVerletIntegration()
{
    Velocity = (transform.position - prevPosition) / Time.fixedDeltaTime;
    Velocity *= (1-Damping);

    Vector3 force = GetForce();
    Vector3 gravity_force = Physics.gravity * Mass;

    if (force.magnitude < 0.001f)
    {
        force = Vector3.zero;
        Velocity = Vector3.zero;
        transform.position = prevPosition;  
    }

    Vector3 newPosition = transform.position + Velocity * Time.fixedDeltaTime + ((force+gravity_force) / Mass) * Mathf.Pow(Time.fixedDeltaTime, 2);
    prevPosition = transform.position;
    transform.position = newPosition;
    // Verlet 
}

I want to solve this turning problem.

0

There are 0 best solutions below