From 20d2a9e6c3f992f3f3db0f4ee2b67f161eaaf29a Mon Sep 17 00:00:00 2001 From: Felix Klenner Date: Fri, 15 Dec 2023 15:08:53 +0100 Subject: [PATCH] partial restructuring towards allowing loops --- Assets/Controller.cs | 28 +++++++++++++--------------- Assets/Model.cs | 25 +++++++++++++++++++++++++ Assets/Model.cs.meta | 3 +++ Assets/RollerProperties.cs | 15 +++++---------- Assets/Scenes/MultipleCables.unity | 14 ++++++++++++++ Packages/manifest.json | 2 +- Packages/packages-lock.json | 2 +- 7 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 Assets/Model.cs create mode 100644 Assets/Model.cs.meta diff --git a/Assets/Controller.cs b/Assets/Controller.cs index 553252d..3ac3ca1 100644 --- a/Assets/Controller.cs +++ b/Assets/Controller.cs @@ -3,7 +3,10 @@ using TMPro; using UnityEngine; public class Controller : MonoBehaviour { - private List cableStartingPoints = new(); + + [Tooltip("List of cables in the scene. Each cable consists of segments describing the link and last/first can optionally be fixed to points.")] + [SerializeField] + public List cables = new(); //Initialize void Awake() { @@ -37,11 +40,9 @@ public class Controller : MonoBehaviour { //collisions and physics Rigidbody2D rb = go.GetOrAddComponent(); // mass is based on area, if not provided - if (rp.mass == null) { - rb.useAutoMass = rp.mass == null; - } - else { - rb.mass = rp.mass.Value; + rb.useAutoMass = rp.mass < 0; + if(rp.mass >= 0) { + rb.mass = rp.mass; } rb.angularDrag = 1; rb.drag = 1; @@ -50,9 +51,7 @@ public class Controller : MonoBehaviour { } go.AddComponent(); - - //build list of cable starting objects - cableStartingPoints.Add(rp); + Debug.Log("Initialized roller: " + go.name); } } @@ -73,9 +72,6 @@ public class Controller : MonoBehaviour { dist.autoConfigureDistance = false; rp.distanceJoint2D = dist; - //remove linked to objects, so only starting points remain - cableStartingPoints.Remove(rp.linkTo); - //set fixed points only once if (rp.useFixedAttachmentPoint) { dist.anchor = rp.attachmentPoint; @@ -126,7 +122,9 @@ public class Controller : MonoBehaviour { } } - Debug.Log("Initialized "+cableStartingPoints.Count+" cables"); + Debug.Log("Initialized "+cables.Count+" cables"); + + /* Define cables directly instead var cableNum = 0; //find and report loops of cables foreach (var cable in cableStartingPoints) { @@ -139,7 +137,7 @@ public class Controller : MonoBehaviour { } visitedSegments.Add(segment); } - } + }*/ } @@ -156,7 +154,7 @@ public class Controller : MonoBehaviour { } if (waitedFrames++ > -1) { - CableJointsAlgorithm.TimeStep(cableStartingPoints); + CableJointsAlgorithm.TimeStep(cables); waitedFrames = 0; } } diff --git a/Assets/Model.cs b/Assets/Model.cs new file mode 100644 index 0000000..076c3f9 --- /dev/null +++ b/Assets/Model.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using JetBrains.Annotations; +using UnityEngine; + +[Serializable] +public class Cable { + [SerializeField] + public List segments; +} + +[Serializable] +public class CableSegment { + [SerializeField] + public GameObject go; + //keep references to all of these, to reduce inefficient getComponent calls + protected RollerProperties rp = null; + protected RollerProperties linkToGO = null; + protected RollerProperties linkToRP = null; + protected DistanceJoint2D dist = null; + + [SerializeField] public bool useFixedAttachmentPoint = false; + + [SerializeField] public Vector2 attachmentPoint = new Vector2(0,0); +} \ No newline at end of file diff --git a/Assets/Model.cs.meta b/Assets/Model.cs.meta new file mode 100644 index 0000000..13e30de --- /dev/null +++ b/Assets/Model.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 35789436d28644f99ea0a7f2230944a1 +timeCreated: 1701891888 \ No newline at end of file diff --git a/Assets/RollerProperties.cs b/Assets/RollerProperties.cs index 47a00df..d5aec52 100644 --- a/Assets/RollerProperties.cs +++ b/Assets/RollerProperties.cs @@ -1,32 +1,27 @@ -using System.Collections.Generic; using TMPro; -using Unity.VisualScripting; using UnityEngine; using UnityEngine.Serialization; [System.Serializable] public class RollerProperties : MonoBehaviour { //settings - /// rotation direction, used to determine attachment sides for the cables + [Tooltip("rotation direction, used to determine attachment sides for the cables")] [SerializeField] public bool clockwise = false; - [SerializeField] public RollerProperties linkTo = null; - [SerializeField] public List listTest; - [SerializeField] public RollerProperties[] arrayTest; + [Tooltip("Type of movement of the object. Powered behaves like a wheel, which can be toggled with \"t\".")] [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; + [Tooltip("Mass of the object. Use -1 to automatically calculate based on area.")] + [SerializeField] public float mass = -1; //references protected internal GameObject text; protected internal DistanceJoint2D distanceJoint2D = null; - protected internal GameObject linkToGameObject => linkTo.gameObject; //simulation properties protected internal float actualDistance; public (float?, float?) updateDistanceJoints() { Vector2? left = null, right = null; + if (linkTo != null) { if (useFixedAttachmentPoint && !linkTo.useFixedAttachmentPoint) { //left = attachmentPoint; diff --git a/Assets/Scenes/MultipleCables.unity b/Assets/Scenes/MultipleCables.unity index 6ea01ef..e8943fb 100644 --- a/Assets/Scenes/MultipleCables.unity +++ b/Assets/Scenes/MultipleCables.unity @@ -393,6 +393,20 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d406720d26e7d1e4ba8afd140d67de7e, type: 3} m_Name: m_EditorClassIdentifier: + cables: + - firstRoller: {fileID: 0} + segments: + - go: {fileID: 0} + fixedAttachmentPoint: 0 + attachmentPoint7: {x: 0, y: 0} + - go: {fileID: 0} + fixedAttachmentPoint: 0 + attachmentPoint7: {x: 0, y: 0} + - go: {fileID: 0} + fixedAttachmentPoint: 0 + attachmentPoint7: {x: 0, y: 0} + cabletes: + - 0 --- !u!1 &1306628726 GameObject: m_ObjectHideFlags: 0 diff --git a/Packages/manifest.json b/Packages/manifest.json index 047e5e3..482ad81 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,7 +3,7 @@ "com.csutil.cscore": "https://github.com/cs-util-com/cscore.git?path=CsCore/PlainNetClassLib/src/Plugins", "com.unity.collab-proxy": "2.2.0", "com.unity.feature.2d": "2.0.0", - "com.unity.ide.rider": "3.0.26", + "com.unity.ide.rider": "3.0.27", "com.unity.test-framework": "1.3.9", "com.unity.textmeshpro": "3.0.6", "com.unity.timeline": "1.8.2", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 18c0132..44f94f9 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -151,7 +151,7 @@ } }, "com.unity.ide.rider": { - "version": "3.0.26", + "version": "3.0.27", "depth": 0, "source": "registry", "dependencies": {