View Full Version : ray intersection point

12-09-2011, 10:27 AM
hi, i did something like this:

i have some objects vertices stored in arrays verts[100][100][4] with verts[#][#][0] = x, verts[#][#][1] = y, verts[#][#][2] = z i also have normals but im not sure if i'll need though

i have a mouse function that shoots a ray when i click the screen and im sure that is working right. i used gluunproject to figure that out. then, i made functions for my sphere and cylinder objects that use the quadratic equation to see if a ray hits it. this is working. if i have some sphere at (5, 0, 0) and cylinder at (0,5, 0) it is tested and returns a hit with the ray.

now my problem is to find the point of intersection. i have run the ray direction vector, and origin vector into inverse transformation of the object so it is in "object coordinates" and i think my code is right for that. now i have to find the nearest point on my object that the ray intersects with.

i do not understand how to use my vertices and these ray vectors in object coordinates to find the nearest point ,or any point for that matter.

i assume i use some for loops to loop through my vertices but i dont understand what i compare it too.

im a noob at this stuff but have slowly gotten my way this far only to get stuck here for the past day.

can someone please explain what i should do now or even give some pseudocode. i'd really appreciate it

also i have the 't' value that returned from the quadratic equation. do i use that to get the point somehow?

12-09-2011, 03:49 PM
Hi sa0sin,
Since you are doing intersection with a couple of objects, you should use the minimum t value. So in pseudo code it is something along these lines.

#include <limits> //for numeric limits::infinity
float tmin = numeric_limits<float>::infinity();
float t = tmin;
for each object o
if(intersects(o, ray, &amp;t))
if(t<tmin) {
tmin = t;
end for

This gives you the nearest point t value. Now the point of intersection is simply

vec3 hit_point = ray.origin + tmin*ray.direction;