[High School - 3D Geometry and Programming]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • argo15
    FFR Veteran
    • May 2006
    • 1863

    #1

    [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
    FFR Veteran
    • May 2006
    • 1863

    #2
    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, 03:51 PM.

    Comment

    • -Barista-
      FFR Player
      • Dec 2007
      • 342

      #3
      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.


      Originally posted by lightdarkness
      lol i created ffr

      Comment

      • dooty_7
        Registered User
        • Jun 2005
        • 462

        #4
        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...

        Comment

        • SpoOkyMagician
          Forum User
          • May 2004
          • 378

          #5
          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, 02:38 AM.

          Comment

          Working...