From cd25f31a7791e36a3604460904e727a20151192f Mon Sep 17 00:00:00 2001 From: Felix Klenner Date: Fri, 1 Dec 2023 18:18:34 +0100 Subject: [PATCH] automatically find starting points of cables --- Assets/CableJointsAlgorithm.cs | 6 +++--- Assets/Controller.cs | 22 +++++++++++++--------- Assets/RollerProperties.cs | 2 +- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Assets/CableJointsAlgorithm.cs b/Assets/CableJointsAlgorithm.cs index c85961e..f3953fb 100644 --- a/Assets/CableJointsAlgorithm.cs +++ b/Assets/CableJointsAlgorithm.cs @@ -5,13 +5,13 @@ using static System.Math; /** Cable Joints algorithm based on Paper by Matthias Müller, Nuttapong Chentanez, Stefan Jeschke and Miles Macklin*/ public static class CableJointsAlgorithm { - public static void TimeStep(List cables) { + public static void TimeStep(List cableStartingPoints) { //handle each cable individually - foreach (var cable in cables) { + foreach (var cable in cableStartingPoints) { float totalDist = 0; //calculate new attachment points for all joints - for (var go = cable.firstRoller; go != null; go = go.GetComponent().linkTo) { + for (var go = cable; go != null; go = go.GetComponent().linkTo) { if (go.GetComponent().linkTo != null) { var dist = go.GetComponent(); var rp = go.GetComponent(); diff --git a/Assets/Controller.cs b/Assets/Controller.cs index b61650f..ab5cdcc 100644 --- a/Assets/Controller.cs +++ b/Assets/Controller.cs @@ -3,7 +3,7 @@ using TMPro; using UnityEngine; public class Controller : MonoBehaviour { - private List cables = new(); + private List cableStartingPoints = new(); //Initialize void Awake() { @@ -36,7 +36,13 @@ public class Controller : MonoBehaviour { //collisions and physics Rigidbody2D rb = go.GetOrAddComponent(); - rb.useAutoMass = true; // mass is based on size + // mass is based on area, if not provided + if (rp.mass == null) { + rb.useAutoMass = rp.mass == null; + } + else { + rb.mass = rp.mass.Value; + } rb.angularDrag = 1; rb.drag = 1; if (rp.movement == RollerProperties.RotationType.Fixed) { @@ -45,8 +51,10 @@ public class Controller : MonoBehaviour { go.AddComponent(); - if (rp.start) { - cables.Add(new Cable { firstRoller = go }); + //build list of cable starting objects + cableStartingPoints.Add(go); + if (rp.linkTo != null) { + cableStartingPoints.Remove(rp.linkTo); } Debug.Log("Initialized roller: " + go.name); @@ -129,12 +137,8 @@ public class Controller : MonoBehaviour { } if (waitedFrames++ > -1) { - CableJointsAlgorithm.TimeStep(cables); + CableJointsAlgorithm.TimeStep(cableStartingPoints); waitedFrames = 0; } } -} - -public class Cable { - public GameObject firstRoller; } \ No newline at end of file diff --git a/Assets/RollerProperties.cs b/Assets/RollerProperties.cs index 2317304..6655884 100644 --- a/Assets/RollerProperties.cs +++ b/Assets/RollerProperties.cs @@ -5,12 +5,12 @@ using UnityEngine.Serialization; [System.Serializable] public class RollerProperties : MonoBehaviour { //settings - [SerializeField] public bool start = false; [SerializeField] public bool clockwise = false; [SerializeField] public GameObject linkTo = null; [SerializeField] public RotationType movement = RotationType.Fixed; [FormerlySerializedAs("fixedAttachmentPoint")] [SerializeField] public bool useFixedAttachmentPoint = false; [SerializeField] public Vector2 attachmentPoint = new Vector2(0, 0); + [SerializeField] public float? mass = null; //references protected internal GameObject text;