Hi ,
I am trying to do object picking.I calculate AABB and do ray - AABB intersaction.
When the object has position(0,0,0) everything works , but when I change the position the ray - AABB intersaction is alwayes true.
This is how I calculate the ray :
float pointX = (2.0f * localX / width - 1.0f) / shaderData.projection._11;
float pointY = (-2.0f * localY / height + 1.0f) / shaderData.projection._22;
XMVECTOR dir = XMVectorSet(pointX, pointY, 1.0f, 0.0f);
XMVECTOR det = XMMatrixDeterminant(view);
XMMATRIX viewInv = XMMatrixInverse(&det, view);
dir = XMVector3TransformNormal(dir, viewInv);
XMStoreFloat3((XMFLOAT3*)&pickRayDirection, dir);
And for every object I do this :
XMFLOAT4X4 mat = ent->GetWorldMatrix();
XMMATRIX world = XMLoadFloat4x4((XMFLOAT4X4*)&mat);
XMVECTOR det = XMMatrixDeterminant(world);
XMMATRIX invWorld = XMMatrixInverse(&det, world);
Vector3 org = cam->GetPosition();
Vector3 dir = cam->GetPickingRayDirection();
XMVECTOR orgInv = XMVector3TransformCoord(XMLoadFloat3((XMFLOAT3*)&org), invWorld);
XMVECTOR dirInv = XMVector3TransformNormal(XMLoadFloat3((XMFLOAT3*)&dir), invWorld);
dirInv = XMVector3Normalize(dirInv);
XMStoreFloat3((XMFLOAT3*)&org, orgInv);
XMStoreFloat3((XMFLOAT3*)&dir, dirInv);
Somewhere I saw that they get the translation matrix of the object , multiply it by the world matrix of the object and then inverse it.
Isn't the inverse of the translation done in the inverse world matrix ?!?!
I can apply any rotation and scale to the object and it will work , but if I change the position it does not work .
Tx
*EDIT :
I fixed it ! I just had to transpose the world matrix before I inverse it wow...