PDA

View Full Version : [High School - 3D Geometry and Programming]


argo15
June 2nd, 2008, 05:23 PM
It's not exactly a high school subject, but for my senior year I convinced the math head to add in a new "independent computer science" course. I'm planning on making a simple FPS game using OpenGL throughout the semester, although during the summer I plan to do all of the research and theory for it.

Bassically, I need an algorithm to determine weather or not a line in 3D space, intersects with a sphere in 3d space. I'm using c++.



POINT point[3]; // Line point 1, Line point 2, Sphere center point
double radus; // Sphere Radius

point[0].x= ...
point[0].y= ...
point[0].z= ...
point[1].x= ...
point[1].y= ...
point[1].z= ...
point[2].x= ...
point[2].y= ...
point[2].z= ...
radius= ...

bool LineSphereCollision(POINT LineP1, POINT LineP2, POINT Center, double Radius)
{
//This is were I need help.
if (The line and sphere intersect)
return true;
else
return false;
}



My AP Calculus techer couldn't remember how to do it, and I've looked on google, didn't get exactly what I wanted on the first page so I quit and came here. =)
Please tell me if I made any basic coding mistakes also.

argo15
June 3rd, 2008, 05:47 PM
Alright, not many FFR math experts I see =P. After doing some research, I've found that this is the function I should use.


bool LineSphereCollision(POINT LineP1, POINT LineP2, POINT Center, double Radius)
{
/* It ends up making some kind of quadratic equation (ax^2 + bx + c) that when b^2-4*a*c >= 0 the line will intersect the sphere. First, the calculations for a, b, and c must be done. */

float a,b,c;
POINT d;

d.x = (LineP2.x - LineP1.x)^2
d.y = (LineP2.y - LineP1.y)^2
d.z = (LineP2.z - LineP1.z)^2
a = d.x * d.x + d.y * d.y + d.z * d.z;
b = 2 * ((d.x * (LineP1.x - Center.x)) + (d.y * (LineP1.y - Center.y)) + (d.z * (LineP1.z - Center.z)))
c = (Center.x * Center.x) + (Center.y * Center.y) + (Center.z * Center.z) + (LineP1.x * LineP1.x) + (LineP1.y * LineP1.y) + (LineP1.z * LineP1.z) - 2 * ((Center.x * LineP1.x) + (Center.y * LineP1.y) + (Center.z * LineP1.z)) - (Radius * Radius)

if ((b * b - 4 * a * c) >= 0)
return true;
else
return false;
}


A cookie to whoever can prove this correct or incorrect.

-Barista-
June 3rd, 2008, 05:50 PM
I can ask my friend if you have a few minutes / hours. Right now he's at DigiPen and he's doing some game programming. He might be able to help you.

dooty_7
June 4th, 2008, 12:38 PM
I think I am looking at this right way, All you need is the equation of the line in question and the sphere. The sphere equation takes the form (x-h)^2 + (y-j)^2 + (z-k)^2 = r^2 where the center of the sphere is given as (h,j,k). From here it should be simple geometry to calculate whether or not an intersection occurs...

spookymagician
July 11th, 2008, 04:27 AM
Very interesting topic... My main programming language is C++. Although, I do not have much experence with OpenGL... I'll try to help if I can. Lemme read this over a bit... (I'll edit in a sec.)

edit: ok I cant do this right now... It's 4am... My brain isnt in the thinking mood atm... But what I am reading so far (If I am understanding it correctly) it sounds correct... But I am not sure at the moment.