public CollisionDelegate(object data, Shape shape) { this.data = data; this.shape = shape; }
public virtual bool Collides(Shape shape, Shape.CollideType detectMethod = CollideType.All) { if (shape is Rect) return Collides(shape as Rect, detectMethod); if (shape is Point) return Collides(shape as Point, detectMethod); if (shape is Circle) return Collides(shape as Circle, detectMethod); throw new Exception("Unsupported IShape specialization."); }
public Vector2 DisallowIntersection(Shape shape, CollisionGroupIdentifier group, Vector2 Velocity, Shape.CollideType typeCheck = Shape.CollideType.All) { if (Velocity == Vector2.Zero) return shape.position; LinkedList<CollisionDelegate> delegates = m_Delegates[group]; List<CollisionDelegate> CollidingDelegates = new List<CollisionDelegate>(); Shape nextFrameShape = shape; Shape fixedPositionShape = shape; nextFrameShape.position += Velocity; Velocity.Normalize(); foreach (CollisionDelegate cd in delegates) { while (nextFrameShape.Collides(cd.shape, typeCheck)) { if (!fixedPositionShape.Collides(cd.shape, typeCheck)) { fixedPositionShape.position += Velocity; } else { fixedPositionShape.position -= Velocity; nextFrameShape.position = fixedPositionShape.position; } } } return nextFrameShape.position; }
/// <summary> /// Returns the first instance that is detected to collide with the given shape. /// </summary> /// <param name="shape">Shape to be checked against for collision.</param> /// <param name="typeCheck">Definition of what a "collision" is for this check. Optional.</param> /// <returns></returns> public CollisionDelegate CollisionCheckFirstObject(Shape shape, CollisionGroupIdentifier group, Shape.CollideType typeCheck = Shape.CollideType.All) { LinkedList<CollisionDelegate> delegates = m_Delegates[group]; CollisionDelegate firstInstance = new CollisionDelegate(); foreach (CollisionDelegate cd in delegates) { if (shape.Collides(cd.shape, typeCheck)) { firstInstance = cd; break; } } return firstInstance; }
public CollisionDelegate() { data = null; shape = null; }
/// <summary> /// Returns a list of collision delegates containing all delegates whose shapes met the collision check with the given shape. /// </summary> /// <param name="shape">Shape to be compared against for collision.</param> /// <param name="typeCheck">Definition of what a "collision" is for this check. Optional.</param> /// <returns></returns> public List<CollisionDelegate> CollisionCheckAllObjects(Shape shape, CollisionGroupIdentifier group, Shape.CollideType typeCheck = Shape.CollideType.All) { LinkedList<CollisionDelegate> delegates = m_Delegates[group]; List<CollisionDelegate> CollidingDelegates = new List<CollisionDelegate>(); foreach (CollisionDelegate cd in delegates) { if (shape.Collides(cd.shape, typeCheck)) { CollidingDelegates.Add(cd); } } return CollidingDelegates; }
/// <summary> /// Returns whether or not an instance collides with the given shape. /// </summary> /// <param name="shape">Shape to be checked against for collision.</param> /// <param name="typeCheck">Definition of what a "collision" is for this check. Optional.</param> /// <returns></returns> public bool CollisionCheck(Shape shape, CollisionGroupIdentifier group, Shape.CollideType typeCheck = Shape.CollideType.All) { LinkedList<CollisionDelegate> delegates = m_Delegates[group]; bool isColliding = false; foreach (CollisionDelegate cd in delegates) { if (shape.Collides(cd.shape, typeCheck)) isColliding = true; } return isColliding; }