Ray Intersection

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

Recommended Posts

Well I found a piece of code, that proccesses ray to sphere intersection. But I am not sure I understand it completely. I have tested it with a ray that has origin in 0 and direction (1,0,0). The sphere (_bs) has a position of (5,0,0) and a radious of 1.5. With this setup it fails at test 3. Wich I dont see why it should. What am I doing wrong?...
public final boolean intersect(Ray ray) {

float dx = _ps.get_position().getX() - ray.get_origin().getX();
float dy = _ps.get_position().getY() - ray.get_origin().getY();
float dz = _ps.get_position().getZ() - ray.get_origin().getZ();

float v = Vector3.dot(ray.get_direction(), new Vector3(dx, dy, dz));

// Do the following quick check to see if there is even a chance
// that an intersection here might be closer than a previous one
return false;
// Test if the ray actually intersects the sphere
double t = this._radSqr + v*v - dx*dx - dy*dy - dz*dz;
if (t < 0)
return false;

// Test if the intersection is in the positive
// ray direction and it is the closest so far
t = v - ((float) Math.sqrt(t));
System.out.println("hum"+ t);
if ((t > ray.getT()) || (t < 0))
return false;

ray.setT((float)t);
//	ray._asterHit= this;

return true;

}



Share on other sites
im too lazy to completely check your code, but this is what i use for sphere intersection:

	public bit SolidHitTrace(Ray ray){		//relative distance		ray.pos.x = Void.World.Position.x - ray.pos.x;		ray.pos.y = Void.World.Position.y - ray.pos.y;		ray.pos.z = Void.World.Position.z - ray.pos.z;				//sphere check		float A, B, C, D;		A = (ray.dir.x*ray.dir.x + ray.dir.y*ray.dir.y + ray.dir.z*ray.dir.z);		B = (ray.pos.x*ray.dir.x + ray.pos.y*ray.dir.y + ray.pos.z*ray.dir.z) * 2;		C = (ray.pos.x*ray.pos.x + ray.pos.y*ray.pos.y + ray.pos.z*ray.pos.z) - (Radius*Radius);		D = (B*B) - (4*A*C);				if (D >= 0){			if (ray.infinite){				if ((B * fabs(B) + D > 0)) return true;			}else{				C = B - 2 * A;				//if    not behind ray    &&    not too far away				if ((B * fabs(B) + D > 0) && (C * fabs(C) - D < 0)) return true;			}		}		return false;	}

if the ray is infinite it means it is checked for t=0..infinity, non infinite means a linesegment from t=0..1, usefull for shadowrays for example.

the rest should all be pretty obvious.

note how i used my l33t math skills to remove all sqrts [grin]

Share on other sites
cool stuff

I want to

1) Either check with an infinite ray and get the point on the ray where it intersects with the sphere

2)use a limited ray and get the point of intersection?.. can I do that?

Share on other sites
I'm using
//// Ray-sphere intersection routine. // p=(ray origin position - sphere position),// d=ray direction,// r=sphere radius,// i1=first intersection distance,// i2=second intersection distance// i1<=i2// i1>=0// returns true if intersection found,false otherwise.// bool RaySphereIntersect(const Vec3d &p, const Vec3d &d,real64 r, real64 &i1, real64 &i2){	real64 det,b;	b = -DotProd(p,d);	det=(b*b) - (p.x*p.x + p.y*p.y + p.z*p.z) + r*r;	if (det<0){		return false;	}//else	det= sqrt(det);	i1= b - det;	i2= b + det;	// intersecting with ray?	if(i2<0) return false;	if(i1<0)i1=0;	return true;};

clean and readable :).I prefer not to pass two parameters if i can just pass their difference.

Share on other sites
Ah so first and second intersection distance is the ray punching through the sphere in both sided?

Share on other sites
yes,exactly :) Also it returns zero for first intersection if you're inside sphere. I really used both results :)