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

47 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VerletParticle
{
public Vector3 position; // 当前帧位置
public Vector3 previousPosition; // 上一帧位置
public float mass = 1f; // 默认质量
public bool isLocked; // 是否锁定
public VerletParticle(Vector3 position)
{
this.position = position;
this.previousPosition = position; // 初始时,上一帧的位置与当前相同
this.isLocked = false; // 默认粒子不锁定
}
// Apply force to the particle (affects velocity)
public void ApplyForce(Vector3 force)
{
if (!isLocked)
{
Vector3 velocity = position - previousPosition;
Vector3 airResistance = -velocity * 0.3f;
//Vector3 damping = velocity * 0.02f; // 额外的速度衰减
velocity += (force + airResistance) * Time.deltaTime / mass; // 考虑质量
previousPosition = position;
position += velocity;
}
}
// Update the particle's position using Verlet integration
public void UpdatePosition()
{
if (!isLocked)
{
Vector3 velocity = position - previousPosition; // 计算当前速度
previousPosition = position; // 更新上一帧位置
position += velocity; // 用速度更新当前位置
}
}
}