Seminar_Cable_Joints_Unity/Assets/RollerProperties.cs

102 lines
4.3 KiB
C#
Raw Normal View History

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 {
//settings
[Tooltip("rotation direction, used to determine attachment sides for the cables")]
2023-11-17 16:09:21 +01:00
[SerializeField] public bool clockwise = false;
[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;
[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
//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
public (float?, float?) updateDistanceJoints() {
Vector2? left = null, right = null;
if (linkTo != null) {
2023-12-04 08:26:05 +01:00
if (useFixedAttachmentPoint && !linkTo.useFixedAttachmentPoint) {
//left = attachmentPoint;
2023-12-04 08:26:05 +01:00
right = CableJointsAlgorithm.TangentPointCircle(gameObject, linkToGameObject);
}
2023-12-04 08:26:05 +01:00
else if (!useFixedAttachmentPoint && linkTo.useFixedAttachmentPoint) {
left = CableJointsAlgorithm.TangentPointCircle(linkToGameObject,gameObject);
//right = linkTo.GetComponent<RollerProperties>().attachmentPoint;
}
2023-12-04 08:26:05 +01:00
else if (!useFixedAttachmentPoint && !linkTo.useFixedAttachmentPoint) {
(left, right) = CableJointsAlgorithm.TangentCircleCircle(gameObject, linkToGameObject);
}
/*
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;
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;
distanceJoint2D.connectedAnchor = right.Value;
}
return (leftSurfaceDist, rightSurfaceDist);
}
2023-11-17 16:09:21 +01:00
/* VISUALS */
2023-11-17 16:09:21 +01:00
private void Update() {
if (text != null) {
text.transform.position = transform.position;
//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
}