Detecting the Direction of a Collision

前端 未结 5 869
执笔经年
执笔经年 2021-02-07 13:52

A square tile collides with another square tile. The bartender says...

I have:

  • The height, width, x, and y of both tiles.
  • The 2D vector of the mo
5条回答
  •  渐次进展
    2021-02-07 14:34

    Assuming you have a way to detect collisions, understanding which sides collided is straight forward. You simply need to examine the x and y positions of each square.

    Square1 : (x1, y1)
    Square2 : (x2, y2)

    I'll work from the assumption that the top left corner of your work area is (0,0) and that x values increase as you move right, and y values increase as you move down.

    With this in mind:

    If (x1 < x2), the right side of square 1 collided with the left side of square 2
    If (x1 > x2), the left side of square 1 collided with the right side of square 2
    if (y1 < y2), the bottom side of square 1 collided with the top side of square 2
    if (y1 > y2), the top side of square 1 collided with the bottom side of square 2

    I suggest you draw yourself a few pictures, and it should become clear to you.

提交回复
热议问题