0

私の衝突検出は、長方形間の交差を取得し、効果を逆にすることで機能します。これは、各フレームで発生しています。プレーヤーがコーナーの上に座ってジャンプする場合を除いて、うまく機能します。時々、垂直交差が水平交差よりも大きくなり、プレーヤーがプラットフォームの側面を滑り落ちることがあります。助言がありますか?

ここに画像の説明を入力

    -- detect initial collision
    if mathy.hasCollided(player, platform) then

        local playerBoundaries = player:boundaries()

        -- list of intersections between platform and player
        local bottomBoundary = mathy.bottomBoundary( playerBoundaries, platform )
        local topBoundary = mathy.topBoundary( playerBoundaries, platform )
        local rightBoundary = mathy.rightBoundary( playerBoundaries, platform )
        local leftBoundary = mathy.leftBoundary( playerBoundaries, platform )

        local smallestDist = ""
        local smallestBoundary
        local boundaries = {
            bottom = bottomBoundary,
            top = topBoundary,
            right = rightBoundary,
            left = leftBoundary
        }

        -- get the smallest intersection (thats the side we're probably closest to)
        for direction, boundary in pairs(boundaries) do
            if not smallestBoundary then
                smallestBoundary = boundary
                smallestDist = direction
            end

            if smallestBoundary > boundary then
                smallestBoundary = boundary
                smallestDist = direction
            end
        end

        -- reverse effects depending on collision location
        if smallestDist == "bottom" then
            player.desiredPos:add(diffX, -bottomBoundary)
            player.velocity.y = 0
            player.onGround = true
        elseif smallestDist == "top" then
            player.velocity.y = 250
            player.desiredPos:add(0, topBoundary)
        elseif smallestDist == "right" then
            player.desiredPos:add(-rightBoundary, 0)
        elseif smallestDist == "left" then
            player.desiredPos:add(leftBoundary, 0)
        end
    end
4

1 に答える 1

0

短いクリップからはわかりにくいですが、問題はオブジェクトの速度の方向の交点をチェックするのではなく、最小の交点をチェックした結果だと思います。smallestBoundaryループの代わりに次のようなものを試すことができます。

local boundary = nil
if (player.velocity.y > 0) then
  boundary = topBoundary
elseif (player.velocity.y < 0) then
  boundary = bottomBoundary
elseif (player.velocity.x > 0) then
  boundary = rightBoundary
elseif (player.velocity.x < 0) then
  boundary = leftBoundary
end

もちろん、これは可能な限り堅牢ではありません。2 つのアプローチを組み合わせて、smallestBoundaryループの代わりに次のようなことを行うこともできます。

    local yMod = math.abs(player.velocity.y)
    local xMod = math.abs(player.velocity.x)
    local topMod = player.velocity.y > 0 and yMod or 1
    local bottomMod = player.velocity.y < 0 and yMod or 1
    local rightMod = player.velocity.x > 0 and xMod or 1
    local leftMod = player.velocity.x < 0 and xMod or 1
    local boundaries = {
        bottom = (bottomMod / MAX_VELOCITY) * bottomBoundary,
        top = (topMod / MAX_VELOCITY) * topBoundary,
        right = (rightMod / MAX_VELOCITY) * rightBoundary,
        left = (leftMod / MAX_VELOCITY) * leftBoundary
    }

    for direction, boundary in pairs(boundaries) do
        if not smallestBoundary then
            smallestBoundary = boundary
            smallestDist = direction
        end

        if smallestBoundary > boundary then
            smallestBoundary = boundary
            smallestDist = direction
        end
    end

これはまさに今行っていることを行いますが、その方向のプレーヤーの速度によって境界のサイズを (比較のコンテキストでのみ) 調整します。したがって、 を使用して と を使用して移動している場合x-5平面y-10の下向きの衝突は、y平面内の左向きの衝突の 2 倍の重みを持ちxます。もちろん、衝突のすべての面でプレーヤーを調整する別のオプションが常にあります。

    if bottomBoundary > 0 then
        player.desiredPos:add(diffX, -bottomBoundary)
        player.velocity.y = 0
        player.onGround = true
    end
    if topBoundary > 0 then
        player.velocity.y = 250
        player.desiredPos:add(0, topBoundary)
    end
    if rightBoundary > 0 then
        player.desiredPos:add(-rightBoundary, 0)
    end
    if leftBoundary > 0 then
        player.desiredPos:add(leftBoundary, 0)
    end

この最後の方法は最も理にかなっていますが、何らかの理由ですべての方向の衝突を一様に処理するようには見えないため、アーキテクチャに適合しない可能性があります。

私はあなたが使用しているフレームワークに精通していないので、このコードはそのままでは機能しない可能性があることに注意してください。+yまた、この投稿では、上、-y下、+x右、左を想定してい-xます。

于 2015-12-15T22:26:53.847 に答える