106 lines
No EOL
4.4 KiB
C#
106 lines
No EOL
4.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using NUnit.Framework.Internal;
|
|
using TMPro;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
[System.Serializable]
|
|
public class RollerProperties : MonoBehaviour {
|
|
//settings
|
|
[SerializeField] public bool start = false;
|
|
[SerializeField] public bool clockwise = false;
|
|
[SerializeField] public GameObject linkTo = null;
|
|
[SerializeField] public RotationType movement = RotationType.Fixed;
|
|
[FormerlySerializedAs("fixedAttachmentPoint")] [SerializeField] public bool useFixedAttachmentPoint = false;
|
|
[SerializeField] public Vector2 attachmentPoint = new Vector2(0, 0);
|
|
|
|
//references
|
|
protected internal GameObject text;
|
|
protected internal DistanceJoint2D distanceJoint2D = null;
|
|
|
|
//simulation properties
|
|
protected internal float currentDistance;
|
|
|
|
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) {
|
|
|
|
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) {
|
|
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;
|
|
|
|
distanceJoint2D.connectedAnchor = right.Value;
|
|
}
|
|
|
|
|
|
return (leftSurfaceDist, rightSurfaceDist);
|
|
}
|
|
|
|
/* VISUALS */
|
|
private void Update() {
|
|
if (text != null) {
|
|
text.transform.position = transform.position;
|
|
//text.transform.rotation = transform.rotation;
|
|
text.GetComponent<TextMeshPro>().text =
|
|
gameObject.name + "\n" +
|
|
(clockwise ? "cw" : "ccw") + " - " + movement + "\n" +
|
|
"D: " + (distanceJoint2D == null ? " - "
|
|
: distanceJoint2D.distance.ToString("0.00")) + "M: "+GetComponent<Rigidbody2D>().mass.ToString("0.00") + "\n"+
|
|
"OrigD: "+currentDistance;
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
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
|
|
}
|
|
} |