If I have a std::vector of objects containing a rotated / translates matrix, is there a way I can use that matrix to calculate a Z position so that I can sort the objects by depth?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer :
sort along matrix[3][2], which is the z position (in world space) of your object’s center.
Long answer :
In homogeneous coordinates, a position is (x,y,z,1) ( as opposed to a direction which is (x,y,z,0), as we will see later )
To transform your point from one space to another, you multiply it by the transformation matrix. So if you want to transform the object’s center, which is thus (0,0,0,1) by a matrix, the only remaining term is the right column of the matrix. The z component is the third.
With a direction, w=0, so the right column (which holds translation) will be multiplied by 0. This means : translating a direction does not change the direction, which makes sense.
Obviously, if you sort object’s centers, two close objects may overlay.