Triangle Intersection

Started by
4 comments, last by Wavewash 20 years, 8 months ago
Hey Gang, I''m a little confused as to how to go about this problem. I have a model in memory that I have multiple instances of. Instead of storing the model many times I just store the position and create a world matrix and use that when I render the model. Now the problem is that I (the user) can move all over the world and I want to beable to click when I see a model and check for intersection with it. But I''m lost to how I should transform my position and direction of the ray I emit. I could test the ray and position as it is against the points in the model after all of them have been transferred to a new location but this seems like a lot more math then necessary. I think I can transform the ray position and ray direction relative to the model''s position at 0,0,0. But I''m not sure how. Does anyone understand or was it a bad explanation? Any help would be appreciated. ~Wave
Advertisement
I''ll probably get lost trying to explain this...

Convert the co-ordinates of the mouse cursor on screen into a direction in space (originating at the eyepoint), where the center of the screen would come to (0,0,z). Then, rotate this vector by the camera-transform matrix, and test the result ray against your geometry.
Yep, I have that part down. I am able to generate a ray from a mouse click. The part that I''m having trouble with is transforming that ray so that I may test it against the geometry in memory.

I think I''ll just take the distance of the model from 0,0,0 and then move the ray that distance. Thanks for the help
~Wave
When you first create the ray, do so in *worldspace*, not in camera space. Now, since you want to test the ray against the original model, you need to convert it from worldspace to the object's local space . This is simply done by transforming the ray with the inverse local transformation matrix.

Pseudocode:
// Unproject the ray in worldspacefvector3 R = ComputeWorldspaceRayOrigin();fvector3 D = ComputeWorldspaceRayDirection();D.normalize();// Assume you want to test against all your instances (objects derived from a common base)for( O in all objects ) {   // Get the matrix that transforms the geometry to the worldspace location of the current object   fmatrix4 M1 = O.LocalMatrix;   // Invert it: now M1 will transform from worldspace to local space   M1.invert();   // Transform the ray origin to local object space   RL = R * M1;   // First isolate the rotationpart of the 4x4 matrix in a 3x3 matrix   // then multiply with the direction vector, to yield the ray's direction in local space   DL = D * fmatrix3(M1);     DL.normalize();     // should be redundant, if your matrix inversion is accurate.   // Now you can safely intersect the ray with the local object geometry or bounding box.   // ...}


In practice, you would store the local matrix inverse with the object, and invert them at loadtime (as it can be a costly operation, you should avoid doing it every frame).


[edited by - Yann L on August 10, 2003 9:32:22 AM]
Heh, I reckon that''s what I was trying to say, except for my complete lack of knowledge regarding math!

Yann, to create the ray in worldspace, surely it is necessary to create it in cameraspace and then transform it to get to worldspace? ''bout time I dusted off the algebra manual...
Ahhh Thankyou so much Yann L. That really helps me out. It''s what I was thinking I just didn''t know how to do it. Thanks again!
~Wave

This topic is closed to new replies.

Advertisement