DirectXMath Einstieg -> Verständnisfrage



  • Ich arbeite gerade an DirectX und nutze zum ersten mal die DirectXMath Funktionen. Die SIMD Unterstützung ist neu für mich. Das Flag _XM_SSE_INTRINSICS_ ist entsprechend gesetzt.

    Von daher wollte ich fragen ob der folgende Code so ok ist:

    float GetDistance2D(const DirectX::XMVECTOR& P1, const DirectX::XMVECTOR& P2)
    {
        DirectX::XMVECTOR delta = DirectX::XMVectorSubtract(P1, P2);
        DirectX::XMVECTOR length = DirectX::XMVector2Length(delta);
    
        return DirectX::XMVectorGetX(length);
    }
    

    Mein gedankliches Problem: DirectX::XMVECTOR ist ein 4D Vektor, aber ich brauche nur die 2D Distanz. Wäre also der folgende Code schneller?

    float GetDistance2D(const DirectX::XMVECTOR& P1, const DirectX::XMVECTOR& P2)
    {
        float P1x, P1y, P1w;
        float P2x, P2y, P2w;
    
        // Bestimme XY von P1
        P1w = DirectX::XMVectorGetW(P1);
        if (P1w != 0.0f && P1w != 1.0f)
        {
            P1x = DirectX::XMVectorGetX(P1) / P1w;
            P1y = DirectX::XMVectorGetY(P1) / P1w;
        }
        else
        {
            P1x = DirectX::XMVectorGetX(P1);
            P1y = DirectX::XMVectorGetY(P1);
        }
        // Bestimme XY von P2
        P2w = DirectX::XMVectorGetW(P2);
        if (P2w != 0.0f && P2w != 1.0f)
        {
            P2x = DirectX::XMVectorGetX(P2) / P2w;
            P2y = DirectX::XMVectorGetY(P2) / P2w;
        }
        else
        {
            P2x = DirectX::XMVectorGetX(P2);
            P2y = DirectX::XMVectorGetY(P2);
        }
        // Berechne nun den Abstand
        float DiffX = P1x - P2x;
        float DiffY = P1y - P2y;
    
        return sqrt(DiffX * DiffX + DiffY * DiffY);
    }
    

Anmelden zum Antworten