Seminar_Cable_Joints_Unity/Assets/RollerProperties.cs

107 lines
4.6 KiB
C#
Raw Normal View History

2023-12-04 08:26:05 +01:00
using System.Collections.Generic;
2023-11-17 16:09:21 +01:00
using TMPro;
2023-12-04 08:26:05 +01:00
using Unity.VisualScripting;
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
2023-12-04 08:26:05 +01:00
/// 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-04 08:26:05 +01:00
[SerializeField] public RollerProperties linkTo = null;
[SerializeField] public List<RollerProperties> listTest;
[SerializeField] public RollerProperties[] arrayTest;
2023-11-17 16:09:21 +01:00
[SerializeField] public RotationType movement = RotationType.Fixed;
[FormerlySerializedAs("fixedAttachmentPoint")] [SerializeField] public bool useFixedAttachmentPoint = false;
[SerializeField] public Vector2 attachmentPoint = new Vector2(0, 0);
[SerializeField] public float? mass = null;
2023-11-17 16:09:21 +01:00
//references
protected internal GameObject text;
protected internal DistanceJoint2D distanceJoint2D = null;
2023-12-04 08:26:05 +01:00
protected internal GameObject linkToGameObject => linkTo.gameObject;
//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
}