Simulating Explosion Effects in C++

Simulating Explosion Effects in C++
>version : unreal engine 5.2<
Today we are going to conduct a super interesting Tutorial, simulating explosion effects in C++ by adding a radial shockwave to all objects, with adjustable ranges.
Create a new Actor named AddRadialForceActor, and do not need to add content in the header file for now.
We will add all the logic in BeginPlay(). Collect hit results within a defined range. Therefore, we will use a TArray array to collect overlapping actors.
Declare the variable arrays used as containers. OutHits is used to store collision results. SweepActors is used to store scanned actors.

Simulating Explosion Effects in C++

Use GetActorLocation() to get the actor’s position, which will serve as the start and end point for the shockwave.

Simulating Explosion Effects in C++

Scan with a collision shape of 500 units — spherical.
Obtain a variable of type FCollisionShape named MyColSphere using FCollisionShape::MakeSphere(500.0f).

Simulating Explosion Effects in C++

To visually observe the spherical effect of the scan, we use the DrawDebugSphere class to draw a debug graphic. For detailed introduction, you can click here 《Accelerate Development! C++ Drawing Unreal Engine Debug Graphics》.

Simulating Explosion Effects in C++

Simulating Explosion Effects in C++

Currently, our Actor does not have a SceneComponent component added, so it can only be placed at the origin and cannot be moved. Add SceneComponent to enable movement, as described in previous articles.
Next, we check if the Actor hits an item when BeginPlay(). Each Actor can access the GetWorld function. Then call SweepMultiByChannel function, using the variable we just created as a parameter. It will return a boolean value indicating whether there are actors within the range that will be hit.

Simulating Explosion Effects in C++

Thus, we have put all actors that will hit into OutHits.

If isHit is true, use a for-each loop to iterate through the OutHits array, get the root component, and add a radial shockwave. The actors within the range will directly apply the shockwave effect.Simulating Explosion Effects in C++

Simulating Explosion Effects in C++

Simulating Explosion Effects in C++

Copy the source code — class library

Leave a Comment