Proper understanding of shape traces (in this case Box Collision Shape Trace)

Hey there,

I think I don’t really understand the correct way to use shape traces in Unreal Engine, so I was hoping someone could help me understand.

In my tutorial project I am currently using this Greystone character provided by Unreal in one of the free projects available on the marketplace or fab now. I want to do a box trace to check if the character hit something with his sword. In the skeleton editor of greystone we can see the trace I want to achieve:

I grabbed the location of both sockets in my .cpp code as well as the rotation of the sword_bottom socket. So far so good.

Now comes the confusing part. To do a box trace you specify a start location, an end location and a rotation. This makes sense as you want to specify from which point to which target you want to trace. However, you also specify a box half size and this is where it gets confusing in my opinion.

I would have expected the box half size to be a Vector2D to define the x and y scale of the box, but apparently it is a Vector3D. Why would that be? You already specified from which start to which end location the box should trace, but suddenly you can specify another Z value in the box half size for tracing.

After researching a bit it seems the start and end location of the trace apparently does nothing to the box shape itself. The box shape is defined by the box half size Vector3D and is drawn from the start location to the end location in a way that is not obvious to me. Maybe in a step of 1.f on x, y and z axis? Maybe in a fixed amount of steps as in "draw the box specified by box half size every 1% of the distance until you reach the end location?

This also means that there seems to be two ways to achieve the same trace (or make similar traces at the very least).

Let’s look at the following trace code (Trace 1 box spanning whole sword):

If you take a closer look what you can see is that I calculate the middle point of the start and end location. Then I just specify a box shape with the size of the sword and do the trace. This means since start and end location is the same, I would assume that we check the trace with a single box drawn. The result is the following trace:

This looks correct to me since the trace spans the whole sword as intended.

Another trace, same output:
Given the trace function will repeat the trace from start location to end location you can also specify a small shape and have it repeated over and over again (in a way not obvious to me as explained above). So here is another trace (Trace2, Small box, repeated from start to end):

Taking a closer look at the Z value of the make vector you can see that the box collision shape is very small, just repeated over and over again from start to end location. This results in the following trace:

The result seems mostly similar to me.

I hope the text gave a clear explanation of what I am trying to figure out, so let me summarize the questions I currently have

Questions:

  1. What are the use cases to prefer Trace 2 (small shape, repeated from start to finish) over Trace 1 (one collision shape)? I would assume Trace 2 to be more performant, but you are welcome to correct me (new to game dev so please do)
  2. If I do Trace 1 (small shape, repeated from start to finish), in which steps does unreal repeat the shape? I couldn’t find anything in the documentation for that.

Thank you all so much for your help in advance!

1 Like

The box trace is used to make a series of boxes that go from location A to location B, checking for overlaps along the way. That’s why you have a start and end and box size.

You only need 1 box here, on the sword. So you can just add a box collision to the sword ( socket ) the moves with the sword, and you’re done :slight_smile:

TBH I haven’t coded any combat stuff, so I could be totally wrong here, but I think you’ll find it works :smiley:

Maybe, if you have problems picking up collision when moving the sword quickly, you might use a box trace, and it would go from the handle of the sword to the tip, for each frame. But I doubt that would help, and harm your frame rate.

Box collision definitely the way to go here. If you use traces, you’ll have to perform trace every frame (tick) during a sword animation, and if the sword is fast or the frame rate is low, you’ll miss some locations inbetween.
With box collision you can handle fast moves by enabling CCD (continuous collision detection) somewhere in the collision parameters, which should solve that problem.

Regarding the original question, is a good question actually. I don’t have a definite answer, but here are some ideas :

  • You are tracing along one of the box axis, so yes you can extend the box. But that might not always be the case. Take for example a character moving and jumping around - you have to trace the character’s capsule collision diagonally all over the place, and cannot just “extend” the shape like you did. So the “Trace 2” approach simply covers more use cases.

  • You are seeing similar results in debug while hitting nothing, but the result may be different if you are actually hitting things. Again I don’t have definite answer so I’m not sure about this, but you might want to check what happens if there are two objects overlapping the sword. Your first trace will immediately hit both, so maybe there’s no guarantee in their hit order, while the second trace would hit them in consistent order. Also, the Hit Location, Hit Normal, and Hit Impact Normal values might be different in first and second traces, because in first trace the object is already overlapping with the shape right from the start, so I’m not sure if the engine would be able to figure out proper hit point and directions (to be confirmed).