   2D Ray to 2D Bounding Box
  i need a formula that will allow me to check a single ray against a bounding box. I've tried working this out in my head but I just can't seem to think of a viable solution. I know there has to be some easy formula to this so any help would be appreciated.Thanks.   
Quote:Original post by ChaosPhoenixi need a formula that will allow me to check a single ray against a bounding box. I've tried working this out in my head but I just can't seem to think of a viable solution. I know there has to be some easy formula to this so any help would be appreciated.Thanks.Yup, there are a couple of methods that are fairly easy to implement, but we need to know a) whether you need the actual points of intersection or just a boolean result, and b) whether the bounding box is oriented or axis-aligned.   ;Quote:Original post by jykQuote:Original post by ChaosPhoenixi need a formula that will allow me to check a single ray against a bounding box. I've tried working this out in my head but I just can't seem to think of a viable solution. I know there has to be some easy formula to this so any help would be appreciated.Thanks.Yup, there are a couple of methods that are fairly easy to implement, but we need to know a) whether you need the actual points of intersection or just a boolean result, and b) whether the bounding box is oriented or axis-aligned.Sure. I just need a boolean value and the bounding boxes are axis-aligned.   ;Quote:Original post by ChaosPhoenixSure. I just need a boolean value and the bounding boxes are axis-aligned.A simple application of the separating axis theorem should do the trick. There are only three axes to test: the cardinal x and y axes, and the normal to the ray direction vector. It can  be done with just a few lines of code. I don't know of any references that cover this particular test (2D ray vs. AABB), so if you're unfamiliar with the SAT or need further info, feel free to ask.   ;Quote:Original post by jykQuote:Original post by ChaosPhoenixSure. I just need a boolean value and the bounding boxes are axis-aligned.A simple application of the separating axis theorem should do the trick. There are only three axes to test: the cardinal x and y axes, and the normal to the ray direction vector. It can  be done with just a few lines of code. I don't know of any references that cover this particular test (2D ray vs. AABB), so if you're unfamiliar with the SAT or need further info, feel free to ask.I am unfamiliar with separating axis theorem. I figured I would need a directional normal so I store the ray as (start position translated a little*rotation) - start position, which should give me a vector aiming in the correct direction. Normalize that and that's where I'm stuck at.   ;Quote:Original post by ChaosPhoenixI am unfamiliar with separating axis theorem. I figured I would need a directional normal so I store the ray as (start position translated a little*rotation) - start position, which should give me a vector aiming in the correct direction. Normalize that and that's where I'm stuck at.We'll just start with what you described above. It sounds like you're deriving a direction vector for the ray from an angle. The easiest way to do this is:vector2 direction;direction.x = cos(angle);direction.y = sin(angle);// Remember that 'angle' must be in radiansThis is probably equivalent to what you're doing, but note that normalization is not required.The normal can then be derived as follows:vector2 normal;normal.x = -direction.y;normal.y = direction.xGotta run now, but I'll try to fill in the rest of the details of the SAT ray-AABB test later on (unless someone else does it first, of course :).   ;  I'll take stab.Seperating axis theorem says that for two convex objects that are not intersecting you can find a line(2d) that seperates the two objects. When you find the line, if you project the points of your convex objects onto that line perpendicular to it, the intervals of the two shapes will not overlap. Draw it out on a piece of paper to see for yourself.There are two things you need to do.1) Find the Seperating Axis2) Project the pointsFor 1, on a convex object in 2d you can just use the edge normals of your box (or a half width, and half height vector), and for your other object the ray, you can use the line perpendicular to that ray.For part 2, project the box and the ray on to all of the three potential seperating axis. If one of the axis has non overlapping intervals, of the box and the ray, then you've found they don't intersect.It's kind of hard to project the two endpoints of a ray because a ray doesn't have a second endpoint. You can either make it a segment with a very far off distance, but this can run into numerical problems, or you can project the ray's beginning point an its point one unit away, and then change your interval test to check for overlap in the direction the ray points.If you need more detail on any of these steps first check N-Tutorials for picture explanation, and then come back and ask for specifics.- Dave   ;  I came across that N-Tutorial site before, but it really doesn't explain how you compare the AABBPos and Vectors to your Normal vector. That's what I'm most confused on. Basically all I know right now is that I have a normal pointing in the direction of my ray and AABBs that are defined:vector Positionvector HalfHeight(0,ObjectHeight/2)vector HalfWidth(ObjectWidth/2,0)I'm unsure as how to project the ray. Should I calculate the distance between the AABB position and the Ray starting point, scale the Ray starting point by that value and then test to see if it falls within the box?   ;What I've got in this picture is how you should check your interval range. If you project the ray's origin and some arbitrary point on the ray, onto the line you can then find out if you should be testing against positive or negative infinity.If this isn't clear then just ask and I can help ya out with the implementation details.   ;  lol, yea, you lost me after the you took the Vector Pos, and then added the Vector Normal to it to get your arbitrary testing position.I kindof get what you are showing in your diagram, but am still confused. :)   