fix: clean gitignore for Godot C#
This commit is contained in:
58
Scripts/Core/RotatingComponent.cs
Normal file
58
Scripts/Core/RotatingComponent.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
|
||||
public abstract class RotatingComponent
|
||||
{
|
||||
public double MomentOfInertia { get; set; } // kg·m²
|
||||
public double AngularVelocity { get; set; } // rad/s
|
||||
public double AngularPosition { get; set; } // rad (0..2π, wrapped)
|
||||
public double TotalAngularPosition { get; set; } // rad (never wraps)
|
||||
public double AccumulatedTorque {get; private set; }
|
||||
|
||||
public RotatingComponent(double momentOfInertia = 0.1, double angularVelocity = 0, double angularPosition = 0)
|
||||
{
|
||||
MomentOfInertia = momentOfInertia;
|
||||
AngularVelocity = angularVelocity;
|
||||
AngularPosition = angularPosition;
|
||||
TotalAngularPosition = angularPosition;
|
||||
AccumulatedTorque = 0;
|
||||
}
|
||||
|
||||
public virtual void ApplyTorque(double torqueNm)
|
||||
{
|
||||
AccumulatedTorque += torqueNm;
|
||||
}
|
||||
|
||||
public virtual void Update(double dt)
|
||||
{
|
||||
if (dt <= 0) return;
|
||||
|
||||
if (MomentOfInertia > 0)
|
||||
{
|
||||
double angularAcceleration = AccumulatedTorque / MomentOfInertia;
|
||||
AngularVelocity += angularAcceleration * dt;
|
||||
}
|
||||
|
||||
double deltaAngle = AngularVelocity * dt;
|
||||
TotalAngularPosition += deltaAngle;
|
||||
AngularPosition += deltaAngle;
|
||||
|
||||
// Wrap AngularPosition to 0..2π for sin/cos
|
||||
AngularPosition = AngularPosition % (2 * Math.PI);
|
||||
if (AngularPosition < 0) AngularPosition += 2 * Math.PI;
|
||||
|
||||
AccumulatedTorque = 0;
|
||||
}
|
||||
|
||||
public double RPM => AngularVelocity * 60 / (2 * Math.PI);
|
||||
public double TotalAngleDeg => TotalAngularPosition * 180 / Math.PI;
|
||||
|
||||
public void SetRPM(double rpm)
|
||||
{
|
||||
AngularVelocity = rpm * 2 * Math.PI / 60;
|
||||
}
|
||||
|
||||
public virtual void ResetTorque()
|
||||
{
|
||||
AccumulatedTorque = 0;
|
||||
}
|
||||
}
|
||||
1
Scripts/Core/RotatingComponent.cs.uid
Normal file
1
Scripts/Core/RotatingComponent.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cym3n25hyryxy
|
||||
55
Scripts/Core/TorqueMap.cs
Normal file
55
Scripts/Core/TorqueMap.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class TorqueMap
|
||||
{
|
||||
public Dictionary<int, double> points;
|
||||
|
||||
public TorqueMap()
|
||||
{
|
||||
points = new Dictionary<int, double>();
|
||||
}
|
||||
|
||||
public void AddPoint(int rpm, double torque)
|
||||
{
|
||||
points.Add(rpm, torque);
|
||||
}
|
||||
|
||||
public double GetTorque(int rpm)
|
||||
{
|
||||
if (points == null || points.Count == 0)
|
||||
throw new System.InvalidOperationException("Torque map has no points.");
|
||||
|
||||
var sortedRpms = points.Keys.OrderBy(r => r).ToList();
|
||||
int firstRpm = sortedRpms[0];
|
||||
int lastRpm = sortedRpms[sortedRpms.Count - 1];
|
||||
|
||||
if (rpm <= firstRpm)
|
||||
return points[firstRpm];
|
||||
if (rpm >= lastRpm)
|
||||
return points[lastRpm];
|
||||
|
||||
for (int i = 0; i < sortedRpms.Count - 1; i++)
|
||||
{
|
||||
int r1 = sortedRpms[i];
|
||||
int r2 = sortedRpms[i + 1];
|
||||
if (rpm >= r1 && rpm <= r2)
|
||||
{
|
||||
double t1 = points[r1];
|
||||
double t2 = points[r2];
|
||||
return t1 + (rpm - r1) * (t2 - t1) / (r2 - r1);
|
||||
}
|
||||
}
|
||||
|
||||
throw new System.InvalidOperationException("Interpolation failed.");
|
||||
}
|
||||
|
||||
public double GetTorque(double angularVelocityRadPerSec)
|
||||
{
|
||||
// Convert rad/s to RPM: 1 rad/s = 60/(2π) RPM
|
||||
double rpmDouble = angularVelocityRadPerSec * 60.0 / (2.0 * Math.PI);
|
||||
int rpm = (int)rpmDouble; // or use rounding if preferred: (int)Math.Round(rpmDouble)
|
||||
return GetTorque(rpm);
|
||||
}
|
||||
}
|
||||
1
Scripts/Core/TorqueMap.cs.uid
Normal file
1
Scripts/Core/TorqueMap.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bn0khacav57pi
|
||||
Reference in New Issue
Block a user