Take the dot product of the axes of the cube and the hit normal.

The hit normal is a vector in the direction of the surface that was hit.
The dot product of 2 vectors gives you the cosine value of the angle between the two vectors.

Eg. if you have 2 perpendicular vectors, the dot product will return 0. (Acos(0) is 90°)

So, to check if your trace hit the front or rear of the cube, take the X (forward) axis (a vector pointing in the direction of the cube's front) of the cube, and dot it with the hit normal.

If it's 1, the difference between the hit normal, and the forward vector is 0°, which tells you that it was hit on the front.
You may want to give some leeway, instead of relying on the hit normal being absolutely perfect (and thus hitting 0.0000°).

Below is a simple macro that uses a high value of 45°.

Let's call it GetHitSideSingleAxis.

The function takes the dot product of the passed in vector (Eg. X (forward)) and the hit normal. If the hit normal is pointing in the same direction as the forward vector of the cube, the front was hit. If the hit normal is pointing in the opposite direction of the forward vector, it was hit in the rear.
0.7071 is approximately 45°
In case of X (forward), the macro will execute either Positive (forward), Negative (backwards) or neither, if the hitnormal is pointing neither forward nor rear.

Making a simple enum with all the sides (None,Front,Rear,Right,Left,Top,Bottom) we can make a simple function that returns which side was hit, given the axes of the cube, and the hit normal from the trace.

Let's call it GetHitSide

The axes can be retrieved by getting the rotation of the cube, and using the GetAxes node.

In case you're having trouble understanding the logic behind this:

answered **
Sep 03 '16 at 11:22 AM **

MulleDK19

2.7k
●
161
●
274
●
254