# CryENGINE 3: Fun Physics

Exclusive offer: get 50% off this eBook here

### CryENGINE 3 Cookbook — Save 50%

Over 90 recipes written by Crytek developers for creating third-generation real-time games

\$29.99    \$15.00
by Dan Tracy | July 2011 | Open Source

CryENGINE 3 is globally recognized as one of the world's most powerful real-time middleware development platforms. In this article, we will look into several different examples, specifically of what the physics engine is capable of, such as manipulating gravity and setting up the different objects with rope physics.

In this article by Dan Tracy, author of CryENGINE 3 Cookbook, we will cover:

• Low gravity
• Hangman on a rope
• Wrecking ball
• Rock slide

## CryENGINE 3 Cookbook

 Over 90 recipes written by Crytek developers for creating third-generation real-time games

(For more resources on this topic, see here.)

# Low gravity

In this simple recipe, we will look at utilizing the GravityBox to set up a low gravity area within a level.

• Have Sandbox open
• Then open My_Level.cry

## How to do it...

1. To start, first we must place down a GravityBox.
2. In the RollupBar, click on the Entities button.
3. Under the Physics section, select GravityBox.
4. Place the GravityBox on the ground:

Keeping the default dimensions (20, 20, 20 meters), the only property here that we want to change is the gravity. The default settings in this box set this entire area within the level to be a zero gravity zone. To adjust the up/down gravity of this, we need to change the value of gravity and the Z axis.

