自定义Verlet物理模拟中车辆上下移动时持续转向问题求解
自定义Verlet积分物理车辆持续转向问题求助
我使用Verlet积分实现了自定义物理模拟,并用节点和关节构建了一辆车辆。当车辆上下移动时会持续转向,当前已添加阻尼和摩擦力。
碰撞响应代码
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); } } } }
节点代码
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 }
现寻求解决该车辆持续转向的问题。
内容的提问来源于stack exchange,提问作者Berkay Onur
相关产品推荐
相关产品推荐

