Old 06-2-2008, 04:23 PM   #1
argo15
FFR Veteran
FFR Veteran
 
argo15's Avatar
 
Join Date: May 2006
Age: 33
Posts: 1,863
Default [High School - 3D Geometry and Programming]

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 is offline   Reply With Quote
Old 06-3-2008, 04:47 PM   #2
argo15
FFR Veteran
FFR Veteran
 
argo15's Avatar
 
Join Date: May 2006
Age: 33
Posts: 1,863
Default Re: [High School - 3D Geometry and Programming]

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.

Last edited by argo15; 06-3-2008 at 04:51 PM..
argo15 is offline   Reply With Quote
Old 06-3-2008, 04:50 PM   #3
-Barista-
FFR Player
 
-Barista-'s Avatar
 
Join Date: Dec 2007
Location: UnderYourBed, Tacoma
Posts: 342
Default Re: [High School - 3D Geometry and Programming]

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.
-Barista- is offline   Reply With Quote
Old 06-4-2008, 11:38 AM   #4
dooty_7
Registered User
FFR Veteran
 
dooty_7's Avatar
 
Join Date: Jun 2005
Location: Hamilton Ontario
Age: 35
Posts: 462
Default Re: [High School - 3D Geometry and Programming]

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...
__________________
dooty_7 is offline   Reply With Quote
Old 07-11-2008, 03:27 AM   #5
SpoOkyMagician
Forum User
FFR Veteran
 
Join Date: May 2004
Posts: 378
Default Re: [High School - 3D Geometry and Programming]

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.

Last edited by SpoOkyMagician; 07-11-2008 at 03:38 AM..
SpoOkyMagician is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution