Creating an Explosion Effect
In the Physics Simulation Basic Workflow, we detailed how to create a suck-up effect. All the characters were sucked up inside the UFO so we did not have to care about computing collisions with the ground or the environment.
Today we are going to experiment with a simple explosion and a terrain, to see how we can get characters to interact with the environment. Something similar to what we did in the fortress demo:
Initial Setup
The initial setup consists in a few character placed on a plan with a few obstacles.
You can have a look at the Quickstart Tutorial to create such a scene.
Add a Motion Behavior with a walking motion so that they walk toward the cube (as we did not created a Navmesh nor used a Goto/Navigation behaviors, they will not care about the cube and go through it, but it's not important for this scene as we are going to make them explode before).
Creating a physical world
As for the UFO scene, we need to create a Physics Locator, but here we are going to create a physical world instead of using the default plane.
- Select the meshes you want to include in the physical world (the others will be ignored in physics computation).
- Click on the "Crowd Rigid Body" icon in the Golaem shelf, Golaem creates every required nodes for the physics simulation including a Physics Locator node .
- Go into the Physics Locator attributes and set the Environment Mode to None to get rid of the default plane as we already have a Pplane set to rigid Body for our ground.
- If you want to check that everything went ok, you can check the Draw Collision Shapes of the display attributes of the Physics Locator to see if the physical world match your environment (you need to go forward a few frames to be able to see it, as the Physics Locator can be refreshed only when the simulation is running).
- As you can see every crowd Rigid body is set to triangle mesh but the Physics shape of the Sphere is not very accurate.
- You can edit it by going into the attributes of the crowd Rigid Body of the sphere and change its Shape Type to Sphere.
Enable Physics on your Characters
To put your characters in the physics world, you need to add a Physicalize Behavior to your Entities in the Behavior Editor. As we are going to add some behaviors after this one, let's create first a container in parrallel with our Motion Behavior:
And then add a Physicalize Behavior inside.
To time the explosion, go inside the Physicalize Behavior's Start Trigger and drag and drop a Current Frame Trigger. Edit it (let's say at frame 20).
Note that to ensure the initial velocity of your characters are correctly taken into account, the Physicalize Behavior needs to last at least 2 frames before setting the dynamic rigid body mode (See Physicalize Behavior Best Pratices page for more details about this point). You can ensure that by setting a Behavior Time Stop Trigger on the Physicalize Behavior.
If you run the simulation at this point, your characters will just fall on the floor and keep running physics simulation forever.
Add a Force Behavior
In order to create an explosion, we need to trigger an impulse in the physic simulation. This is done using the Force Behavior. Drag and drop it just after your Physicalize Behavior and check its attribute editor for configuration.
For this example we used a locator to control direction and set the magnitude empirically. The direction of the impulse is "from the locator to your character's root", so if you want to create an explosion effect, you should place it under the ground. Note that the distance from the character is not relevant is you only use a fixed magnitude, as we did.
Run simulation and watch your characters be thrusted out by the explosion.
Stopping characters when they reach the ground
Note that when they reach the floor and should act as dead, you can notice they are still slightly moving. This is because the physics simulation is still running. It can be stopped very easily by adding another Physicalize Behavior (using the Dephysicalize Behavior Mode / Lock Posture mode) after the Force Behavior.
Remember to stop it at once, by changing the Stop Trigger value from false to true.
The only trick is to know when to stop the Force Behavior and dephysicalize your characters.
To stop the Force Behavior we can use the fact that when characters reach the ground, their velocity is close to 0 and set a Stop Trigger based on this per-particle attribute value. For more information about how to setup a trigger based on velocity and why you need to protect your trigger with "current frame > 5", see how to select a motion based on character speed.
Possible improvements
Body Masks
You can also use the Body Mask of the Force Behavior to make the impulse only impact some of the limbs. See the Physics Simulation Basic Workflow for an example.
Collision with moving objects
You can also use a keyframed Zone Trigger in order to simulate a collision with a moving object.
E.g, you can to create a sphere and keyframe it to be used as a Start Trigger for the first Physicalize Behavior instead of the Current Frame Trigger. Note that this sphere is just used as a Start Trigger, not at all to compute a collision and the resulting force.
Note: although the Zone Trigger already gives nice and controllable results, a more physically correct way of simulating this kind of collision would be to link a CrowdRigidBody on top of your sphere. See Physics with Animated Colliders
Secondary collisions
If you do this, you will notice that maybe some characters will not fall because of the sphere (because it does not touch them), but are collided by falling characters and do not care about it.
To make the fall when they are collided by other characters, we need to add a Physicalize Behavior in the Parallel Operator, and set its stop trigger to be activated when collided.
Use a Collision Trigger and set it as root. Then in the attribute editor, click on the + button next to Physics Mode and set the Physics Mode Filter value to Dynamic Rigid Body.
Indeed, the trick here is to filter the collisions correctly. If we use the default values, all characters will fall at the start of the simulation because they touch the ground (= a Rigid Body).
Now, even if they were not collided by the sphere (i.e. the 3d zone trigger was never activated and the corresponding Physicalize Behavior is still waiting), if the character is hit by an Entity in Dynamic Mode (i.e. an exploding character), the Physicalize Behavior at the bottom of the parrallel operator will stop and the character fall on the ground.