Vector2D and Box classes in monogame? Trying to determine if line segment intersects with rotated rectangle.

Hi, I am trying to incorporate this code into my program:
http://pastebin.com/qy40VfcR

The purpose of it is to determine if line segment intersects with rotated rectangle and output the length of the segment before it hits the rectangle.

The code originated in this thread:
http://forums.tigsource.com/index.php?topic=1083.0

The problem is this function uses classes Vector2D and Box. Is there any way to replace or access these classes with monogame? I am running it on visual studio 2013.

In addition to Vector2D and Box, the class Ray doesn’t seem to work right, because ray.orig doesnt exist.

It sounds like you might find a lot of use incorporating Cocos2D into your project.

Say you have line segment L1,L2 and a polygon B1,B2,B3,B4.(those are points/Vector2)

Subtract L1 from every point. Now L1 is (0,0). Everything else is translated to L1 origin.
Extract a rotation matrix from L2 and rotate everything (Math.atan2, Matrix.CreateRotationFromAxis, Vector2d.Transform). L1 is still (0,0). L1,L2 segment is aligned to the Y axis, L2.X is 0 & Abs(L2.Y) is the length.

You have just transformed the word to a new point of view that makes things much easier. The relative position between the line and the box is unchanged. Notice that if any segment of the box intersect the Y axis also intersect the ray of L1,L2. This is easy to test, if B(n).X <=0 and B(n+1).X >=0.
You now have to find where on the Y-axis is the intersection, the Y value for where the segment B(n),B(n+1) has X=0. If it falls within the line segment length (=L2.Y) you have an intersection. Just use the last part from the pastebin code.

1 Like