Door/hood sync via hinge spring instead of teleport (fixes NaN crash and parts falling off); join/leave toasts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+60
-40
@@ -7,8 +7,10 @@ namespace MSCO
|
||||
{
|
||||
/// <summary>
|
||||
/// Türen, Motorhaube und Kofferraum an Fahrzeugen: hängen mit einem Scharnier (HingeJoint) an der Karosserie
|
||||
/// und werden deshalb nicht vom Objekt-Sync bewegt. Übertragen wird die Stellung relativ zum Auto, damit eine
|
||||
/// offene Tür beim anderen genauso weit offen steht.
|
||||
/// und werden deshalb nicht vom Objekt-Sync bewegt. Übertragen wird der Öffnungswinkel des Scharniers.
|
||||
/// Beim Empfänger wird die Tür NICHT hingesetzt (Teleportieren gegen das Gelenk erzeugt riesige Kräfte - Teile
|
||||
/// reißen ab, im schlimmsten Fall stürzt die Physik ab), sondern die Feder des Scharniers zieht sie sanft auf
|
||||
/// den Winkel. Danach bekommt das Scharnier seine ursprüngliche Einstellung zurück.
|
||||
/// Wer sendet: Fährt jemand das Auto, der Fahrer. Ist es frei, wer gerade daneben steht (Tür auf/zu gemacht).
|
||||
/// </summary>
|
||||
public class HingeSync
|
||||
@@ -17,19 +19,23 @@ namespace MSCO
|
||||
const float SendInterval = 0.1f;
|
||||
const float MinAngle = 2f; // Grad - kleiner ist Zittern
|
||||
const float NearRange = 4f; // so nah muss man an einer Tür stehen, um sie zu bewegen
|
||||
const float ApplyHold = 1.5f; // so lange nach einer Nachricht die Stellung halten und selbst nicht senden
|
||||
const float ApplyHold = 1.5f; // so lange nach einer Nachricht die Feder halten und selbst nicht senden
|
||||
const float SpringForce = 120f;
|
||||
const float SpringDamper = 12f;
|
||||
|
||||
class Hinge
|
||||
{
|
||||
public int Key;
|
||||
public string Root;
|
||||
public HingeJoint Joint;
|
||||
public Rigidbody Rb;
|
||||
public Rigidbody Body; // woran das Scharnier hängt
|
||||
public Quaternion SentRot;
|
||||
public float SentAngle;
|
||||
public bool HasSent;
|
||||
public Quaternion TargetRot;
|
||||
public Vector3 TargetPos;
|
||||
public float TargetAngle;
|
||||
public float TargetTime = -100f;
|
||||
public bool Driving; // Feder gerade von uns gesetzt
|
||||
public bool OrigUseSpring;
|
||||
public JointSpring OrigSpring;
|
||||
}
|
||||
|
||||
readonly Dictionary<int, Hinge> hinges = new Dictionary<int, Hinge>();
|
||||
@@ -41,6 +47,7 @@ namespace MSCO
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
foreach (Hinge h in hinges.Values) StopDriving(h);
|
||||
hinges.Clear();
|
||||
seen.Clear();
|
||||
nextScan = 0f;
|
||||
@@ -60,15 +67,16 @@ namespace MSCO
|
||||
outgoing.Clear();
|
||||
foreach (Hinge h in hinges.Values)
|
||||
{
|
||||
if (h.Rb == null || h.Body == null || !h.Rb.gameObject.activeInHierarchy) continue;
|
||||
if (h.Joint == null || h.Rb == null || !h.Rb.gameObject.activeInHierarchy) continue;
|
||||
if (Time.time - h.TargetTime < ApplyHold) continue; // gerade von anderen gesetzt
|
||||
int owner = objects.VehicleOwner(h.Root);
|
||||
if (owner != SyncedBody.NoOwner && owner != localId) continue; // der Fahrer schickt
|
||||
if (owner == SyncedBody.NoOwner && (h.Rb.position - playerPos).sqrMagnitude > NearRange * NearRange) continue;
|
||||
|
||||
Quaternion rel = Quaternion.Inverse(h.Body.rotation) * h.Rb.rotation;
|
||||
if (h.HasSent && Quaternion.Angle(rel, h.SentRot) < MinAngle) continue;
|
||||
h.SentRot = rel;
|
||||
float angle = h.Joint.angle;
|
||||
if (float.IsNaN(angle) || float.IsInfinity(angle)) continue;
|
||||
if (h.HasSent && Mathf.Abs(angle - h.SentAngle) < MinAngle) continue;
|
||||
h.SentAngle = angle;
|
||||
h.HasSent = true;
|
||||
outgoing.Add(h);
|
||||
}
|
||||
@@ -77,14 +85,7 @@ namespace MSCO
|
||||
send(MsgType.HingeStates, NetSession.Build(w =>
|
||||
{
|
||||
w.Write(outgoing.Count);
|
||||
foreach (Hinge h in outgoing)
|
||||
{
|
||||
Vector3 p = h.Body.transform.InverseTransformPoint(h.Rb.position);
|
||||
Quaternion q = h.SentRot;
|
||||
w.Write(h.Key);
|
||||
w.Write(p.x); w.Write(p.y); w.Write(p.z);
|
||||
w.Write(q.x); w.Write(q.y); w.Write(q.z); w.Write(q.w);
|
||||
}
|
||||
foreach (Hinge h in outgoing) { w.Write(h.Key); w.Write(h.SentAngle); }
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -94,35 +95,57 @@ namespace MSCO
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
int key = r.ReadInt32();
|
||||
var p = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
var q = new Quaternion(r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
float angle = r.ReadSingle();
|
||||
Hinge h;
|
||||
if (!hinges.TryGetValue(key, out h)) { nextScan = 0f; continue; }
|
||||
if (float.IsNaN(p.x) || float.IsNaN(q.w) || p.sqrMagnitude > 25f) continue; // ein Scharnier ist nie weit weg
|
||||
h.TargetPos = p;
|
||||
h.TargetRot = q;
|
||||
if (float.IsNaN(angle) || float.IsInfinity(angle) || Mathf.Abs(angle) > 180f) continue;
|
||||
h.TargetAngle = angle;
|
||||
h.TargetTime = Time.time;
|
||||
h.SentRot = q; // nicht als eigene Bewegung zurückschicken
|
||||
h.SentAngle = angle; // nicht als eigene Bewegung zurückschicken
|
||||
h.HasSent = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Empfangene Stellungen halten (die Physik würde die Tür sonst beim Fahren weiterschwingen lassen).</summary>
|
||||
/// <summary>Feder des Scharniers auf den empfangenen Winkel stellen bzw. wieder zurückgeben.</summary>
|
||||
public void FixedUpdate()
|
||||
{
|
||||
foreach (Hinge h in hinges.Values)
|
||||
{
|
||||
if (Time.time - h.TargetTime > ApplyHold || h.Rb == null || h.Body == null) continue;
|
||||
Quaternion rot = h.Body.rotation * h.TargetRot;
|
||||
Vector3 pos = h.Body.transform.TransformPoint(h.TargetPos);
|
||||
float t = Mathf.Clamp01(Time.fixedDeltaTime * 15f);
|
||||
h.Rb.MoveRotation(Quaternion.Slerp(h.Rb.rotation, rot, t));
|
||||
h.Rb.MovePosition(Vector3.Lerp(h.Rb.position, pos, t));
|
||||
h.Rb.angularVelocity = Vector3.zero;
|
||||
h.Rb.velocity = h.Body.velocity;
|
||||
if (h.Joint == null || h.Rb == null) continue;
|
||||
bool fresh = Time.time - h.TargetTime < ApplyHold && !h.Rb.isKinematic;
|
||||
if (!fresh) { StopDriving(h); continue; }
|
||||
|
||||
if (!h.Driving)
|
||||
{
|
||||
h.OrigUseSpring = h.Joint.useSpring;
|
||||
h.OrigSpring = h.Joint.spring;
|
||||
h.Driving = true;
|
||||
}
|
||||
float target = h.TargetAngle;
|
||||
if (h.Joint.useLimits)
|
||||
{
|
||||
JointLimits l = h.Joint.limits;
|
||||
target = Mathf.Clamp(target, Mathf.Min(l.min, l.max), Mathf.Max(l.min, l.max));
|
||||
}
|
||||
JointSpring s = h.Joint.spring;
|
||||
s.spring = SpringForce;
|
||||
s.damper = SpringDamper;
|
||||
s.targetPosition = target;
|
||||
h.Joint.spring = s;
|
||||
h.Joint.useSpring = true;
|
||||
h.Rb.WakeUp();
|
||||
}
|
||||
}
|
||||
|
||||
static void StopDriving(Hinge h)
|
||||
{
|
||||
if (!h.Driving) return;
|
||||
h.Driving = false;
|
||||
if (h.Joint == null) return;
|
||||
h.Joint.spring = h.OrigSpring;
|
||||
h.Joint.useSpring = h.OrigUseSpring;
|
||||
}
|
||||
|
||||
void Scan()
|
||||
{
|
||||
int before = hinges.Count;
|
||||
@@ -132,17 +155,14 @@ namespace MSCO
|
||||
Rigidbody rb = j.GetComponent<Rigidbody>();
|
||||
Rigidbody body = j.connectedBody;
|
||||
if (rb == null || body == null || !IsVehicle(body.transform.root.name)) continue;
|
||||
int key = Hash(FsmSync.PathOf(j.transform));
|
||||
int key = Hash(FsmSync.PathOf(j.transform) + "|" + FsmSync.PathOf(body.transform));
|
||||
if (hinges.ContainsKey(key)) continue;
|
||||
hinges[key] = new Hinge { Key = key, Root = body.transform.root.name, Rb = rb, Body = body };
|
||||
hinges[key] = new Hinge { Key = key, Root = body.transform.root.name, Joint = j, Rb = rb };
|
||||
}
|
||||
// Abgebaute Teile (Scharnier weg) vergessen
|
||||
var gone = new List<int>();
|
||||
foreach (KeyValuePair<int, Hinge> kv in hinges)
|
||||
{
|
||||
Hinge h = kv.Value;
|
||||
if (h.Rb == null || h.Body == null || h.Rb.GetComponent<HingeJoint>() == null) gone.Add(kv.Key);
|
||||
}
|
||||
if (kv.Value.Joint == null || kv.Value.Rb == null) gone.Add(kv.Key);
|
||||
foreach (int k in gone) hinges.Remove(k);
|
||||
if (gone.Count > 0) seen.RemoveWhere(x => x == null);
|
||||
if (hinges.Count != before) MscoLog.Info("[MSCO] Vehicle doors/hood/trunk: " + hinges.Count + " synced");
|
||||
|
||||
Reference in New Issue
Block a user