Files
aibis-dream/Assets/Scripts/FixSystem/Cable/VerletStick.cs
T
2025-03-21 15:31:50 +08:00

37 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VerletStick
{
private VerletParticle particleA;
private VerletParticle particleB;
private float length;
public float stiffness = 0.75f; // Add this line
public VerletStick(VerletParticle particleA, VerletParticle particleB, float length)
{
this.particleA = particleA;
this.particleB = particleB;
this.length = length;
stiffness = Mathf.Clamp(0.2f * (length / 0.5f), 0.5f, 0.75f); // 调整范围根据需要
}
public void UpdateStick()
{
float currentLength = Vector3.Distance(particleA.position, particleB.position);
float difference = length - currentLength;
Vector3 direction = (particleB.position - particleA.position).normalized;
if (!particleA.isLocked)
{
particleA.position -= direction * difference * stiffness;
}
if (!particleB.isLocked)
{
particleB.position += direction * difference * stiffness;
}
}
}