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-12-15 15:08:53 +01:00
|
|
|
[Tooltip("rotation direction, used to determine attachment sides for the cables")]
|
2023-11-17 16:09:21 +01:00
|
|
|
[SerializeField] public bool clockwise = false;
|
2023-12-15 15:08:53 +01:00
|
|
|
[Tooltip("Type of movement of the object. Powered behaves like a wheel, which can be toggled with \"t\".")]
|
2023-11-17 16:09:21 +01:00
|
|
|
[SerializeField] public RotationType movement = RotationType.Fixed;
|
2023-12-15 15:08:53 +01:00
|
|
|
[Tooltip("Mass of the object. Use -1 to automatically calculate based on area.")]
|
|
|
|
[SerializeField] public float mass = -1;
|
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;
|
2023-12-15 15:08:53 +01:00
|
|
|
|
2023-11-19 15:45:08 +01:00
|
|
|
if (linkTo != null) {
|
2023-12-04 08:26:05 +01:00
|
|
|
if (useFixedAttachmentPoint && !linkTo.useFixedAttachmentPoint) {
|
2023-11-19 15:45:08 +01:00
|
|
|
//left = attachmentPoint;
|
2023-12-04 08:26:05 +01:00
|
|
|
right = CableJointsAlgorithm.TangentPointCircle(gameObject, linkToGameObject);
|
2023-11-19 15:45:08 +01:00
|
|
|
}
|
2023-12-04 08:26:05 +01:00
|
|
|
else if (!useFixedAttachmentPoint && linkTo.useFixedAttachmentPoint) {
|
|
|
|
left = CableJointsAlgorithm.TangentPointCircle(linkToGameObject,gameObject);
|
2023-11-19 15:45:08 +01:00
|
|
|
//right = linkTo.GetComponent<RollerProperties>().attachmentPoint;
|
|
|
|
}
|
2023-12-04 08:26:05 +01:00
|
|
|
else if (!useFixedAttachmentPoint && !linkTo.useFixedAttachmentPoint) {
|
|
|
|
(left, right) = CableJointsAlgorithm.TangentCircleCircle(gameObject, linkToGameObject);
|
2023-11-19 15:45:08 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
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-12-04 08:26:05 +01:00
|
|
|
rightSurfaceDist = CableJointsAlgorithm.CirclePointDistance(linkToGameObject.transform.TransformPoint(distanceJoint2D.connectedAnchor) , linkToGameObject.transform.TransformPoint(right.Value), linkToGameObject);
|
2023-11-20 09:13:14 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|