Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6.  
  7. namespace Aphelion.Entities
  8. {
  9.     [Serializable]
  10.     public abstract class CollisionObject
  11.     {
  12.         public abstract Vector2 Intersects(PhysicsObject physicsObj);
  13.  
  14.         public static bool PointIntersectsTriangle(Vector2 testPoint, Vector2 trianglePoint1, Vector2 trianglePoint2, Vector2 trianglePoint3) // Thanks to Glenn Slayden of StackOverflow for this solution
  15.         {
  16.             float s = trianglePoint1.Y * trianglePoint3.X - trianglePoint1.X * trianglePoint3.Y + (trianglePoint3.Y - trianglePoint1.Y) * testPoint.X + (trianglePoint1.X - trianglePoint3.X) * testPoint.Y;
  17.             float t = trianglePoint1.X * trianglePoint2.Y - trianglePoint1.Y * trianglePoint2.X + (trianglePoint1.Y - trianglePoint2.Y) * testPoint.X + (trianglePoint2.X - trianglePoint1.X) * testPoint.Y;
  18.  
  19.             if ((s < 0) != (t < 0))
  20.             {
  21.                 return false;
  22.             }
  23.  
  24.             float A = -trianglePoint2.Y * trianglePoint3.X + trianglePoint1.Y * (trianglePoint3.X - trianglePoint2.X) + trianglePoint1.X * (trianglePoint2.Y - trianglePoint3.Y) + trianglePoint2.X * trianglePoint3.Y;
  25.  
  26.             if (A < 0.0)
  27.             {
  28.                 s = -s;
  29.                 t = -t;
  30.                 A = -A;
  31.             }
  32.  
  33.             return s > 0 && t > 0 && (s + t) < A;
  34.         }
  35.     }
  36.  
  37.     [Serializable]
  38.     public class Point : CollisionObject
  39.     {
  40.         public Vector2 Position;
  41.  
  42.         public Point(Vector2 position)
  43.         {
  44.             Position = position;
  45.         }
  46.  
  47.         public bool Intersects(PhysicsObject physicsObj)
  48.         {
  49.             Point point = physicsObj.CollisionObj as Point;
  50.             Triangle triangle = physicsObj.CollisionObj as Triangle;
  51.             ComplexShape complexShape = physicsObj.CollisionObj as ComplexShape;
  52.             Circle circle = physicsObj.CollisionObj as Circle;
  53.  
  54.             if (point != null)
  55.             {
  56.                 return point.Position == Position;
  57.             }
  58.             else if (triangle != null)
  59.             {
  60.                 return CollisionObject.PointIntersectsTriangle(Position, triangle.Points[0], triangle.Points[1], triangle.Points[2]);
  61.             }
  62.             else if (complexShape != null)
  63.             {
  64.                 foreach (Triangle complexShapeTriangle in complexShape.Triangles)
  65.                 {
  66.                     if (CollisionObject.PointIntersectsTriangle(Position, complexShapeTriangle.Points[0], complexShapeTriangle.Points[1], complexShapeTriangle.Points[2]))
  67.                     {
  68.                         return true;
  69.                     }
  70.                 }
  71.             }
  72.             else if (circle != null)
  73.             {
  74.                 return Vector2.Distance(Position, circle.Position) <= circle.Radius;
  75.             }
  76.  
  77.             return false;
  78.         }
  79.     }
  80.  
  81.     [Serializable]
  82.     public class Triangle : CollisionObject
  83.     {
  84.         public Vector2[] Points;
  85.  
  86.         public Triangle(Vector2 point1, Vector2 point2, Vector2 point3)
  87.         {
  88.             Points = new Vector2[] { point1, point2, point3 };
  89.         }
  90.  
  91.         public bool Intersects(PhysicsObject physicsObj)
  92.         {
  93.             Point point = physicsObj.CollisionObj as Point;
  94.             Triangle triangle = physicsObj.CollisionObj as Triangle;
  95.             ComplexShape complexShape = physicsObj.CollisionObj as ComplexShape;
  96.             Circle circle = physicsObj.CollisionObj as Circle;
  97.  
  98.             if (point != null)
  99.             {
  100.             }
  101.             else if (triangle != null)
  102.             {
  103.             }
  104.             else if (complexShape != null)
  105.             {
  106.             }
  107.             else if (circle != null)
  108.             {
  109.             }
  110.  
  111.             return false;
  112.         }
  113.     }
  114.  
  115.     [Serializable]
  116.     public class ComplexShape : CollisionObject
  117.     {
  118.         public Triangle[] Triangles;
  119.  
  120.         public ComplexShape(params Triangle[] triangles)
  121.         {
  122.             Triangles = triangles;
  123.         }
  124.     }
  125.  
  126.     [Serializable]
  127.     public class Circle : CollisionObject
  128.     {
  129.         public Vector2 Position;
  130.         public float Radius;
  131.  
  132.         public Circle(Vector2 position, float radius)
  133.         {
  134.             Position = position;
  135.             Radius = radius;
  136.         }
  137.     }
  138.  
  139.     // To do: Add line segments
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement