automatically find starting points of cables
This commit is contained in:
parent
0f4911a96e
commit
cd25f31a77
3 changed files with 17 additions and 13 deletions
|
@ -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*/
|
/** Cable Joints algorithm based on Paper by Matthias Müller, Nuttapong Chentanez, Stefan Jeschke and Miles Macklin*/
|
||||||
public static class CableJointsAlgorithm {
|
public static class CableJointsAlgorithm {
|
||||||
public static void TimeStep(List<Cable> cables) {
|
public static void TimeStep(List<GameObject> cableStartingPoints) {
|
||||||
//handle each cable individually
|
//handle each cable individually
|
||||||
foreach (var cable in cables) {
|
foreach (var cable in cableStartingPoints) {
|
||||||
float totalDist = 0;
|
float totalDist = 0;
|
||||||
|
|
||||||
//calculate new attachment points for all joints
|
//calculate new attachment points for all joints
|
||||||
for (var go = cable.firstRoller; go != null; go = go.GetComponent<RollerProperties>().linkTo) {
|
for (var go = cable; go != null; go = go.GetComponent<RollerProperties>().linkTo) {
|
||||||
if (go.GetComponent<RollerProperties>().linkTo != null) {
|
if (go.GetComponent<RollerProperties>().linkTo != null) {
|
||||||
var dist = go.GetComponent<DistanceJoint2D>();
|
var dist = go.GetComponent<DistanceJoint2D>();
|
||||||
var rp = go.GetComponent<RollerProperties>();
|
var rp = go.GetComponent<RollerProperties>();
|
||||||
|
|
|
@ -3,7 +3,7 @@ using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Controller : MonoBehaviour {
|
public class Controller : MonoBehaviour {
|
||||||
private List<Cable> cables = new();
|
private List<GameObject> cableStartingPoints = new();
|
||||||
|
|
||||||
//Initialize
|
//Initialize
|
||||||
void Awake() {
|
void Awake() {
|
||||||
|
@ -36,7 +36,13 @@ public class Controller : MonoBehaviour {
|
||||||
|
|
||||||
//collisions and physics
|
//collisions and physics
|
||||||
Rigidbody2D rb = go.GetOrAddComponent<Rigidbody2D>();
|
Rigidbody2D rb = go.GetOrAddComponent<Rigidbody2D>();
|
||||||
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.angularDrag = 1;
|
||||||
rb.drag = 1;
|
rb.drag = 1;
|
||||||
if (rp.movement == RollerProperties.RotationType.Fixed) {
|
if (rp.movement == RollerProperties.RotationType.Fixed) {
|
||||||
|
@ -45,8 +51,10 @@ public class Controller : MonoBehaviour {
|
||||||
|
|
||||||
go.AddComponent<PolygonCollider2D>();
|
go.AddComponent<PolygonCollider2D>();
|
||||||
|
|
||||||
if (rp.start) {
|
//build list of cable starting objects
|
||||||
cables.Add(new Cable { firstRoller = go });
|
cableStartingPoints.Add(go);
|
||||||
|
if (rp.linkTo != null) {
|
||||||
|
cableStartingPoints.Remove(rp.linkTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.Log("Initialized roller: " + go.name);
|
Debug.Log("Initialized roller: " + go.name);
|
||||||
|
@ -129,12 +137,8 @@ public class Controller : MonoBehaviour {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (waitedFrames++ > -1) {
|
if (waitedFrames++ > -1) {
|
||||||
CableJointsAlgorithm.TimeStep(cables);
|
CableJointsAlgorithm.TimeStep(cableStartingPoints);
|
||||||
waitedFrames = 0;
|
waitedFrames = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public class Cable {
|
|
||||||
public GameObject firstRoller;
|
|
||||||
}
|
}
|
|
@ -5,12 +5,12 @@ using UnityEngine.Serialization;
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class RollerProperties : MonoBehaviour {
|
public class RollerProperties : MonoBehaviour {
|
||||||
//settings
|
//settings
|
||||||
[SerializeField] public bool start = false;
|
|
||||||
[SerializeField] public bool clockwise = false;
|
[SerializeField] public bool clockwise = false;
|
||||||
[SerializeField] public GameObject linkTo = null;
|
[SerializeField] public GameObject linkTo = null;
|
||||||
[SerializeField] public RotationType movement = RotationType.Fixed;
|
[SerializeField] public RotationType movement = RotationType.Fixed;
|
||||||
[FormerlySerializedAs("fixedAttachmentPoint")] [SerializeField] public bool useFixedAttachmentPoint = false;
|
[FormerlySerializedAs("fixedAttachmentPoint")] [SerializeField] public bool useFixedAttachmentPoint = false;
|
||||||
[SerializeField] public Vector2 attachmentPoint = new Vector2(0, 0);
|
[SerializeField] public Vector2 attachmentPoint = new Vector2(0, 0);
|
||||||
|
[SerializeField] public float? mass = null;
|
||||||
|
|
||||||
//references
|
//references
|
||||||
protected internal GameObject text;
|
protected internal GameObject text;
|
||||||
|
|
Loading…
Reference in a new issue