Skip to main content
gamedev.net
🔒 Locked

Ray/Cube intersection question

Started by Jumpster Oct 11, 2000 at 2:55 PM 6 replies 6.3k views
Original Post
Jumpster
Jumpster
I have a ray originating in a cube. How do I determine which wall of the cube the ray hits? Do I HAVE to do an intersection test on each hyper-plane of the cube or is there an easier way? Regards, Jumpster
Regards,JumpsterSemper Fi
neocron
neocron

You could get the angle between each of the cube face normals and the ray. Discard the faces that can''t possibly intersect with the ray, then do a ray/plane intersection on each of the remaining faces.

Hope it helps.
Jumpster
Jumpster
Perhaps, but determining an angle between the origin and each plane is likely to be just as slow as intersection testing each of the planes. Even if it isn''t, seems like I would still (quite possibly) have to test up to 5 of the sides anyway (front, left, right, top and bottom). The only plane truly eliminated would be the behind the origin plane.

That seems like a waste to me. I seem to remember someone explaining it before (vaguely) in a post here once, but for some reason, I keep getting a time-out error whenever I do a search.

Also, I read in TCS''s writing in his Oct-tree tutorial something about Node/Line testing... but he doesn''t give the exact details (only a summary of his method)... Perhaps this may work for me, given an arbitrary distance greater than the size of the cube to define the end point for the test...?


Anyway, any other ideas? Thanks for any assistance and/or suggestions provided.

Regards,
Jumpster







Regards,JumpsterSemper Fi
paulcoz
paulcoz
If the ray origin and the cube were in the same coordinate system, you might be able to create vectors from the origin of the ray to the vertices of the walls and do a comparison of the gradients to find out which vertices the ray intersection lies between (and thus which plane it intersects with). You could run your intersection test on this plane - using the gradients might be cheaper than doing something with the normals or dot/cross, although this one is off the top of my head .

Paulcoz.

Edited by - paulcoz on October 12, 2000 2:59:31 AM
Jumpster
Jumpster
paulcoz:

I''m thinking something like this:

Extrapolate a vector from the ray: tst=O+D*dist where dist is the distance from min/max points in the box. This garuantees that the ray pierces at least one wall. O and D are origin/direction respectively.

Then: NewTst=O-tst; (reverse the direction of the ray)

Finally, determine the distance from NewTst to EACH plane normal. I''m thinking that the largest distance is the normal to the wall that would be struck.

Note: I don''t care where it was struck. Just that it WAS struck.

Is this basically what you''re thinking?

If not, could you give me an example?

thanks.

Regards,
Jumpster


Semper Fi
Regards,JumpsterSemper Fi
Jumpster
Jumpster
Does that seem reasonable?

I know the distance between two vectors has to have a sqrt() call but I think it can be eliminated because the sqrt() of 1 is still greater than the sqrt() of .99999...

Anyone?



Regards,
Jumpster


Semper Fi
Regards,JumpsterSemper Fi
paulcoz
paulcoz
Jumpster,

My idea was more of a compare operation than a calculation. This diagram is viewing the cube (V*) and ray origin (O) from above, with I (intersection):

@ = V5 is directly below V1

.............I
@*V1.....*.....*V2
.............|
.............|
.............|
.............*O


*V3................*V4

(ignore the dots - I had to add them to get this diagram to format correctly on the page )

V1 belongs to the plane that contains V1 & V2, as well as the plane that contains V1 & V3.

I was thinking you could create a vector from O to V1 (pick a point on the left hand side of any wall) and calculate a gradient using only the X and Z values of the vector purely. Do the same with a vector from O to V2 (on the right hand side of the wall).

Then check whether the gradient of the vector (X and Z only) made by the ray (which can use any point away from the origin on the ray) is > than V1, but less than V2. You then do the same test for the Y and Z gradients (using V1 and V5), checking to see if the ray gradient (now Y and Z too) is > than V5, but < than V1.

If the first test fails you can go straight to the next wall. You might still have to check all six faces but there is a chance you will find the correct wall before that.

As I said before this will only check for an intersection not find the intersection point, and you may need to adjust for the orientation of the cube first - that was what I meant when I said 'in the same coordinate system'. It is an easy and accurate test to do when the coordinates of V1 and V2 are relative to the origin of the ray (like they are when O is the origin in camera-space). If you do find you have to transform the points to another space first, the cost of doing it this way might not be worth it.

I hope this is helpful, or at least gives you some ideas.
Paulcoz.

Edited by - paulcoz on October 13, 2000 11:10:45 PM
warpexplorer
warpexplorer
I have a neat idea for finding which side of a cube a ray intersects.

First, make the ray always point in the same direction. Instead of rotating the ray, rotate the cube. So...now you have a ray going directly forward, and a cube rotated in some way around the origin of the ray. Now...the ray will intersect the side of the cube that...out of all of its vertices, none have a negative y(or z depending on coordinate system). If you don''t know what im talking about, try experimenting with a 2D square centered on 0,0 , rotate it and take out the sides that have points that have ys that are less than 0 and see whats left. The only problem is if you rotate it 45 to the right you will either intersect two sides or none (depending if you chose <=0 or <0).

If I find any neat ideas for finding where on the side it intercest i''ll let you know.

hope i helped

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.