Opengl 中的球体-立方体碰撞检测



我正在尝试在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; 
}

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

    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));
}

当边缘发生命中时,此算法不起作用,第二组 if 条件触发但不发生冲突

最新更新