2023-11-17 16:09:21 +01:00
|
|
|
using TMPro;
|
2023-11-16 12:14:12 +01:00
|
|
|
using UnityEngine;
|
2023-11-17 16:09:21 +01:00
|
|
|
using UnityEngine.Serialization;
|
2023-11-16 12:14:12 +01:00
|
|
|
|
2023-11-17 16:09:21 +01:00
|
|
|
[System.Serializable]
|
2023-11-16 12:14:12 +01:00
|
|
|
public class RollerProperties : MonoBehaviour {
|
2023-11-19 15:45:08 +01:00
|
|
|
//settings
|
2023-11-17 16:09:21 +01:00
|
|
|
[SerializeField] public bool start = false;
|
|
|
|
[SerializeField] public bool clockwise = false;
|
|
|
|
[SerializeField] public GameObject linkTo = null;
|
|
|
|
[SerializeField] public RotationType movement = RotationType.Fixed;
|
2023-11-19 15:45:08 +01:00
|
|
|
[FormerlySerializedAs("fixedAttachmentPoint")] [SerializeField] public bool useFixedAttachmentPoint = false;
|
|
|
|
[SerializeField] public Vector2 attachmentPoint = new Vector2(0, 0);
|
2023-11-17 16:09:21 +01:00
|
|
|
|
2023-11-19 15:45:08 +01:00
|
|
|
//references
|
|
|
|
protected internal GameObject text;
|
|
|
|
protected internal DistanceJoint2D distanceJoint2D = null;
|
|
|
|
|
|
|
|
//simulation properties
|
2023-11-30 09:19:37 +01:00
|
|
|
protected internal float actualDistance;
|
2023-11-17 16:09:21 +01:00
|
|
|
|
2023-11-19 15:45:08 +01:00
|
|
|
public (float?, float?) updateDistanceJoints() {
|
|
|
|
Vector2? left = null, right = null;
|
|
|
|
|
|
|
|
if (linkTo != null) {
|
|
|
|
if (useFixedAttachmentPoint && !linkTo.GetComponent<RollerProperties>().useFixedAttachmentPoint) {
|
|
|
|
//left = attachmentPoint;
|
|
|
|
right = CableJointsAlgorithm.TangentPointCircle(gameObject, linkTo);
|
|
|
|
}
|
|
|
|
else if (!useFixedAttachmentPoint && linkTo.GetComponent<RollerProperties>().useFixedAttachmentPoint) {
|
|
|
|
left = CableJointsAlgorithm.TangentPointCircle(linkTo,gameObject);
|
|
|
|
//right = linkTo.GetComponent<RollerProperties>().attachmentPoint;
|
|
|
|
}
|
|
|
|
else if (!useFixedAttachmentPoint && !linkTo.GetComponent<RollerProperties>().useFixedAttachmentPoint) {
|
|
|
|
(left, right) = CableJointsAlgorithm.TangentCircleCircle(gameObject, linkTo);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
else {
|
|
|
|
left = attachmentPoint;
|
|
|
|
right = linkTo.GetComponent<RollerProperties>().attachmentPoint;
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//TODO: assumes circles are flat for sufficiently small timesteps
|
|
|
|
float? leftSurfaceDist = null, rightSurfaceDist = null;
|
|
|
|
if (left.HasValue) {
|
|
|
|
|
2023-11-20 09:13:14 +01:00
|
|
|
leftSurfaceDist = CableJointsAlgorithm.CirclePointDistance(gameObject.transform.TransformPoint(distanceJoint2D.anchor), gameObject.transform.TransformPoint(left.Value), gameObject);
|
|
|
|
|
|
|
|
// leftSurfaceDist = (gameObject.transform.TransformPoint(distanceJoint2D.anchor) - gameObject.transform.TransformPoint(left.Value)).magnitude;
|
|
|
|
|
2023-11-19 15:45:08 +01:00
|
|
|
distanceJoint2D.anchor = left.Value;
|
|
|
|
}
|
|
|
|
if (right.HasValue) {
|
2023-11-20 09:13:14 +01:00
|
|
|
rightSurfaceDist = CableJointsAlgorithm.CirclePointDistance(linkTo.transform.TransformPoint(distanceJoint2D.connectedAnchor) , linkTo.transform.TransformPoint(right.Value), linkTo);
|
|
|
|
|
|
|
|
// rightSurfaceDist = (linkTo.transform.TransformPoint(distanceJoint2D.connectedAnchor) - linkTo.transform.TransformPoint(right.Value)).magnitude;
|
2023-11-19 15:45:08 +01:00
|
|
|
|
|
|
|
distanceJoint2D.connectedAnchor = right.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (leftSurfaceDist, rightSurfaceDist);
|
|
|
|
}
|
2023-11-17 16:09:21 +01:00
|
|
|
|
2023-11-19 15:45:08 +01:00
|
|
|
/* VISUALS */
|
2023-11-17 16:09:21 +01:00
|
|
|
private void Update() {
|
|
|
|
if (text != null) {
|
|
|
|
text.transform.position = transform.position;
|
2023-11-19 15:45:08 +01:00
|
|
|
//text.transform.rotation = transform.rotation;
|
2023-11-20 09:13:14 +01:00
|
|
|
text.GetComponent<TextMeshPro>().text =
|
|
|
|
gameObject.name + "\n" +
|
|
|
|
(clockwise ? "cw" : "ccw") + " - " + movement + "\n" +
|
2023-11-30 09:19:37 +01:00
|
|
|
" Mass: "+GetComponent<Rigidbody2D>().mass.ToString("0.00") +
|
|
|
|
" Dist: " + (distanceJoint2D == null ? " - "
|
|
|
|
: distanceJoint2D.distance.ToString("0.00") + "\n"+
|
|
|
|
"Slack: "+(distanceJoint2D.distance-actualDistance).ToString("0.00"));
|
|
|
|
|
2023-11-17 16:09:21 +01:00
|
|
|
}
|
2023-11-16 12:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
2023-11-17 16:09:21 +01:00
|
|
|
void OnDrawGizmos() {
|
|
|
|
foreach (var distanceJoint in gameObject.GetComponents<DistanceJoint2D>()) {
|
|
|
|
Gizmos.color = new Color(1, .3f, 0);
|
|
|
|
var cableStart = transform.TransformPoint(distanceJoint.anchor);
|
|
|
|
var cableEnd = distanceJoint.connectedBody.transform.TransformPoint(distanceJoint.connectedAnchor);
|
|
|
|
Gizmos.DrawLine(cableStart, cableEnd);
|
|
|
|
Gizmos.color = Color.yellow;
|
|
|
|
Gizmos.DrawCube(cableStart, new Vector3(0.1f, 0.1f, 0.1f));
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
Gizmos.DrawCube(cableEnd, new Vector3(0.1f, 0.1f, 0.1f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum RotationType {
|
|
|
|
Fixed,
|
|
|
|
Rotatable,
|
|
|
|
Powered,
|
|
|
|
Free
|
2023-11-16 12:14:12 +01:00
|
|
|
}
|
2023-11-17 16:09:21 +01:00
|
|
|
}
|