- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
public void isCollideWith(gObj obj, ref bool xCollision, ref bool yCollision)
{
xCollision = (obj.bounds.Top <= this.bounds.Center.Y && this.bounds.Center.Y <= obj.bounds.Bottom) &&
(obj.bounds.Top > this.bounds.Top && obj.bounds.Top < this.bounds.Bottom) ||
(obj.bounds.Bottom > this.bounds.Top && obj.bounds.Bottom < this.bounds.Bottom);
yCollision = (obj.bounds.Left <= this.bounds.Center.X && this.bounds.Center.X <= obj.bounds.Right) &&
(obj.bounds.Left > this.bounds.Left && obj.bounds.Left < this.bounds.Right) ||
(obj.bounds.Right > this.bounds.Left && obj.bounds.Right < this.bounds.Right);
}
Вот так я проверяю произошло ли столкновения двух(obj и this) прямоугольных объектов.
В данном примере можно сделать так:
Rectangle.Intersect(new Rectangle(x1, y1, width1, height1), new Rectangle(x2, y2, width2, height2));