This site features a collection of common technical interview questions gathered by a group of programmers who have been through, and given, lots of technical interviews. There is an emphasis on C++ and game programming technical interviews, but most of the questions are relevant to any technical interview.
Headshot

How can you determine if a ray intersects a sphere? The more efficient your solution, the better.


1 Comment so far
Leave a comment

Given a Ray R: P(t)=P_orig + t*d
where R represent all the points that start to P_orig(a,b,c) and go in ‘d’ direction;
and given a sphere with center point Q (x,y,z) and the Radius lenght K
1) Find the vector from P_orig to Q:

V = Q – P_orig = [x-a,y-b,z-c]

2) find the lenght of V

||V||=sqrt((x-a)^2 + (y-b)^2 + (z-c)^2)

3) calculate il dot product beetwen vector V and vector d

V(dot)d = cos(theta)*||V||*||d|| = >
(V(dot)d)/(||V||*||d||) = cos(theta)

theta represent the angle beetwen d and V
4) Multiply cos(theta) by ||V|| the have the distance from Q to the ray

G= ||V||*cos(theta)

5) check: if the radius is equal less then the distance, there is a collision!

if (G<=K)
{
//Collision!
}



Leave a comment
If you are including code in your comment, place it within a <div class='code'></div> tag for better formatting.


Do you have a technical interview question you would like to submit? Some tips you would like to pass on? Just want to say hi? Feel free to contact us