Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GeometryUtils {
- public static float LookRotationY2D(Vector2 start, Vector2 end)
- {
- return Quaternion.LookRotation(new Vector3(end.x, 0, end.y) - new Vector3(start.x, 0, start.y)).eulerAngles.y;
- }
- public static float XZDistance(Vector3 point1, Vector3 point2)
- {
- Vector2 point12D = new Vector2(point1.x, point1.z);
- Vector2 point22D = new Vector2(point2.x, point2.z);
- return Vector2.Distance(point12D, point22D);
- }
- public static GameObject DrawDebugCircle(Vector3 pos, Color color, string name = "Circle", float scale = 2f)
- {
- GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- obj.name = name;
- obj.transform.localScale = new Vector3(scale, scale, scale);
- obj.GetComponent<Renderer>().material.color = color;
- obj.transform.position = pos;
- obj.layer = 2;
- return obj;
- }
- private static void set(ref ColliderRect p)
- {
- double temp;
- if (p.a.x > p.b.x) { temp = p.a.x; p.a.x = p.b.x; p.b.x = temp; }
- if (p.a.y > p.b.y) { temp = p.a.y; p.a.y = p.b.y; p.b.y = temp; }
- }
- private static bool inside(ColliderRect p1, ColliderRect p2)
- {
- return (p2.a.x <= p1.a.x && p1.b.x <= p2.b.x &&
- p2.a.y <= p1.a.y && p1.b.y <= p2.b.y ? true : false);
- }
- public static bool isPointInsideRectangle(Vector3 searchPoint, Vector3 rectPoint1, Vector3 rectPoint2)
- {
- ColliderRect p1 = new ColliderRect( new ColliderRectPoint(searchPoint.x, searchPoint.z), new ColliderRectPoint(searchPoint.x, searchPoint.z) );
- ColliderRect p2 = new ColliderRect(new ColliderRectPoint(rectPoint1.x, rectPoint1.z), new ColliderRectPoint(rectPoint2.x, rectPoint2.z));
- set(ref p1);
- set(ref p2);
- return inside(p1, p2);
- }
- public static bool is_point_in_rectangle(List<Transform> rect, Vector3 p)
- {
- Vector2 P1= new Vector2(rect[0].position.x, rect[0].position.z);
- Vector2 P2= new Vector2(rect[1].position.x, rect[1].position.z);
- Vector2 P3= new Vector2(rect[2].position.x, rect[2].position.z);
- Vector2 P4= new Vector2(rect[3].position.x, rect[3].position.z);
- Vector2 P = new Vector3(p.x, p.z);
- Vector2 P1_P4 = P1 - P4;
- Vector2 P3_P4 = P3 - P4;
- Vector2 TWO_P_C = 2.0f * P - P1 - P3; // TWO_P_C=2P-C, C=Center of rectangle
- return (Vector2.Dot(P3_P4, TWO_P_C - P3_P4) <= 0 && Vector2.Dot(P3_P4, TWO_P_C + P3_P4) >= 0) &&
- (Vector2.Dot(P1_P4, TWO_P_C - P1_P4) <= 0 && Vector2.Dot(P1_P4, TWO_P_C + P1_P4) >= 0);
- }
- }
- struct ColliderRectPoint
- {
- public double x, y;
- public ColliderRectPoint(double x, double y)
- {
- this.x = x;
- this.y = y;
- }
- };
- struct ColliderRect
- {
- public ColliderRectPoint a, b;
- public ColliderRect(ColliderRectPoint a, ColliderRectPoint b)
- {
- this.a = a;
- this.b = b;
- }
- };
Add Comment
Please, Sign In to add comment