DirectX碰撞(AABB)

时间:2018-12-07 11:08:59

标签: c++ directx

我一直在尝试将Axis-Aligned边界框应用于DX11中的多维数据集。

到目前为止,我有一个UserInput类,其中包含每个多维数据集,xPos,yPos,zPos,Rotation等的所有变量。

在UserInput中,我有一个检查两个对象是否碰撞的函数。

class UserInput
{
public:
    float xPos[2];
    float yPos[2];
    float zPos[2];
    Vector3 vecMin;
    Vector3 vecMax;
    float rotationX;
    float rotationY;
    float rotationZ;
    bool isMoving;
    bool isMovingRight;
    bool isMovingLeft;
    bool isMovingUp;
    bool isMovingDown;
    bool isMovingCloser;
    bool isMovingFurther;
    bool isRotatingX;
    bool isRotatingY;
    bool isRotatingZ;
    UserInput()
    {
        xPos[0] = -1.5f;
        yPos[0] = 0.0f;
        zPos[0] = 0.0f;
        xPos[1] = 1.5f;
        yPos[1] = 0.0f;
        zPos[1] = 0.0f;
        vecMin.x = -1.0f;
        vecMax.x = 1.0f;
        vecMin.y = -1.0f;
        vecMax.y = 1.0f;
        vecMin.z = -1.0f;
        vecMax.z = 1.0f;
        rotationX = 0.0f;
        rotationY = 0.0f;
        rotationZ = 0.0f;
        isMovingRight = false;
        isMovingLeft = false;
        isMovingUp = false;
        isMovingDown = false;
        isMovingCloser = false;
        isMovingFurther = false;
        isRotatingX = false;
        isRotatingY = false;
        isRotatingZ = false;
    }

    //Collision Rejection Test
    bool Collision(const UserInput& box1, const UserInput& box2)
    {
        return !(
            box1.vecMin.x > box2.vecMax.x ||
            box1.vecMax.x < box2.vecMin.x ||
            box1.vecMin.y > box2.vecMax.y ||
            box1.vecMax.y < box2.vecMin.y ||
            box1.vecMin.z > box2.vecMax.z ||
            box1.vecMax.z < box2.vecMin.z
            );
    }
};

我有两个基本的场景功能:更新和渲染。在更新功能中,我具有以下代码行

input->Collision(input[0], input[1]);

每次更新时应检查碰撞。

每个立方体都是这样绘制和渲染的

    //Update variables for the first cube
ConstantBuffer cb1;
cb1.mWorld = XMMatrixTranspose(g_World);
cb1.mView = XMMatrixTranspose(g_View);
cb1.mProjection = XMMatrixTranspose(g_Projection);
cb1.LightDir = LightDirs;
cb1.LightColour = LightColours;
cb1.OutputColour = XMFLOAT4(0, 0, 0, 0);
g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb1, 0, 0);


//Render first cube
g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer);
g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0);
g_pImmediateContext->PSSetConstantBuffers(0, 1, &g_pConstantBuffer);
g_pImmediateContext->PSSetShaderResources(0, 1, &g_Texture);
g_pImmediateContext->PSSetSamplers(0, 1, &g_TextureSamplerState);
g_pImmediateContext->DrawIndexed(36, 0, 0);

但是我似乎无法弄清楚如何用立方体“附加”盒子。 显然,到目前为止,这些多维数据集只是相互穿过而没有碰撞。

0 个答案:

没有答案
相关问题