ray collision

Started by
2 comments, last by Zakwayda 18 years, 8 months ago
ok how do I test if a ray intersects a sphere. I have this book and it gives this example:
Quote: if the endpoints of a ray are (x1,y1,z1) and (x2,y2,z2) the first step is to parametrize the ray. x=x1 +(x2-x1)t=x1 +i*t y=y1 +(y2-y1)t=y1 +j*t z=z1 +(z2-z1)t=z1 +k*t where 0<=t<=1 A sphere at centre (l,m,n) of radius r is given by: (x-l)^2 +(y-m)^2 +(z-n)^2 =r^2 substuting for x,y and z gives the quadratic equation in t of the form at^2 +bt +c =0 where a=i^2 + j^2 + k^2 b=2*i(x1-l) + 2*j(y1-m) + 2*k(z1-n) c=l^2 + m^2 + n^2 + x1^2 + y1^2 + z1^2 + 2(-l*x1 - m*y1 -n*z1)-r^2
ok can someone please clear this up a little. for startes what the heck is i,j,k, t. and where is he getting them from?
Advertisement
Quote:ok can someone please clear this up a little. for startes what the heck is i,j,k, t. and where is he getting them from?
You wrote at the top of your post what i, j, and k are, namely: i = x2 - x1, j = y2 - y1, and k = z2 - z1. t is any (or all) values greater than zero. That is, given all different values for t, t >= 0, the (x, y, z) triplet of values you wrote at the start will take on all possible points on the ray. t = 0 gives the first point of the ray: (x1, y1, z1).

When you're inserting the equation(s) of the ray into the equation of the sphere and solving for t, you're finding the particular t value that corresponds to a point that lies both on the ray and on the sphere surface.

You're solving a quadratic equation, so you can get two solutions. That makes sense, because when a ray penetrates a sphere it will generally intersect in two points: the entry and exit points. If the quadratic has only one root, the ray is tangential to the sphere (ie. the entry and exit points are one and the same). If there are no roots, the ray does not intersect the sphere.

Hope this helps to make it a little bit clearer.
can someone please give an example with numbers.
theres got to be a faster way then finding the determinant.

[Edited by - Ksingh30 on August 19, 2005 1:03:29 PM]
I don't know how useful or intuitive an example with numbers would be, as it's really the theory that's important.

As for whether it's fast enough, this is a fairly standard method for finding the points of intersection of a linear component and a circle or sphere. If however you only need a boolean result, it can be done more cheaply.

This topic is closed to new replies.

Advertisement