Detecting the Direction of a Collision

前端 未结 5 862
执笔经年
执笔经年 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

    Given r1 and r2 (r2 being stationary), first find the closest corner of r2 to r1. This point is (c1.x,c1.y) and imagine now you extend this out into two planes, one parallel to the x axis and one to the y.

    Now get the closest corner of r1 to r2 (call it c2) and use it in the following formula y = mx + b where b is c2.x and m is your vector. and x is c1.x

    So if y is greater than c1.y then it means at the point of x contact (width) you've already hit the top. If it's less, then you haven't hit it yet. Invert for bottom/top.

提交回复
热议问题