View Full Version : test for intersection in 3D

05-03-2010, 10:27 AM
Ok, I tried to explain this somewhat in the beginner's forum, but either this isn't a beginner's topic, or I didn't explain it very well.

Note the image below:


All I'm trying to do is compute whether the vector is intersecting a cell on the grid. The vector is computed using glUnProject (two calls) using the near and far clipping panes. This gives me a 3D vector that represents a line going through the mouse cursor towards the far clipping pane. I just want to determine which cell the vector intersects.

Problem is, the cell coordinates are all on the same plane. When I do a global rotate, the grid is rotated but I'm not sure how to tell what the actual coordinates are for each cell. Do I need to apply rotation matrices in some way?

Thanks for any responses.

Ilian Dinev
05-03-2010, 06:59 PM
Right now you have the grid rotated with a model-to-world matrix M1. You have retrieved the positions of two points from the yellow line, in worldspace, L1_A and L1_B.
Convert the line to the grid's space, and do simple 2D grid-test

Mat4x4 invM1 = inverse(M1);
vec3 gridspaceL1_A = invM1 * L1_A; // might require division by W
vec3 gridspaceL1_B = invM1 * L1_B;

vec3 gsL1_C = intersectLineWithPlane(gridspaceL1_A,gridspaceL1_B , Plane(0,0,1,0));

vec2 gridPlace = vec2(gsL1_C.x,gsL1_C.y)/fCellSize;
int x = int(gridPlace.x);
int y = int(gridPlace.y);

if(x>=0 &amp;&amp; x<10 &amp;&amp; y>=0 &amp;&amp; y<10){
printf("we hit the grid at xy %d:%d",x,y);

05-05-2010, 04:43 AM
Ilian, arent you assuming a parallel ray?

05-05-2010, 08:22 AM
Ok, I've almost got it. In the images below, assume that the top of the yellow line is where the mouse cursor is:


So, now I'm detecting intersections no problemo. But what I really want is to "shoot" a ray towards the mouse cursor (imagining that it is "in" the display) from the camera-eye. So, the bottom line is that in this following picture...


... the cell where the mouse cursor is, should be highlighted, and not where the current line intersects the grid.

Does that make sense?

Ilian Dinev
05-05-2010, 01:29 PM
I don't understand...
When there's perspective projection: A ray from the eye to the "mouse-position" is visible onscreen ... as a dot, never a line like this yellow one.

When it's ortho-projection, the input ray needs to be constructed in such a way, that it's again a dot. If he's in ortho-mode and constructing it so that it's such a yellow line , then he simply needs to construct it via the previously-mentioned correct way, execute the code I specified, and he'll get the required result.

In all cases, he just needs to construct the line that he needs (very easy), convert it from screenspace (or whatever-space) into modelspace, and do the very-simple grid intersection test.