public Collision CheckCollision(CollisionBox box) { int direction = -1; float coord = 0f; if (IsColliding(box)) { var leftDist = Math.Abs(left - box.right); var rightDist = Math.Abs(right - box.left); var upDist = Math.Abs(up - box.down); var downDist = Math.Abs(down - box.up); var minDist = new [] { leftDist, rightDist, upDist, downDist }.Min(); if (minDist == leftDist) { direction = (int)Direction.Left; coord = box.right; } else if (minDist == rightDist) { direction = (int)Direction.Right; coord = box.left; } else if (minDist == upDist) { direction = (int)Direction.Up; coord = box.down; } else if (minDist == downDist) { direction = (int)Direction.Down; coord = box.up; } return(new Collision((Direction)direction, coord)); } return(null); }
public bool IsColliding(CollisionBox box) { return(!(left > box.right || right < box.left || up > box.down || down < box.up)); }