5

I'm implementing my own Raycasting library to learn how it works. As a starting point I have a box that rotates and, using rays, detects the different obstacles that it encounters in its way. Worth mention: I have this working using the line AABB intersection method. The following image show how it works:

Line Intersection with AABB

My problem comes when I rotate one of the above walls and try to use the same method to check the collision of the rays:

Rotated Object collision

I've looking for a possible solution to correct that, using different algorithms like the Separating Axis Theorem and Cohen-Sutherland but I couldn't find anything that works similar to AABB intersection but using OBB. Any ideas how to round the problem in the second picture where it collides with the boundaries of the object when it's rotated? I'm using Unity and C#.

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44

1 Answers1

-1

I don't think you will find an algorithm similar to AABB for rotated objects. You could attempt to rotate the coordinate system (i.e. the entire world) before checking a rotated wall in order to force the wall to be axis-aligned, and then rotate the result back into the original coordinate system again, but in my opinion that would end up messier than decomposing your walls into lines and doing line-line intersection tests.

Modifying something like the Separating Axis Theorem could work, too - the ray is not a convex hull, so you cannot use the algorithm directly, but you can use similar principles: project the points of the wall onto a line perpendicular to the ray, and you will then be able to calculate the distances of the extreme ends of the wall to your ray, and so whether or not the wall overlaps the ray. With this information you should also be able to calculate the "shadows" you have in your screenshot. Whether this is easier than line-line intersection or not, though, you will have to see for yourself!

Logan Pickup
  • 2,294
  • 2
  • 22
  • 29