views:

268

answers:

2

I'm creating a 2D game and want to test for collision between an OBB (Oriented Bounding Box) and a Circle. I'm unsure of the maths and code to do this. I'm creating the game in c++ and opengl.

+6  A: 

Since both your shapes are convex, you can use the Separating Axis Theorem. Here's a tutorial on how to implement an algorithm to do this.

Essentially, you try to find if it's possible to put a line somewhere that's between the two shapes and if you can't find one, then you know they're colliding.

References and general answer taken from this question.

Ben S
That explains it well but I am still unsure how to work out what I need.
Chris
A: 

Here's what I would do, in pseudocode:

function does_line_go_through_circle (original_line, circle_centerpoint, radius):
    original_slope = get_slope_of_line (original_line)
    perpendicular_slope = 1/original_slope
    perpendicular_line = create_line_with_slope_through_point (perpendicular_slope, circle_centerpoint)
    intersect_point = intersection_of_infinite_lines (perpendicular_line, original_line)
    if point_is_on_line (intersect_point, original_line):
        finite_line_along_radius = create_finite_line_between_points (circle_centerpoint, intersect_point)
        if length_of_line (finite_line_along_radius) < length_of_line (radius):
            return true
        end
    end
    return false
end

function does_box_intersect_with_circle (bounding_box, circle):
    for each side in bounding_box:
        if does_line_go_through_circle (side, circle.center, circle.radius):
            return true
        end
    end
    return false
end

Keep in mind, I'm a little rusty on this stuff, I might be wrong.

Anyway, it should be trivial to implement this in C++.

Max E.