I have two squares drawn on the screen, how can I detect collision on the edges of both objects?

前端 未结 3 1006
天命终不由人
天命终不由人 2021-01-29 11:36

Right now, I can compare the X and Y to check for collision, but that\'s only if the two objects pass right through each other, on exactly the same X & Y pos. I need to chec

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 12:23

    If the rectangles can rotate, all you have to do is change your coordinate system so one of them is axis-aligned, and then check each of the vertices of the other one to see if it (the vertex) is inside the axis-aligned rectangle. You can change your coordinate system by rotating all the vertices of both rectangles around the same point. The angle of this rotation should be the negation of the angle of rotation of the rotated rectangle. E.g. if one rectangle is tilted by 13°, you would rotate the vertices of both rectangles by -13° around the same point.

提交回复
热议问题