Opengl中的球体碰撞检测?

时间:2014-12-17 02:05:40

标签: c++ collision-detection collision

我正在尝试在Opengl中构建游戏。在我开始制作更好的运动机制之前,我想让碰撞工作。我有立方体碰撞工作,我有球体碰撞工作,但不能弄清楚立方体 - 球体碰撞。因为我想要它在3d中我有枢轴在对象的中心。有人有什么建议吗?

编辑:这是我目前的代码:

    bool SphereRectCollision( Sphere& sphere, Rectangle& rect) 
{ 

    //Closest point on collision box
    float cX, cY;

    //Find closest x offset 
    if( sphere.getCenterX() < rect.GetCenterX())//checks if the center of the circle is to the left of the rectangle
        cX = rect.GetCenterX(); 
    else if( sphere.getCenterX() > rect.GetCenterX() + rect.GetWidth()) //checks if the center of the circle is to the right of the rectangle
        cX = rect.GetCenterX() + rect.GetWidth(); 
    else //the circle is inside the rectagle
        cX = sphere.getCenterX(); 

    //Find closest y offset 
    if( sphere.getCenterY() > rect.GetCenterY() + rect.GetHeight() )
        cY = rect.GetCenterY(); 
    else if( sphere.getCenterY() < rect.GetCenterY() - rect.GetHeight() ) 
        cY = rect.GetCenterY() + rect.GetHeight(); 
    else 
        cY = sphere.getCenterY(); 

    //If the closest point is inside the circle 
    if( distanceSquared( sphere.getCenterX(), sphere.getCenterY(), cX, cY ) < sphere.getRadius() * sphere.getRadius() )
    { 
        //This box and the circle have collided 
        return false; 
    }

    //If the shapes have not collided 
    return true; 
}

float distanceSquared( float x1, float y1, float x2, float y2 ) 
{ 
    float deltaX = x2 - x1; 
    float deltaY = y2 - y1; 
    return deltaX*deltaX + deltaY*deltaY; 
}

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。我有正确的想法,但不知道如何执行它:

    bool SphereRectCollision( Sphere& sphere, Rectangle& rect) 
{ 
    float sphereXDistance = abs(sphere.X - rect.X);
    float sphereYDistance = abs(sphere.Y - rect.Y);
    float sphereZDistance = abs(sphere.Z - rect.Z);

    if (sphereXDistance >= (rect.Width + sphere.Radius)) { return false; }
    if (sphereYDistance >= (rect.Height + sphere.Radius)) { return false; }
    if (sphereZDistance >= (rect.Depth + sphere.Radius)) { return false; }

    if (sphereXDistance < (rect.Width)) { return true; } 
    if (sphereYDistance < (rect.Height)) { return true; }
    if (sphereZDistance < (rect.GetDepth)) { return true; }

   float cornerDistance_sq = ((sphereXDistance - rect.Width) * (sphereXDistance - rect.Width)) +
                         ((sphereYDistance - rect.Height) * (sphereYDistance - rect.Height) +
                         ((sphereYDistance - rect.Depth) * (sphereYDistance - rect.Depth)));

    return (cornerDistance_sq < (sphere.Radius * sphere.Radius));
}