refactor(fix-system): 将 Cable 线缆系统迁移至 FixSystemNew
Made-with: Cursor
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 655be486d0c27fa458354a686c592f55
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,52 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
public class PhysicCable : MonoBehaviour
|
||||
{
|
||||
[FormerlySerializedAs("StartTranform")] public Transform startTransform;
|
||||
|
||||
public PhysicLineSegment physicLine;
|
||||
|
||||
private Transform targetTransform;
|
||||
private bool isEnabled;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
physicLine = transform.GetComponent<PhysicLineSegment>();
|
||||
}
|
||||
|
||||
public void Init(Vector3 endPos)
|
||||
{
|
||||
physicLine.Initialize(startTransform.position, endPos);
|
||||
}
|
||||
|
||||
public void HideCable()
|
||||
{
|
||||
GetComponent<LineRenderer>().enabled = false;
|
||||
}
|
||||
|
||||
public void ShowCable()
|
||||
{
|
||||
GetComponent<LineRenderer>().enabled = true;
|
||||
}
|
||||
|
||||
public void SetEnabled(bool enabled)
|
||||
{
|
||||
isEnabled = enabled;
|
||||
GetComponent<LineRenderer>().enabled = enabled;
|
||||
}
|
||||
|
||||
public void SetTarget(Transform target)
|
||||
{
|
||||
targetTransform = target;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isEnabled && targetTransform != null)
|
||||
{
|
||||
physicLine.UpdatePlug(targetTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc956cb4eb967a4498d7eeef575ceb63
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,95 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(LineRenderer))]
|
||||
public class PhysicLineSegment : MonoBehaviour
|
||||
{
|
||||
public float segmentSpacing = 0.1f;
|
||||
public int k = 30;
|
||||
|
||||
public List<VerletParticle> particles = new();
|
||||
private List<VerletStick> sticks = new();
|
||||
[HideInInspector]
|
||||
public LineRenderer lineRenderer;
|
||||
private int segmentCount;
|
||||
public float gravity = -9.8f;
|
||||
|
||||
private float originalLength;
|
||||
|
||||
public void Initialize(Vector3 start, Vector3 end)
|
||||
{
|
||||
particles.Clear();
|
||||
sticks.Clear();
|
||||
if (lineRenderer != null)
|
||||
{
|
||||
lineRenderer.positionCount = 0;
|
||||
}
|
||||
|
||||
originalLength = Vector3.Distance(start, end);
|
||||
|
||||
segmentCount = Mathf.RoundToInt(originalLength / segmentSpacing);
|
||||
lineRenderer = GetComponent<LineRenderer>();
|
||||
|
||||
// Initialize particles
|
||||
for (int i = 0; i < segmentCount; i++)
|
||||
{
|
||||
var particle = new VerletParticle(Vector3.Lerp(start, end, i / (float)segmentCount));
|
||||
particles.Add(particle);
|
||||
}
|
||||
|
||||
// Initialize sticks
|
||||
for (int i = 0; i < segmentCount - 1; i++)
|
||||
{
|
||||
sticks.Add(new VerletStick(particles[i], particles[i + 1], segmentSpacing));
|
||||
}
|
||||
|
||||
// Lock the first particle
|
||||
particles[0].isLocked = true;
|
||||
|
||||
lineRenderer.positionCount = segmentCount;
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
UpdateLine();
|
||||
for (int i = 0; i < segmentCount; i++)
|
||||
{
|
||||
if (lineRenderer != null)
|
||||
{
|
||||
lineRenderer.SetPosition(i, particles[i].position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateLine()
|
||||
{
|
||||
// Apply gravity to particles
|
||||
for (int i = 1; i < particles.Count; i++)
|
||||
{
|
||||
particles[i].ApplyForce(Vector3.up * gravity * Time.deltaTime);
|
||||
particles[i].UpdatePosition();
|
||||
}
|
||||
|
||||
// Update sticks
|
||||
for (int ik = 0; ik < k; ik++)
|
||||
{
|
||||
foreach (var stick in sticks)
|
||||
{
|
||||
stick.UpdateStick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdatePlug(Transform plug)
|
||||
{
|
||||
if (plug && particles.Count > 1)
|
||||
{
|
||||
Vector3 plugPosition = particles[^1].position;
|
||||
float plugLength = plug.GetComponent<SpriteRenderer>().bounds.size.y;
|
||||
Vector3 direction = particles[^1].position - particles[^2].position;
|
||||
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
||||
plug.rotation = Quaternion.Euler(0, 0, angle - 90f);
|
||||
plug.position = plugPosition + direction.normalized * (plugLength / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1198bee0150e12d4aa20514b2c8f3362
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,46 +0,0 @@
|
||||
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; // 用速度更新当前位置
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cdb6feb01313af04f8abf13c63227196
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,36 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f52ebcdf1c225c48ad6413459d49309
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user