# How to calculate whether a cuboids face edge is infront of or behind another for sorting

I'm building an isometric game, I want to position and sort objects in a particular way. All my entities have 3d bounds which determines their cuboid, looking my example, a different edge is used to decide if a cuboid is in front of another, depending on whether the point b2 is left or right of a2.

I have already calculated all the points a1,a2,b1,b2 which are x,y,z I think I basically need to virtually draw "aedge" and "bedge", and compare some intersection.

If b2 is right of a2 the green cuboid (b) is consider in front of the red cuboid when bline is south east of aline.

If b2 is left of a2 the green cuboid (b) is considered in front of the red cuboid when bline is south west of aline.

Please forgive my ignorance here, I'm sure there's some terminology I should have used here. I'm struggling to put down in words what it is I need. • Could you use the x value of b2 (projection space) to determine if it is left or right of a2? I'm guessing the answer is no, so I think there is something about this question I've not understood. – CiscoIPPhone Jul 1 '14 at 16:06
• The vertices are xyz-vectors, correct? And the lines are always axis aligned? If so, this could be very simple. – jzx Jul 1 '14 at 19:08
• @jzx indeed they are, I have vectors to draw all the faces, they're xyz from the centre point of each entity – Rob Jul 2 '14 at 9:32
• What if one of the vertices in a near cube would be inside another cuboid? You still want to draw the entirety of it over the top? – jzx Jul 2 '14 at 16:34
• I'm only comparing a single cuboid against another, so if we introduce cube C, that will be compared seperately against the A & B and vice versa – Rob Jul 3 '14 at 9:02

I'm not sure how you have your axes set up, so this will work for any situation where the faces are parallel.

Subtract ax from bx. (ax is any point/vertex on face/edge a, bx is any point/vertex on face/edge b) This gives you a vector. Then do a vector projection onto the normal of the face you are testing (which is a unit vector). I.e.:

#pseudocode

v = bx - ax // e.g. a1 - b1
n = (-1, 0, -1).normalize // Change this based on how your axes are set up
vec_proj = v.dot(n) // dot product of the vector and normal

if (vec_proj > 0) {
// in front
} else {
// behind or overlapping
}