How to determine a cube's: right,left,etc through line trace?
So heres the thing, I have an actor(it has a simplified box collision) with 6 sockets. 1 for each side: up,down,left,right,etc. I want to be able to ray cast and when it hits the actor I want it to check which side it hit and if it hits the right side of the cube I can get the right socket's transform and spawn another actor.
In short, I just want know which side the ray cast hit in a cube.
I've tried many things and it has work but it drained performance and so i want to know if there is a way more efficent way?
Sorry for the bad english, I hope you'll help me!
asked Sep 03 '16 at 02:26 AM in Blueprint Scripting
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:
Hmm, I'm not an expert on that level but maybe you could create 6 box collisions, one for each side, and set them to block linetraces in their collision settings. Then in the line/ray trace, you check which component of those 6 box collisions you hit, and if you hit the box collision which is named "Left" for example, you know you hit the left side..
answered Sep 03 '16 at 06:51 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here