Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- namespace Aphelion.Entities
- {
- [Serializable]
- public abstract class CollisionObject
- {
- public abstract Vector2 Intersects(PhysicsObject physicsObj);
- public static bool PointIntersectsTriangle(Vector2 testPoint, Vector2 trianglePoint1, Vector2 trianglePoint2, Vector2 trianglePoint3) // Thanks to Glenn Slayden of StackOverflow for this solution
- {
- float s = trianglePoint1.Y * trianglePoint3.X - trianglePoint1.X * trianglePoint3.Y + (trianglePoint3.Y - trianglePoint1.Y) * testPoint.X + (trianglePoint1.X - trianglePoint3.X) * testPoint.Y;
- float t = trianglePoint1.X * trianglePoint2.Y - trianglePoint1.Y * trianglePoint2.X + (trianglePoint1.Y - trianglePoint2.Y) * testPoint.X + (trianglePoint2.X - trianglePoint1.X) * testPoint.Y;
- if ((s < 0) != (t < 0))
- {
- return false;
- }
- float A = -trianglePoint2.Y * trianglePoint3.X + trianglePoint1.Y * (trianglePoint3.X - trianglePoint2.X) + trianglePoint1.X * (trianglePoint2.Y - trianglePoint3.Y) + trianglePoint2.X * trianglePoint3.Y;
- if (A < 0.0)
- {
- s = -s;
- t = -t;
- A = -A;
- }
- return s > 0 && t > 0 && (s + t) < A;
- }
- }
- [Serializable]
- public class Point : CollisionObject
- {
- public Vector2 Position;
- public Point(Vector2 position)
- {
- Position = position;
- }
- public bool Intersects(PhysicsObject physicsObj)
- {
- Point point = physicsObj.CollisionObj as Point;
- Triangle triangle = physicsObj.CollisionObj as Triangle;
- ComplexShape complexShape = physicsObj.CollisionObj as ComplexShape;
- Circle circle = physicsObj.CollisionObj as Circle;
- if (point != null)
- {
- return point.Position == Position;
- }
- else if (triangle != null)
- {
- return CollisionObject.PointIntersectsTriangle(Position, triangle.Points[0], triangle.Points[1], triangle.Points[2]);
- }
- else if (complexShape != null)
- {
- foreach (Triangle complexShapeTriangle in complexShape.Triangles)
- {
- if (CollisionObject.PointIntersectsTriangle(Position, complexShapeTriangle.Points[0], complexShapeTriangle.Points[1], complexShapeTriangle.Points[2]))
- {
- return true;
- }
- }
- }
- else if (circle != null)
- {
- return Vector2.Distance(Position, circle.Position) <= circle.Radius;
- }
- return false;
- }
- }
- [Serializable]
- public class Triangle : CollisionObject
- {
- public Vector2[] Points;
- public Triangle(Vector2 point1, Vector2 point2, Vector2 point3)
- {
- Points = new Vector2[] { point1, point2, point3 };
- }
- public bool Intersects(PhysicsObject physicsObj)
- {
- Point point = physicsObj.CollisionObj as Point;
- Triangle triangle = physicsObj.CollisionObj as Triangle;
- ComplexShape complexShape = physicsObj.CollisionObj as ComplexShape;
- Circle circle = physicsObj.CollisionObj as Circle;
- if (point != null)
- {
- }
- else if (triangle != null)
- {
- }
- else if (complexShape != null)
- {
- }
- else if (circle != null)
- {
- }
- return false;
- }
- }
- [Serializable]
- public class ComplexShape : CollisionObject
- {
- public Triangle[] Triangles;
- public ComplexShape(params Triangle[] triangles)
- {
- Triangles = triangles;
- }
- }
- [Serializable]
- public class Circle : CollisionObject
- {
- public Vector2 Position;
- public float Radius;
- public Circle(Vector2 position, float radius)
- {
- Position = position;
- Radius = radius;
- }
- }
- // To do: Add line segments
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement