Jan 22, 2015

Soft Bodies in Blender Game Engine: Suspension Bridge Tutorial

Soft Bodies in BGE are so limited and finicky, some game developers have given up on them and have resorted to simulating them with chains of Rigid Body objects held together with Rigid Body Joint constraints. We can't be too surprised; after all, BGE is based on Rigid Body physics. Nevertheless, as this tutorial will show you, Soft Bodies can be used in BGE quite well, if we learn to work within their limitations.

Before we begin, some ground rules. Firstly, Soft Body objects do not work with Collision, Touch, Near, and Radar logic brick sensors. Secondly, physics simulations in general are unpredictable, something to beware of in games. Your use of physics in BGE should be limited to things that can be controlled sufficiently that the outcome remains predictable. And thirdly, the more accurately you set up the physics, the more likely playback framerate will suffer. The golden rule is to Keep It Simple, as simple as you can get away with.

Click to enlarge

To demonstrate the main principles of good Soft Body design in BGE, we're going to create a simple suspension bridge across a canyon, made of wooden planks and rope. How do we simplify a suspension bridge? At a minimum, the object has three parts: the deck, the ropes holding the deck, and the supports that the ropes are attached to.

The supports don't need to move, so they can be static objects, not part of the Soft Body. The deck and the ropes would ideally be modelled and simulated as interacting rigid and soft bodies, but this is not feasible in BGE. We'll model them as one object, taking advantage of a particular aspect of how Soft Bodies work: the greater the density of the mesh, the greater its flex and elasticity. Keeping the mesh of the wooden planks less dense than the ropes, will help us toward an acceptably realistic illusion of a solid deck. Additionally, a number of planks strung together into a flexible structure is allowed to behave like a soft body to an extent.

After a little bit of math to work out the correct placement of things, we settle on a design where the horizontal "handrail" ropes are attached to the deck with 4 vertical ropes on each side, and the deck consists of a total of 24 wooden planks. A plank will be "missing" on either end of the bridge, providing the margin needed for the support columns holding the ropes. The deck will be anchored to hidden supports in the walls of the canyon. Finally, four additional, "tensioning" rope objects will connect the supports to struts in the ground, completing the object.

The symmetrical nature of the bridge in both X and Y directions means that we only need to construct one quarter of the bridge, using two Mirror modifiers to create the rest.

We start by creating the mesh of one quarter of the main bridge body: half the handrail rope, 2 hanging ropes and 12 half planks. With the cursor in the centre of the grid, create a long, thin cube for the handrail: Y = 5 units, Z and X = 0.075. Rename the cube to Bridge, and in Edit mode (leaving the object origin in the centre) move the mesh down the Y axis 2.5 units, down the X 1 unit, and up the Z 1.5 units. Delete the front face nearest the centre (at Y = 0). Select the other, far end face of the cube, and set Edge Crease to 1 for sharp edges there.

Add two Mirror modifiers, one for X and one for Y axis. Important: in the Y axis modifier, uncheck the Merge option. This way we prevent the front face of the first wooden plank being merged by the mirroring.

Add a Subsurf modifier, default settings are fine (factor of 1). Don't apply the modifiers just yet. In Object mode, you can see we have the two handrails, total length 10 and separation of 2 units.

Now for the two vertical ropes. We'll extrude them from the handrail, making them appear as if hanging down from knots. Again in Edit mode, place four loops (Ctrl-R) on the cube mesh at 0.95, 1.05, 2.95 and 3.05 Y units from centre. This gives you the two bands of faces you need for the knots, each 0.1 units wide, centred on 1 and 3 units from centre. For each band: select the band of faces, press E to extrude and with no mouse movement press Return. Press S and Shift-Y to scale up the new extruded band on X and Z only, by 1.5. We have our knots. Select the 2 x 4 edges connecting each knot to the handrail on both sides and set Edge Crease to 1, to retain the straight shape of the handrail.

For the vertical ropes, select the bottom face of each knot, set its Edge Crease to 1, scale it down by 0.5 and move it down on Z a tad, say 0.05 units, for a tapered look to the knot. Now extrude this face down to 0 on the Z axis. The edges of the extruded face should already have Edge Crease of 1, but if not, set it. That's the ropes done for now.

Still in Edit mode, create 12 wooden plank halves: add a cube of dimensions X = 1.2, Y = 0.4, Z = 0.1, move it down on Y and X so that one corner edge is at grid centre. Delete the face at X = 0 (where the Mirror modifier will create the other half of the plank). Select the face on the other, outer end of the plank and set Edge Crease to 1.

Click to enlarge
Duplicate this cube and move it down 0.4 on Y, repeat another 10 times for a total of 12 of these cubes tightly packed together.

Tab into Object mode and admire your work, we're almost there. Now apply the modifiers - the two Mirrors and 1 level of Subsurf. Then back into Edit mode, we must remove the first and third edge loop in each plank that the Subsurf added, leaving only the middle loops. The easiest way: in Top View, Wireframe view, Vertex or Edge select mode, box select the loops down the entire length of the row of planks. Press X and choose Edge Loops to delete them.

We need to connect the planks into one object, otherwise they'll be falling all over the place in the simulation. (A destroyed suspension bridge with planks strewn about might be useful though - feel free to save a copy of the document for safekeeping before proceeding further.) To connect the planks, select all, press W and choose Remove Doubles. The Info notice should say 85 vertices were removed. Check that the planks are indeed connected: select an edge or point in a far corner, and press Ctrl-L. The entire deck should be selected.

Our next task is to connect the vertical ropes to the planks underneath them. We couldn't have done this earlier because the shape of the planks wasn't right yet. The problem is, we now have 8 of these ropes, not just 2. Let's make this easy and reduce the bridge back to one quarter. Carefully select vertices on the positive X and Y axes from the centre and delete them. Add two Mirror modifiers as before, but with the Merge option remaining checked on both.

Click to enlarge
Because edges in a Soft Body are springs, connectors can distort the surfaces around them, and we want to avoid that. We're going to connect the ropes inside the planks, to their bottom faces. For each rope/half-plank combo: select the two bottom faces of the plank, press I to inset faces, move mouse until the "frame" polygons and the new faces inside it are about the same width and click to set. Select the two new faces and scale and move them into a square-ish shape under the rope, doesn't need to be exact (just be careful to do all operations with Shift-Z to lock the Z axis). Then connect the bottom edge of the rope to edges of the two faces forming the square under it. The number of edges of the two objects doesn't match, you'll have to create some triangles, but that's fine.

Click to enlarge
Before we apply the Mirror modifiers again, we'll add edge loops to the ropes, increasing mesh density. Subsurf has already added a loop in each section of the handrail and in vertical ropes. Add 8 loops on either side of the loop in the two longer handrail segments (for a total of 17 per segment), and 4 loops either side of the loop in the short segment (so this middle segment will have a total of 19 loops after Mirrors are applied). On verticals, 6 loops either side of the middle loop, making up a total of 13 loops per rope (not counting the knot or connector).

Apply the Mirror modifiers and that's the Soft Body part of the bridge almost done. (Just check again everything is connected, using the selection method as before.)

Click to enlarge
One last thing we need to do, we need to give the bridge a gentle downward curve in the middle. We do this with a Lattice, so add one to the scene. In the Lattice panel, set V and W to 3, leave U at 2. Scale and move the Lattice so that the bridge fits inside it nicely (use the orthogonal views). Then in Edit mode, move the middle vertices of the Lattice down Z by -0.4. Add a Lattice modifier to the bridge, choose the Lattice for its Object setting, and apply the modifier. You can now delete or hide the Lattice object.

Finally, we scale the bridge up by 10 times and apply scale. The physics system likes bigger objects. The dimensions of the bridge should now be X = 24, Y = 100 and Z = 18 or thereabouts. We're done with modelling of the Soft Body part.

Click to enlarge
On to the anchors. These are hidden boxes that will serve as hinges from which the bridge is suspended. One box for each handrail end, two boxes for each deck end. For handrails: in Object mode, add a cube of 1x1x1 dimensions and move it to the end of one of the handrails, the rope end inside the box.

Switch renderer to Blender Game if that isn't the case already. In Physics tab, set the box's Physics Type to Static, check Ghost, uncheck Actor, leave the rest.

In Constraints panel, add a Rigid Body Joint constraint, select Bridge for Target and leave Ball as Pivot and the rest is fine too. Rename the box Support, make three copies and move them to the other ends of the two handrails.

Duplicate the last box and resize it to X = 14, Y = 4, Z = 2. In Constraints panel for this box, change Pivot Type to Hinge, the rest is fine. Duplicate the box, and set the duplicate next to the original lengthwise on X axis, they should look like two halves of one long box. Move them right next to one of the deck ends, the protruding edge of the last deck plank almost touching the middle of the two boxes. Duplicate the two boxes again and move the duplicates to the other end of the deck.

Select the bridge and in Physics panel, set its Physics Type to Soft Body, uncheck Shape Match, be sure Bending Constraints is checked. Set Mass to 100, Position Iterations to 10, Linear Stiffness to 0.7, Friction to 0.1, Margin to 0.1.

Finally, in the World panel, in Physics settings, set Max Physics Steps, Max Logic Steps, and Substeps all to 5, FPS to 30 (or 25 maybe).

We're ready for a test. Press P to play. You should see the bridge sway wildly but not out of control and slowly calm down. It should look acceptably authentic. That's it, you're mostly done, the rest is decoration.

For the canyon walls, have fun creating steep jagged edges, setting them so they just cover the deck-anchoring boxes. Canyon should be a Static object in Physics. For handrail support columns, four subdivided boxes will do, also hiding the anchor boxes. The supports' Physics Type should be Static.

Create tensioning ropes the same way as the handrails, and use the same Physics settings, but set Mass to 1 and Linear Stiffness to 0.2 or higher. (If you want them moving, that is. They could also be static.) Anchor the ropes as we did the handrail. The existing anchor boxes in support columns can be used, just create new Rigid Body Joint constraints. Duplicate and scale down the columns to make the stumpy struts in the ground.

For texturing, BGE requires UV mapping, so unwrap the objects. For shadows, the Sun works well, experiment with the settings.

I leave you with a short video capture of the bridge in play, providing a pathway for a few balls to bounce on.



The balls are Rigid Body icospheres, using Sphere collision bounds, and Radius a little smaller or larger than the ball. They have Motion actuators propelling them along the Y axis, after a delay. The setup strikes the right balance between control and simulation for an authentic and predictable outcome.

Click to enlarge

3 comments:

  1. brilliant and helpful tutorials
    Thankyou

    ReplyDelete
  2. I create this https://www.youtube.com/watch?v=HnXjBkWqdKQ, a long time ago, in blender physics.

    ReplyDelete

Note: Only a member of this blog may post a comment.