Sign in to follow this  

Ray Intersection

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 =, 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
		if (v -_radius > ray.getT())
		   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._asterHit= this;
		return true;	

Share this post

Link to post
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;
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 this post

Link to post
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 this post

Link to post
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;
det= sqrt(det);
i1= b - det;
i2= b + det;
// intersecting with ray?
if(i2<0) return false;
return true;

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

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this