-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHelpers2D.cs
104 lines (85 loc) · 4.09 KB
/
Helpers2D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace toxicFork.GUIHelpers {
public class Helpers2D : Helpers {
/// <summary>
/// Transforms a 2D point into the transform's local space.
/// </summary>
/// <param name="transform">The transform of the selected object.</param>
/// <param name="point">The world coordinates of the point.</param>
/// <returns>The point in the object's local space.</returns>
public static Vector2 TransformPoint(Transform transform, Vector2 point) {
var rotatedScaledPoint = TransformVector(transform, point);
var translatedRotatedScaledPoint = (Vector2) transform.position + rotatedScaledPoint;
return translatedRotatedScaledPoint;
}
public static Vector2 GetPerpendicularVector(Vector3 dir)
{
return new Vector2(-dir.y, dir.x);
}
/// <summary>
/// Transforms a 2D vector into the transform's local space.
/// </summary>
/// <param name="transform">The transform of the selected object.</param>
/// <param name="vector">The world-space vector to transform.</param>
/// <returns>The vector in the object's local space.</returns>
public static Vector2 TransformVector(Transform transform, Vector2 vector) {
var scaledPoint = Vector2.Scale(vector, transform.lossyScale);
var angle = transform.rotation.eulerAngles.z;
Vector2 rotatedScaledPoint = Quaternion.AngleAxis(angle, Vector3.forward)*scaledPoint;
return rotatedScaledPoint;
}
public static Vector2 InverseTransformPoint(Transform transform, Vector2 point) {
var rotatedScaledVector = point - (Vector2) transform.position;
return InverseTransformVector(transform, rotatedScaledVector);
}
public static Vector2 InverseTransformVector(Transform transform, Vector2 vector) {
var angle = transform.rotation.eulerAngles.z;
Vector2 scaledVector = Quaternion.AngleAxis(-angle, Vector3.forward)*vector;
var inverseTransformVector = Vector2.Scale(scaledVector, new Vector2(1/transform.lossyScale.x, 1/transform.lossyScale.y));
return inverseTransformVector;
}
public static float GetAngle(Vector2 direction) {
return Mathf.Rad2Deg*Mathf.Atan2(direction.y, direction.x);
}
public static Quaternion Rotate(float angle) {
return Quaternion.AngleAxis(angle, Vector3.forward);
}
public static Vector2 GetDirection(float angle) {
return Rotate(angle)*Vector3.right;
}
public static float DistanceToLine(Ray ray, Vector3 point) {
return Vector3.Cross(ray.direction, point - ray.origin).magnitude;
}
public static Vector2 ClosestPointToRay(Ray ray, Vector2 point) {
//http://pastie.org/1066490
var t = Vector2.Dot(point - (Vector2) ray.origin, ray.direction);
return ray.GetPoint(t);
}
public static Vector2 Intersect2DPlane(Ray ray) {
var d = -(Vector3.Dot(ray.origin, Vector3.back)+0)/Vector3.Dot(ray.direction, Vector3.back);
return ray.GetPoint(d);
}
#if UNITY_EDITOR
public static Vector2 GUIPointTo2DPosition(Vector2 position) {
return Intersect2DPlane(HandleUtility.GUIPointToWorldRay(position));
}
#endif
public static float DistanceAlongLine(Ray ray, Vector2 wantedPosition) {
var normalFromCenter = new Ray(ray.origin, new Vector2(-ray.direction.y, ray.direction.x));
var distance = DistanceToLine(normalFromCenter, wantedPosition);
var wantedDirection = (wantedPosition - (Vector2)ray.origin).normalized;
var dot = Vector2.Dot(wantedDirection, ray.direction);
if (dot < 0)
{
distance *= -1;
}
return distance;
}
public static Vector2 GetNormal(Vector2 direction) {
return new Vector2(-direction.y, direction.x);
}
}
}