0

What I have currently is causing my 3D object to become flat. But it is looking at my target.

    Vector4 up;
    newMatrix.SetIdentity();

    up.set_x(0);
    up.set_y(1);
    up.set_z(0);

    Vector4 zaxis = player_->transform().GetTranslation() - spider_->transform().GetTranslation();
    zaxis.Normalise();
    Vector4 xaxis = CrossProduct(up, zaxis);
    xaxis.Normalise();
    Vector4 yaxis = CrossProduct(zaxis, xaxis);

    newMatrix.set_m(0, 0, xaxis.x()); newMatrix.set_m(0, 1, xaxis.y()); newMatrix.set_m(0, 2, xaxis.z());
    newMatrix.set_m(1, 0, yaxis.x()); newMatrix.set_m(1, 1, yaxis.y()); newMatrix.set_m(1, 2, yaxis.z());
    newMatrix.set_m(2, 0, zaxis.x()); newMatrix.set_m(2, 1, zaxis.y()); newMatrix.set_m(2, 2, zaxis.z());

Excuse the method for putting values into the matrix, I'm working with what my framework gives me.

Vector4 Game::CrossProduct(Vector4 v1, Vector4 v2)
{
    Vector4 crossProduct;
    crossProduct.set_x((v1.y() * v2.z()) - (v2.y() * v2.z()));
    crossProduct.set_y((v1.z() * v2.x()) - (v1.z() * v2.x()));
    crossProduct.set_z((v1.x() * v2.y()) - (v1.x() * v2.y()));
    return crossProduct;

}

What am I doing wrong here? Note that I have added the forth line before with the 1 in the corner before just in case, with no change.

1 Answers1

0

you got problem when (0,1,0) is close to parallel to the direction you want to look at. Than the cross product will fail leading in one or two basis vectors to be zero which could lead to 2D appearance. But that would happen only if your objects are offset only in y axis between each other. To avoid that you can test dot product between the up and view direction and if the abs of result is bigger than 0.7 use (1,0,0) instead (as a up or right or whatever...).

Also as We know nothing about your notations we can not confirm your setting the matrix is correct (it might be or may be not, it could be transposed etc.) for more info take a look at:

But most likely your matrix holds also the perspective transform and by setting its cells you are overriding it completely leading to no perspective hence 2D output. In such case you should multiply original perspective matrix with your new matrix and use the result.

Spektre
  • 49,595
  • 11
  • 110
  • 380