To mimic normal gravity, this value would need to be set to the acceleration value of -9.81. To change this value to a lower gravity value, (something like the Moon's gravity) simply change it to a higher negative value such as -1.62.

## How it works...

The GravityBox is a simple bounding box which overrides the defined gravity in the code (-9.81) and sets its own gravity value within the bounding box. Anything physicalized and activated to receive physics updates will behave within the confines of these gravitational rules unless they fall outside of the bounding box.

## There's more...

Here are some useful tips about the gravity objects.

### Uniform property

The uniform property within the GravityBox defines whether the GravityBox should use its own local orientation or the world's. If true, the GravityBox will use its own local rotation for the gravitational direction. If false, it will use the world's direction. This is used when you wish to have the gravity directed sideways. Set this value to True and then rotate the GravityBox onto its side.

### Gravity sphere

Much like the GravityBox, the GravitySphere holds all the same principles but in a radius instead of a bounding box. The only other difference with the GravitySphere is that a false uniform Boolean will cause any object within the sphere to be attracted/repulsed from the center of the axis.

# Hangman on a rope

In this recipe, we will look at how we can utilize a rope to hang a dead body from it.

• Open Sandbox
• Then open My_Level.cry

## How to do it...

Begin by drawing out a rope:

1. Open the RollupBar.
2. From the Misc button, select Rope.
3. With Grid Snap on and set to 1 meter, draw out a straight rope that has increments of one meter (by clicking once for every increment) up to four meters (double-click to finalize the rope).
4. Align the rope so that from end to end it is along the Z axis (up and down) and a few meters off the ground:

5. Next, we will need something solid to hang the rope from.
6. Place down a solid with 1, 1, 1 meter.
7. Align the rope underneath the solid cube while keeping both off the ground. Make sure when aligning the rope to get the end constraint to turn from red to green. This means it is attached to a physical surface:

8. Lastly, we will need to hang a body from this rope. However, we will not hang him in the traditional manner, but rather by one of his feet.
9. In the RollupBar, click on the Entities button.
10. Under the Physics section, select DeadBody.
11. Rotate this body up-side-down and align one of his feet to the bottom end of the rope.
12. Select the rope to make sure the bottom constraint turns green to signal that it is attached.
13. Verify that the Hangman on a rope recipe works by going into game mode and punching the dead body:

## How it works...

The rope is a complicated cylinder that can contain as many bending segments as defined and is allowed to stretch and compress depending on the values defined. Tension and breaking strength can also be defined. But since ropes have expensive physics properties involved, they should be used sparingly.

 Over 90 recipes written by Crytek developers for creating third-generation real-time games
Published: June 2011
eBook Price: \$29.99
Book Price: \$49.99
See more

(For more resources on this topic, see here.)

# Wrecking ball

Instead of using the dead body that we used in the Hangman on a rope recipe, we will look at attaching a heavy RigidBodyEx in the shape of a ball to make a wrecking ball. Through this the player will be able to activate the rope physics to swing the wrecking ball into a breakable house and destroy a section of wall on it.

• You should have completed the Hangman on a rope recipe
• Open My_Level.cry within the Sandbox

## How to do it...

Using what we've already learned from the Hangman on a rope recipe, we will now swap out the DeadBody for a RigidbodyEx:

1. In the RollupBar, click on the Entities button.
2. Under the Physics section, select RigidBodyEx.
3. After placing the RigidBodyEx into your level, you first want to change the model over to a sphere.
4. Select the newly placed RigidBodyEx.
5. In the RollupBar, you will find Model under Entity Properties.
6. Change the model to the following .cgf model objects/default/primitive_sphere.cgf.
7. Then change the following properties still found under Entity Properties:

CanBreakOthers = true
Mass = 1000
Resting = False

8. Next, we will need to adjust our vertical rope to line up more horizontally with a bit of a bend in it.

Select your rope and use Edit Rope to move the rope segments along the Y and Z axes.

9. Lastly, line up the RigidBodyEx to the end of the rope. The final assembly of this should look like the following screenshot:

Now the wrecking ball is ready. However, it needs something to destroy. Place down a destructive object in the path of the wrecking ball such as the following brush Objects/library/architecture/village/village_house1_c.cgf. After setting up the wrecking ball over the head of the house, the player will see destruction occur the next time they enter the level.

## How it works...

The setup of the wrecking ball is very similar to the hangman, but just used in a different context of how designers can utilize the rope physics.

# Rock slide

In the wrecking ball recipe, we looked at using a RigidBodyEx sphere model to use as our wrecking ball. In this recipe, we will use that same RigidBodyEx as the basis for a rock analog and clone it a few times so we can build our landslide.

• You should have completed the Wrecking ball recipe
• Open My_Level.cry within the Sandbox

## How to do it...

1. Begin by creating a terrain hill about 30 meters in height with a slope of about 35-45 degrees.
2. Copy and paste the house that was used from the Wrecking ball recipe and place it at the bottom of the hill.
3. Next, select the sphere RigidBodyEx from the Wrecking ball recipe and clone it and place it more than half way up the hill. Also, change the following properties on the sphere:

Resting = False

4. Next, clone the RigidBodyEx five times and stack them on the hill like a carton of eggs. Make sure they are tight and are also touching each other (this will make sure they all get activated physics when triggered).
5. Last, place the breakable brush fence object Objects/library/barriers/fence/wooden/wooden_fence_120_400_a.cgf under the bottom RigidBodyExs. Be sure that the fence is also touching the RigidBodyExs.
6. Your setup should look like the following screenshot:

7. The next time the player enters the level and destroys the fence placed with something like a rifle, the RigidBodyExs will activate its physics and slide/roll down the hill and destroy the house as shown in the following image:

## How it works...

The RigidBodyEx entity is an extension to the BasicEntity that reacts more realistically with other entities and water. The attached object can break other objects and has more options as to when and how to react.

## There's more...

Here are some additional properties that can be applied to the RigidBodyEx class of the entity.

### Physics properties

The physics properties are as follows:

• ActivateOnDamage: This tells us that an inactive rigid body (RigidBodyActive=0) should be activated on damage.
• CanBreakOthers: This is True if the entity can break joined objects by colliding with them (provided they overcome the strength limit). Basic entities have this flag off unconditionally.
• Density: (Density = Volume / Mass) This affects the way objects interact with other objects and float in the water (they sink if their density is more than that of the water). Note that both mass and density can be overridden in the asset.
• Mass: (Mass = Density * Volume) This is the weight of the object (the density of the object multiplied by its volume).
• Physicalize: If not set, the object will not be taken into account by physics.
• PushableByPlayers: When set, the object can be moved by the player.
• Resting: When True, the object's physics are asleep on start. When False, the physics are activated until kinetic energy falls below the sleep_speed limit and is put to sleep again (refer to sleep_speed shown further).
• RigidBody: False means a static entity; True means a simulated rigid body. Note that a rigid body can still behave like a static entity if it has mass 0 (set either explicitly or by unchecking RigidBodyActive). The main difference between these rigid bodies and pure statics is that the physics system knows that they can be moved by some other means (such as the Track View) and expects them to do so. This means that objects that are supposed to be externally animated should be mass-0 rigid bodies in order to interact properly with pure physicalized entities.
• RigidBodyActive: This property indicates that the object is a rigid body, but initially it is immovable. Instead, it can be activated by an event later.

### Simulation properties

The simulation properties are as follows:

• Damping: (0..3) This sets the strength of the damping on an object's movement. Most objects can work with 0 damping; if an object has trouble coming to rest, try values like 0.2-0.3. Values of 0.5 and higher appear visually as overdamping. Note that when several objects are in contact, the highest damping is used for the entire group.
• FixedDamping: (true/false) When true, this object will force its damping to the entire colliding group (use it when you don't want a particular object being slowed by a highly damped entity, such as a dead body).
• max_time_step: (0.005..0.1) This sets the maximum time step the entity is allowed to make (defaults to 0.01). Smaller time steps increase stability (can be required for long and thin objects, for instance), but are more expensive. Each time the physical world is requested to make a step, the objects that have their maxsteps smaller than the requested one, slice the big step into smaller chunks and perform several sub steps. If several objects are in contact, the smallest max_time_step is used.
• sleep_speed: (0.01..0.3) If the object's kinetic energy falls below some limit over several frames, the object is considered sleeping. This limit is proportional to the square of the sleep speed value. A sleep speed of 0.01 loosely corresponds to the object's center moving at a velocity of the order of 1 cm/s.

# Summary

In this article we saw how to set up some enjoyable physics contraptions using CryENGINE 3's physics system.

Further resources on this subject:

 Over 90 recipes written by Crytek developers for creating third-generation real-time games
Published: June 2011
eBook Price: \$29.99
Book Price: \$49.99
See more

## Dan Tracy

Dan Tracy is Crytek’s Technical Level Designer for the award-winning CryENGINE and Crysis 2. He is responsible for the creation and maintenance of numerous technical features and external applications used for telemetry and optimization. Viewed as more than a level designer, Dan prides himself on pushing the envelope when it comes to improving both technical and game related designs across multiple production disciplines. Dan was recruited by Crytek in 2009 after the previous year working as a Quality Assurance Technician for BioWare. He was recruited due to his pivotal role in co-founding and leading development on the award winning total conversion project ‘MechWarrior:Living Legends’. Since then, he has been featured in numerous gaming magazines and has been interviewed by multiple media outlets. Dan is a passionate gamer, but an even more passionate modder and game designer with vast knowledge and experience with multiple engines and titles including Never Winter Nights’ “Aurora”, Battlefield’s engine “Frostbite”, Unreal 3, and CryENGINE.