How do I test for collision of two moving 2d oriented bounding boxes?

前端 未结 5 1596
抹茶落季
抹茶落季 2021-02-10 01:31

The OBBs have a position(x,y), a velocity(x,y) and an orientation(Matrix). Given periodic updates, the OBBs must collide with each other, returning the fraction of the move tha

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-10 01:59

    If you have two bounding boxes (i.e. rectangles) with some arbitrary orientation (which I assume means some "rotation"), then I would do the following:

    • Starting at an initial position (where I assume the bounding boxes are not intersecting), translate each box forward based on its velocity (however you're applying movements over time).
    • Find the coordinates of the corners of each translated bounding box. These 4 coordinates define the endpoints of the 4 line segments that make up the edges of the bounding box.
    • For bounding box #1, test for an intersection between each of its line segments and the 4 line segments of bounding box #2. You can do this using standard equations for computing the intersection point of two lines, as discussed here, for example.
    • If there was an intersection, figure out the fraction of a move that was successful using the coordinates of the intersection points and the known translation you applied.
    • Repeat the above steps for each update.

    EDIT: A rough pseudocode (incorporating what was discussed in the comments) would look as follows:

    ...test for intersections between the OBB edges...
    if any intersections are found{
        ...run code for when OBBs are partially overlapping...
    }else{
        P = line segment whose endpoints are the OBB centers;
        ...test for intersections between P and OBB edges...
        if P intersects edges of both OBBs{
            ...run code for when OBBs are not touching...
        }else{
            ...run code for when one OBB is completely inside the other...
        }
    }
    

提交回复
热议问题