partial restructuring towards allowing loops

This commit is contained in:
Felix Klenner 2023-12-15 15:08:53 +01:00
parent d6428dba3e
commit 20d2a9e6c3
7 changed files with 62 additions and 27 deletions

View file

@ -3,7 +3,10 @@ using TMPro;
using UnityEngine; using UnityEngine;
public class Controller : MonoBehaviour { public class Controller : MonoBehaviour {
private List<RollerProperties> 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<Cable> cables = new();
//Initialize //Initialize
void Awake() { void Awake() {
@ -37,11 +40,9 @@ public class Controller : MonoBehaviour {
//collisions and physics //collisions and physics
Rigidbody2D rb = go.GetOrAddComponent<Rigidbody2D>(); Rigidbody2D rb = go.GetOrAddComponent<Rigidbody2D>();
// mass is based on area, if not provided // mass is based on area, if not provided
if (rp.mass == null) { rb.useAutoMass = rp.mass < 0;
rb.useAutoMass = rp.mass == null; if(rp.mass >= 0) {
} rb.mass = rp.mass;
else {
rb.mass = rp.mass.Value;
} }
rb.angularDrag = 1; rb.angularDrag = 1;
rb.drag = 1; rb.drag = 1;
@ -50,9 +51,7 @@ public class Controller : MonoBehaviour {
} }
go.AddComponent<PolygonCollider2D>(); go.AddComponent<PolygonCollider2D>();
//build list of cable starting objects
cableStartingPoints.Add(rp);
Debug.Log("Initialized roller: " + go.name); Debug.Log("Initialized roller: " + go.name);
} }
} }
@ -73,9 +72,6 @@ public class Controller : MonoBehaviour {
dist.autoConfigureDistance = false; dist.autoConfigureDistance = false;
rp.distanceJoint2D = dist; rp.distanceJoint2D = dist;
//remove linked to objects, so only starting points remain
cableStartingPoints.Remove(rp.linkTo);
//set fixed points only once //set fixed points only once
if (rp.useFixedAttachmentPoint) { if (rp.useFixedAttachmentPoint) {
dist.anchor = rp.attachmentPoint; 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; var cableNum = 0;
//find and report loops of cables //find and report loops of cables
foreach (var cable in cableStartingPoints) { foreach (var cable in cableStartingPoints) {
@ -139,7 +137,7 @@ public class Controller : MonoBehaviour {
} }
visitedSegments.Add(segment); visitedSegments.Add(segment);
} }
} }*/
} }
@ -156,7 +154,7 @@ public class Controller : MonoBehaviour {
} }
if (waitedFrames++ > -1) { if (waitedFrames++ > -1) {
CableJointsAlgorithm.TimeStep(cableStartingPoints); CableJointsAlgorithm.TimeStep(cables);
waitedFrames = 0; waitedFrames = 0;
} }
} }

25
Assets/Model.cs Normal file
View file

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
[Serializable]
public class Cable {
[SerializeField]
public List<CableSegment> 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);
}

3
Assets/Model.cs.meta Normal file
View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 35789436d28644f99ea0a7f2230944a1
timeCreated: 1701891888

View file

@ -1,32 +1,27 @@
using System.Collections.Generic;
using TMPro; using TMPro;
using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
[System.Serializable] [System.Serializable]
public class RollerProperties : MonoBehaviour { public class RollerProperties : MonoBehaviour {
//settings //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 bool clockwise = false;
[SerializeField] public RollerProperties linkTo = null; [Tooltip("Type of movement of the object. Powered behaves like a wheel, which can be toggled with \"t\".")]
[SerializeField] public List<RollerProperties> listTest;
[SerializeField] public RollerProperties[] arrayTest;
[SerializeField] public RotationType movement = RotationType.Fixed; [SerializeField] public RotationType movement = RotationType.Fixed;
[FormerlySerializedAs("fixedAttachmentPoint")] [SerializeField] public bool useFixedAttachmentPoint = false; [Tooltip("Mass of the object. Use -1 to automatically calculate based on area.")]
[SerializeField] public Vector2 attachmentPoint = new Vector2(0, 0); [SerializeField] public float mass = -1;
[SerializeField] public float? mass = null;
//references //references
protected internal GameObject text; protected internal GameObject text;
protected internal DistanceJoint2D distanceJoint2D = null; protected internal DistanceJoint2D distanceJoint2D = null;
protected internal GameObject linkToGameObject => linkTo.gameObject;
//simulation properties //simulation properties
protected internal float actualDistance; protected internal float actualDistance;
public (float?, float?) updateDistanceJoints() { public (float?, float?) updateDistanceJoints() {
Vector2? left = null, right = null; Vector2? left = null, right = null;
if (linkTo != null) { if (linkTo != null) {
if (useFixedAttachmentPoint && !linkTo.useFixedAttachmentPoint) { if (useFixedAttachmentPoint && !linkTo.useFixedAttachmentPoint) {
//left = attachmentPoint; //left = attachmentPoint;

View file

@ -393,6 +393,20 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d406720d26e7d1e4ba8afd140d67de7e, type: 3} m_Script: {fileID: 11500000, guid: d406720d26e7d1e4ba8afd140d67de7e, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: 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 --- !u!1 &1306628726
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -3,7 +3,7 @@
"com.csutil.cscore": "https://github.com/cs-util-com/cscore.git?path=CsCore/PlainNetClassLib/src/Plugins", "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.collab-proxy": "2.2.0",
"com.unity.feature.2d": "2.0.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.test-framework": "1.3.9",
"com.unity.textmeshpro": "3.0.6", "com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.8.2", "com.unity.timeline": "1.8.2",

View file

@ -151,7 +151,7 @@
} }
}, },
"com.unity.ide.rider": { "com.unity.ide.rider": {
"version": "3.0.26", "version": "3.0.27",
"depth": 0, "depth": 0,
"source": "registry", "source": "registry",
"dependencies": { "dependencies": {