public bool Insert(ICollidableGameObject obj) { if (!Boundary.IntersectsWith(obj.Collider)) { return(false); } if (objectsInQuad.Count < maxCapacity) { objectsInQuad.Add(obj); return(true); } if (!divided) { Subdivide(); } if (northWest.Insert(obj)) { return(true); } if (northEast.Insert(obj)) { return(true); } if (southWest.Insert(obj)) { return(true); } if (southEast.Insert(obj)) { return(true); } return(false); }
public bool Collision(ICollidableGameObject testingObject) { List <ICollidableGameObject> objectsInRange = game.QuadTree.FindObjectsInRange(testingObject); ObjectsInRange = objectsInRange; CollidingObjects = CheckCollision(objectsInRange, testingObject); if (CollidingObjects.Count > 0) { CollidingObject = CollidingObjects[0]; return(true); } return(false); }
public List <ICollidableGameObject> FindObjectsInRange(ICollidableGameObject range) { List <ICollidableGameObject> objectsInRange = new List <ICollidableGameObject>(); if (!Boundary.IntersectsWith(range.Collider)) { return(objectsInRange); } objectsInRange.AddRange(objectsInQuad); if (divided) { objectsInRange.AddRange(northWest.FindObjectsInRange(range)); objectsInRange.AddRange(northEast.FindObjectsInRange(range)); objectsInRange.AddRange(southWest.FindObjectsInRange(range)); objectsInRange.AddRange(southEast.FindObjectsInRange(range)); } return(objectsInRange); }
public BoxCollider(ICollidableGameObject target, GeneralTransform transform) { Position = target.Position; Y = Position.Y; X = Position.X; Height = target.Model.Height; Width = target.Model.Width; Center = transform.Transform(new Point(Width / 2, Height / 2)); TopLeft = transform.Transform(new Point()); TopRight = transform.Transform(new Point(target.Model.Width, 0)); BottomLeft = transform.Transform(new Point(0, target.Model.Height)); BottomRight = transform.Transform(new Point(target.Model.Width, target.Model.Height)); float minX = (float)Math.Min(Math.Min(TopLeft.X, TopRight.X), Math.Min(BottomLeft.X, BottomRight.X)); float minY = (float)Math.Min(Math.Min(TopLeft.Y, TopRight.Y), Math.Min(BottomLeft.Y, BottomRight.Y)); float maxX = (float)Math.Max(Math.Max(TopLeft.X, TopRight.X), Math.Max(BottomLeft.X, BottomRight.X)); float maxY = (float)Math.Max(Math.Max(TopLeft.Y, TopRight.Y), Math.Max(BottomLeft.Y, BottomRight.Y)); RectCollider = new Rect(minX, minY, (maxX - minX), (maxY - minY)); }
private List <ICollidableGameObject> CheckCollision(List <ICollidableGameObject> objects, ICollidableGameObject testingObject) { List <ICollidableGameObject> collidingObjects = new List <ICollidableGameObject>(); foreach (var obj in objects) { if (obj.Collider.IntersectsWith(testingObject.Collider)) { collidingObjects.Add(obj); } } return(collidingObjects); }
public CirlceCollider(ICollidableGameObject target, GeneralTransform transform, Point centerOfCollider, double radius) { Center = centerOfCollider; Position = Center; Radius = radius; }