x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: Not Selected
Tags:
more ▼

asked Sep 03 '16 at 02:26 AM in Blueprint Scripting

avatar image

Ace7806
15 2 3 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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. alt text

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 alt text

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

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

more ▼

answered Sep 03 '16 at 11:22 AM

avatar image

MulleDK19
2.7k 161 274 254

avatar image Ace7806 Sep 03 '16 at 11:58 AM

Sounds legit , thanks for the answer , I'll work on adding it soon and then reply how it went.

avatar image Ace7806 Sep 03 '16 at 01:28 PM

Ok so the good news i can finally clasify the side's but the bad news is this:

Error Blueprint Runtime Error: Accessed None trying to read property CallFunc_BreakHitResult_HitActor from function: 'ExecuteUbergraph_Character' from node: Get Hit Side in graph: EventGraph in object: Character

But i fixed by just casting to the cube i want to hit. I'm really grateful for your help , ive been trying to do this for 2 weeks. Thanks!

avatar image MulleDK19 Sep 03 '16 at 01:35 PM

No problem.

The error is probably because HitActor is None. Instead of casting it, use the IsValid node.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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..

more ▼

answered Sep 03 '16 at 06:51 AM

avatar image

iUltimateLP
188 17 28 38

avatar image Ace7806 Sep 03 '16 at 11:56 AM

That was the method I was using before. The thing is the next actor I spawn I also want to check the six sides , and as soon as I I get to 40 of the same actor frames start to drop, so yeah I don't want to use that.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question