107 lines
2.5 KiB
C#
107 lines
2.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace PhysicsTools
|
|
{
|
|
public class Winch : MonoBehaviour
|
|
{
|
|
public float brakeForce;
|
|
|
|
public float heaveTorque;
|
|
|
|
public float tensionTorque;
|
|
|
|
public float radius;
|
|
|
|
public float length;
|
|
|
|
public float inertia;
|
|
|
|
public float maxRopeLength;
|
|
|
|
public float minRopeLength;
|
|
|
|
public float rpm;
|
|
|
|
public float maxRPM;
|
|
|
|
private float omega;
|
|
|
|
public Rope rope;
|
|
|
|
public Winch()
|
|
{
|
|
radius = 1f;
|
|
length = 1f;
|
|
maxRPM = 1f;
|
|
omega = 0f;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
float fixedDeltaTime = Time.fixedDeltaTime;
|
|
float num = tensionTorqueValue() - heaveTorque;
|
|
float num2 = num / inertia;
|
|
float num3 = brakeForce * radius / inertia;
|
|
float num4 = omega + num2 * fixedDeltaTime;
|
|
if ((num2 < 0f && rope.getLength() > minRopeLength) ||
|
|
(num2 > 0f && num2 > num3 && rope.getLength() < maxRopeLength))
|
|
{
|
|
if (num4 > 0f && num4 - num3 * fixedDeltaTime < 0f)
|
|
{
|
|
num4 = 0f;
|
|
}
|
|
else if (num4 < 0f && num4 + num3 * fixedDeltaTime > 0f)
|
|
{
|
|
num4 = 0f;
|
|
}
|
|
else if (num4 > 0f)
|
|
{
|
|
num4 -= num3 * fixedDeltaTime;
|
|
}
|
|
else if (num4 < 0f)
|
|
{
|
|
num4 += num3 * fixedDeltaTime;
|
|
}
|
|
|
|
omega = num4;
|
|
rope.changeLength(getLinearSpeed() * fixedDeltaTime);
|
|
}
|
|
else
|
|
{
|
|
omega = 0f;
|
|
}
|
|
|
|
rpm = omega / 6.28318f * 60f;
|
|
if (Math.Abs(rpm) > maxRPM)
|
|
{
|
|
if (rpm < 0f)
|
|
{
|
|
rpm = 0f - maxRPM;
|
|
}
|
|
else
|
|
{
|
|
rpm = maxRPM;
|
|
}
|
|
}
|
|
|
|
omega = rpm / 60f * 6.28318f;
|
|
}
|
|
|
|
private float tensionTorqueValue()
|
|
{
|
|
tensionTorque = radius * getTension();
|
|
return tensionTorque;
|
|
}
|
|
|
|
private float getLinearSpeed()
|
|
{
|
|
return omega * radius;
|
|
}
|
|
|
|
private float getTension()
|
|
{
|
|
return rope.getTension();
|
|
}
|
|
}
|
|
} |