I'm attempting to find the world position of an object that exists in screen space. For example in the below screenshot the pistol and hands are rendered without being translated into world space (using only projectionMatrix * modelMatrix), while the other objects in the scene exist in world space (translated via projectionMatrix * viewMatrix * modelMatrix):
I have added a "muzzle" bone to the end of the armature for this pistol, and I am attempting to convert the translation of the muzzle bone into world space so that I can cast a ray starting at this position.
I have this kind of working, but something isn't quite right. For example, note the closest green sphere in the above screenshot. That is the position which I come up with in world space for my muzzle bone. What I am expecting is below:
To obtain this position I am multiplying the position of the muzzle bone by the inverse of the projection and view matrixes of the world camera as follows:
glm::mat4 invMat = glm::inverse(worldStage.getCamera().value().getProjectionMatrix() *
worldStage.getCamera().value().getViewMatrix());
glm::vec4 tmp = this->weapons[this->currentWeapon].actor->getTransform().getMatrix() *
glm::vec4(this->weapons[this->currentWeapon].muzzleBone->translation, 1.0f);
glm::vec4 muzzleWorldTranslation = invMat * tmp;
muzzleWorldTranslation /= muzzleWorldTranslation.w;
It feels like I'm pretty close and there's just something that I'm missing or messed up slightly. If anyone is able to point out what that might be, I would be very grateful!
P
is a projection matrix andV
is the view matrix, the point of what you are doing that,inv (P * V) = inv (V) * inv (P)
, gives you the identityP * V * inv (V) * inv (P) = I
, you need to follow the same concept using the model matrix.i.e. inv(PVM)
, whereM
is the model matrix. – 4.Pi.ninvMat
, however this yielded worse results. I did notice however through some more debugging that themuzzleWorldTranslation
value I am coming up with appears to be mirrored, almost like they are rotated 180 around the up axis (Y) from what they should be. – whitwhoathis->weapons[this->currentWeapon].actor->getTransform().getMatrix()
containts the transformation to clip space, which doesn't make any sense whatsoever.Why would you mix in the projection matrix at this point? – derhassthis->weapons[this->currentWeapon].muzzleBone->translation
is relative to the transform of the weapon actor which is why I'm multiplying it bythis->weapons[this->currentWeapon].actor->getTransform().getMatrix()
. Should I not be multiplying these matrices by the inverted projectionView matrix? – whitwhoa