# ray - plane intersection problem (fixed)

This topic is 4172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, Im still trudging through the swamp of colision detection, and ive run into a strange one. The collision point of a ray with a plane is offset in the y axis by what looks like one opengl unit. I have coded a test system which leaves a trail of purple dots, one per frame, to show where the ray colided with the plane, and it follows the angle of the plane accordingly, but all dots are slightly above the plane. Here is the function im using:
int ray_vs_poly(ray * r, polygon * P, g_vector & contact_point)
{
/*
t=distance along ray
Pn = plane normal
Po = plane origin (any point of the plane works but the origin is calculated already
Rs = ray start
Rd = ray direction

t=(Pn.(Po - Rs)) / (Pn.Rd);

*/
double t = 0;
double D = 0; //dot product between ray vector and plane vector
plane p = P->p;

//work out D first to see if the ray and plane dont intersect, i.e. they are parrallell
D = dot_product(p.normal,r->direction);
if(in_range(D, -0.01, 0.01)){ //slight angle between plane and ray so they are almost parrallell
printf("no collision\n");
return 0;

};

t = dot_product(p.normal,(p.origin-r->start)) / D;

contact_point = r->start +(r->direction*t);

return 1;
};


im out of ideas for how this could happen. Has anybody got an idea for whats going wrong? the maths is still looking a little like greek to me. [Edited by - speciesUnknown on June 14, 2007 2:04:31 PM]

##### Share on other sites
can anybody spot a mistake in the code sample i poseted above? if not, the error could be in my plane equation:

plane create_plane(vertex v1, vertex v2, vertex v3)//create a plane for the given triangle//pass the first three vertices if you dont have a triangle{	//normal: cross product of (v1-v2)*(v1-v3)		g_vector V1,V2,V3;	V1.set_vector(v1.coords[0],v1.coords[1],v1.coords[2]);	V2.set_vector(v2.coords[0],v2.coords[1],v2.coords[2]);	V3.set_vector(v3.coords[0],v3.coords[1],v3.coords[2]);	//convert the vertices into vectors			plane p;	p.normal=cross_product(V1-V2,V1-V3);	p.normal.normalize();			float A,B,C,x,y,z;	p.normal.get_vector(A,B,C);	V1.get_vector(x,y,z);					p.D= 0-(A*x + B*y + C*z);	return p;};

or in my cross product code:
g_vector cross_product(g_vector a, g_vector b){	float Az,Bz,Rz,Ay,By,Ry,Ax,Bx,Rx;	g_vector r;	a.get_vector(Ax,Ay,Az);	b.get_vector(Bx,By,Bz);	Rx = (Ay * Bz) - (Az * By);	Ry = (Az * Bx) - (Ax * Bz);	Rz = (Ax * By) - (Ay * Bx);	r.set_vector(Rx,Ry,Rz);	return r;};

or in the dot product code:
float dot_product(g_vector a, g_vector b){	float x,X,y,Y,z,Z;	float product;	a.get_vector(x,y,z);	b.get_vector(X,Y,Z);	product = (x*X)+(y*Y)+(z*Z);	return product;};

##### Share on other sites
it ok, i fixed it myself!

If you look at my second post you will see that i didnt calculate the plane origin, so the plane was defaulting to origin 0,0,0 which is in the constructor for my vector class.

the algorythms now work upon brief testing, with the origin of the plane being the first vertex of the shape it is calculated from.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011315
• ### Who's Online (See full list)

There are no registered users currently online

×