我想找个当静态和运动中的球之间碰撞的发生,但我想出了算法,有时没有检测到碰撞和运动球穿过静态的。移动高尔夫球是受到重力作用和静一个不是

这是我的碰撞检测代码:

GLfloat whenSpheresCollide(const sphere2d &firstSphere, const sphere2d &secondSphere)
{
    Vector2f relativePosition = subtractVectors(firstSphere.vPosition, secondSphere.vPosition);
    Vector2f relativeVelocity = subtractVectors(firstSphere.vVelocity, secondSphere.vVelocity);

    GLfloat radiusSum = firstSphere.radius + secondSphere.radius;

    //We'll find the time when objects collide if a collision takes place

    //r(t) = P[0] + t * V[0]
    //
    //d^2(t) = P[0]^2 + 2 * t * P[0] * V[0] + t^2 * V[0]^2
    //
    //d^2(t) = V[0]^2 * t^2 + 2t( P[0] . V[0] ) + P[0]^2
    //
    //d(t) = R
    //
    //d(t)^2 = R^2
    //
    //V[0]^2 * t^2 + 2t( P[0] . V[0] ) + P[0]^2 - R^2 = 0
    //
    //delta = ( P[0] . V[0] )^2 - V[0]^2 * (P[0]^2 - R^2)
    //
    //  We are interested in the lowest t:
    //
    //t = ( -( P[0] . V[0] ) - sqrt(delta) ) / V[0]^2
    //

    GLfloat equationDelta = squaref( dotProduct(relativePosition, relativeVelocity) ) - squarev( relativeVelocity ) * ( squarev( relativePosition ) - squaref(radiusSum)  );

    if (equationDelta >= 0.0f)
    {
        GLfloat collisionTime = ( - dotProduct(relativePosition, relativeVelocity) - sqrtf(equationDelta) ) / squarev(relativeVelocity);

        if (collisionTime >= 0.0f && collisionTime <= 1.0f / GAME_FPS)
        {
            return collisionTime;
        }
    }

    return -1.0f;
}

和这里是调用碰撞检测更新功能:

void GamePhysicsManager::updateBallPhysics()
{
    //
    //Update velocity
    vVelocity->y -= constG / GAME_FPS;  //v = a * t = g * 1 sec / (updates per second)

    shouldApplyForcesToBall = TRUE;

    vPosition->x += vVelocity->x / GAME_FPS;
    vPosition->y += vVelocity->y / GAME_FPS;

    if ( distanceBetweenVectors( *pBall->getPositionVector(), *pBasket->getPositionVector() ) <= pBasket->getRadius() + vectorLength(*vVelocity) / GAME_FPS )
    {
        //Ball sphere
        sphere2d ballSphere;
        ballSphere.radius = pBall->getRadius();
        ballSphere.mass = 1.0f;
        ballSphere.vPosition = *( pBall->getPositionVector() );
        ballSphere.vVelocity = *( pBall->getVelocityVector() );


        sphere2d ringSphereRight;
        ringSphereRight.radius = 0.05f;
        ringSphereRight.mass = -1.0f;
        ringSphereRight.vPosition = *( pBasket->getPositionVector() );
        //ringSphereRight.vPosition.x += pBasket->getRadius();
        ringSphereRight.vPosition.x += (pBasket->getRadius() - ringSphereRight.radius);
        ringSphereRight.vVelocity = zeroVector();


        GLfloat collisionTime = whenSpheresCollide(ballSphere, ringSphereRight);

        if ( collisionTime >= 0.0f )
        {
            DebugLog("collision");
            respondToCollision(&ballSphere, &ringSphereRight, collisionTime, pBall->getRestitution() * 0.75f );
        }

        //
        //Implement selection of the results that are first to collide collision

        vVelocity->x = ballSphere.vVelocity.x;
        vVelocity->y = ballSphere.vVelocity.y;

        vPosition->x = ballSphere.vPosition.x;
        vPosition->y = ballSphere.vPosition.y;
    }

为什么没有在箱子100%被检测的碰撞?它被发现仅在70%的病例。 感谢。

UPDATE:问题似乎解决了,当我从30更改FPS分至10如何FPS影响我的碰撞检测

有帮助吗?

解决方案

delta = ( P[0] . V[0] )^2 - V[0]^2 * (P[0]^2 - R^2)

不应该认为是增量= B 2 - ?的 4 AC


<强> [编辑] 喔我看到的,你因素4出来。在这种情况下,你确定你正在考虑在t两种解决方案?

t = ( -( P[0] . V[0] ) - sqrt(delta) ) / V[0]^2

t = ( -( P[0] . V[0] ) + sqrt(delta) ) / V[0]^2

其他提示

如何大是球体的速度有多快被他们感动?可以在一帧期间的球“跳”过第二个(即,是它的速度矢量长于它的宽度?)。

根据这些原则,如果你在这里删除上限发生了:

if (collisionTime >= 0.0f && collisionTime <= 1.0f / GAME_FPS)
{
    return collisionTime;
}

如果球体得太快,可能你的算法检测到发生了一个以上的帧前的碰撞..(?)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top