AmbushedRaccoon

GeometryUtils from Grid Terrain Video

Jul 25th, 2021 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GeometryUtils {
  6.  
  7.     public static float LookRotationY2D(Vector2 start, Vector2 end)
  8.     {
  9.         return Quaternion.LookRotation(new Vector3(end.x, 0, end.y) - new Vector3(start.x, 0, start.y)).eulerAngles.y;
  10.     }
  11.  
  12.     public static float XZDistance(Vector3 point1, Vector3 point2)
  13.     {
  14.         Vector2 point12D = new Vector2(point1.x, point1.z);
  15.         Vector2 point22D = new Vector2(point2.x, point2.z);
  16.         return Vector2.Distance(point12D, point22D);
  17.     }
  18.  
  19.     public static GameObject DrawDebugCircle(Vector3 pos, Color color, string name = "Circle", float scale = 2f)
  20.     {
  21.         GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  22.         obj.name = name;
  23.         obj.transform.localScale = new Vector3(scale, scale, scale);
  24.         obj.GetComponent<Renderer>().material.color = color;
  25.         obj.transform.position = pos;
  26.         obj.layer = 2;
  27.         return obj;
  28.     }
  29.  
  30.    
  31.  
  32.     private static void set(ref ColliderRect p)
  33.     {
  34.         double temp;
  35.         if (p.a.x > p.b.x) { temp = p.a.x; p.a.x = p.b.x; p.b.x = temp; }
  36.         if (p.a.y > p.b.y) { temp = p.a.y; p.a.y = p.b.y; p.b.y = temp; }
  37.     }
  38.  
  39.     private static bool inside(ColliderRect p1, ColliderRect p2)
  40.     {
  41.         return (p2.a.x <= p1.a.x && p1.b.x <= p2.b.x &&
  42.             p2.a.y <= p1.a.y && p1.b.y <= p2.b.y ? true : false);
  43.     }
  44.  
  45.     public static  bool isPointInsideRectangle(Vector3 searchPoint, Vector3 rectPoint1, Vector3 rectPoint2)
  46.     {
  47.         ColliderRect p1 = new  ColliderRect( new ColliderRectPoint(searchPoint.x, searchPoint.z), new ColliderRectPoint(searchPoint.x, searchPoint.z) );
  48.         ColliderRect p2 = new ColliderRect(new ColliderRectPoint(rectPoint1.x, rectPoint1.z), new ColliderRectPoint(rectPoint2.x, rectPoint2.z));
  49.         set(ref p1);
  50.         set(ref p2);
  51.         return inside(p1, p2);
  52.     }
  53.  
  54.  
  55.     public static bool is_point_in_rectangle(List<Transform> rect, Vector3 p)
  56.     {
  57.         Vector2 P1= new Vector2(rect[0].position.x, rect[0].position.z);
  58.         Vector2 P2= new Vector2(rect[1].position.x, rect[1].position.z);
  59.         Vector2 P3= new Vector2(rect[2].position.x, rect[2].position.z);
  60.         Vector2 P4= new Vector2(rect[3].position.x, rect[3].position.z);
  61.         Vector2 P = new Vector3(p.x, p.z);
  62.  
  63.         Vector2 P1_P4 = P1 - P4;
  64.         Vector2 P3_P4 = P3 - P4;
  65.         Vector2 TWO_P_C = 2.0f * P - P1 - P3; // TWO_P_C=2P-C, C=Center of rectangle
  66.  
  67.         return (Vector2.Dot(P3_P4, TWO_P_C - P3_P4) <= 0 && Vector2.Dot(P3_P4, TWO_P_C + P3_P4) >= 0) &&
  68.         (Vector2.Dot(P1_P4, TWO_P_C - P1_P4) <= 0 && Vector2.Dot(P1_P4, TWO_P_C + P1_P4) >= 0);
  69.     }
  70.  
  71.  
  72.  
  73.  
  74. }
  75.  
  76.  
  77. struct ColliderRectPoint
  78. {
  79.     public  double x, y;
  80.     public ColliderRectPoint(double x, double y)
  81.     {
  82.         this.x = x;
  83.         this.y = y;
  84.     }
  85.  
  86. };
  87.  
  88. struct ColliderRect
  89. {
  90.     public ColliderRectPoint a, b;
  91.     public ColliderRect(ColliderRectPoint a, ColliderRectPoint b)
  92.     {
  93.         this.a = a;
  94.         this.b = b;
  95.     }
  96.  
  97. };
  98.  
Add Comment
Please, Sign In to add comment