From BlenderWiki

Jump to: navigation, search

The basics

Sometimes you wish to render a complex scene that involves collisions, multiple forces, friction between multiple bodies, and air drag, but you don't want to try to manually animate each. Luckily, you can count on the Blender game engine to do it for you. Currently, aside from Blender, very few 3d suites offer this function, even professional ones, unless you purchase the most advanced versions. Game engines are designed to simulate rigid (undeformable) bodies. There is currently a plan to integrate this functionality into the Blender's animation system, which will then eliminate the need for baking.

This idealized system can mimic a lot of what happens in our macroscopic world really well and it is this mimicking that is useful for many applications (today's games and animation software included).

In Blender we can use rigid body functionality to animate the dynamics of objects moving, rotating, and colliding with each other. This is much easier, faster, more realistic, and more fun than trying to recreate it manually. This is the case for even simple cases, not to mention scenes where we can have hundreds or thousands of objects falling and colliding against each other and static obstacles.

Importing from other programs

In case your 3d suite does not offer this functionality, you can use Blender to generate a rigid body simulation and then export it back to your suite by exporting in the native format of your player from Blender. Though a wiser choice is using the COLLADA format.

COLLADA is a COLLAborative Design Activity for establishing an interchange file format for interactive 3D applications. COLLADA version 1.4, released in January 2006, supports features such as character skinning and morph targets, rigid body dynamics and shader effects for multiple shading languages including the Cg programming language, GLSL and HLSL.

Go to the homepage of the COLLADA project, and search if they currently have a plugin for your 3d suite. If they have it (or you find it on other community driven sites), install it, export from your program, and import in Blender. The guidelines you see will have a set basic method of operation.

Importing a COLLADA file from the File > Import menu

Dominoes

We will simulate an arrangement of dominoes falling one onto the other. This is a pretty classic simulation for game engines, and very difficult to manually produce, so a good use for IPO Baking.

How to:

This example does not intend to show how to use the game engine. If you don't know how to use the game engine, however, you will see some very basic guidelines here. The method used here to produce simulations emphasizes accuracy of the simulations, while one designing games wants to ensure a real-time experience. Most of the ways to improve the game engine simulation speed will not be used here, as they may decrease the realism of the simulation.

Our domino piece, already enabled as actor, as simple as possible.
  • Add a cube, scale it in the axis, so it looks like a domino piece.
  • Enable our cube to be an actor, to be rendered in the game engine. Press F4 and click Actor. Now our domino piece will be emulated by the engine. After selecting our piece as an actor, do not scale it anymore.
  • Select Dynamic,Rigid Body,enable Bounds and select Box on the dropdown menu near Bounds. Dynamic means that our object will respond to gravity, and will move itself when colliding. Rigid body means our object will also rotate when colliding.
  • Create a plane, to be our floor, and move it under the domino piece.
Selecting the drawmode.
  • Now we are ready to duplicate our domino piece and to create a good setup. Use AltD to duplicate the pieces as linked duplicates. So afterwards, when we edit the mesh of one, all the other will be edited also. To give it our first shot, put six dominoes lined up in a column and rotate the first piece forward a little bit. Press P to run the game engine, and Esc to stop it. It may happen that your first piece doesn´t fall on the second, so you will need to rotate it more, just like the example. Do not enable IPO baking right now.
  • If your pieces give a little jump when you start. This is because they are too near the plane, move them up a little (or move the plane down a little).
  • If you see all white objects when you play the simulation, change the drawmode. For best viewing the results while simulating, use shaded solid. For best performance, use wire. Press D in the 3D window to get this box of choices.
  • Want to change something, create more pieces? You can do it, and run the game engine again. Satisfied with your simulation? We are now going to bake. Save. In the Game menu (in the top of the screen), select Record Game Physics to IPO and Enable all frames. The recording is near real-time.
  • Now you can render, apply subdivsion, apply textures, modify the meshes (as we have used duplicate linked, any modification to a mesh, like a bevel, of a single domino piece will also be done on all others).
Our first try to simulate dominoes. First frame.
Bsod-ridigbodies-domino-firstshot2.jpg
Bsod-ridigbodies-domino-firstshot3.jpg

If wish to modify you simulation, go to the first frame. Move, rotate, duplicate (do not scale) the pieces as you wish. Now, select all by pressing A, insert a keyframe I, then select LocRot. You can Bake again. The simulation below is an expansion of the first. Now, this simulation renders real time in the game engine, but when you bake it, it takes about ten times more.

Extension of the first simulation. First frame.
Frame 250.
Frame 400.
Another application of the methods seen here: A castle of cards

Effective use of the game engine

Our scene setup

Let's suppose you want to interact with the simulation, and to have this baked. We will simulate a game that when you click with the left mouse button, a ball "shoots" against boxes.

How to

  • Create an isosphere, and enable it as an actor, enable Dynamic, Rigid Body, and set Bounds to Sphere. To make it move when you press the mouse button, make the setup similar to this: Create a mouse sensor to the left button, an AND controller, and a motion actuator. Set linV in the axis of the collision (in this case, the Y axis) to a high value, like 50, and link them, so it looks like the pic below
  • Create a cube, enable it as an actor, use the same settings used in the domino piece above. Duplicate the cube some more times, and move the copies so it looks like our scene setup
  • Add a plane to be the floor. Now run the game engine, by pressing P.
  • If the ball goes in the wrong direction when you press the mouse button, you have used the wrong axis, or used positive instead of negative orientation.
  • If you liked the results, enable IPO baking, rerun the game engine, and now you can render, apply textures, export to another program, anything.


Our shooter ball, enabled


Shooting our ball. First frame, motion blurred
First collision.
Second collision.

Huge simulations

The number of objects is limited by just the amount of RAM memory you have, and by the amount of time you want to wait for the baking. Keep in mind that .blend files with many objects with baked animations can consume gigabytes when saved. Also, a simulation that may be feasible in the game engine becomes much more slow when you enable IPO baking, and can crash the computer.

The first piece is about to fall on the second. This domino set, with 289 pieces, when IPO baked for the first 1000 frames (until all pieces had fallen), and saved, produced a .blend file of more than 2GB. The baking process took about 2 hours, and saving this file took 20 minutes, but just running the simulation without baking was about 40 seconds, until all pieces had fallen. The bounds used was Convex Hull Polytope, for max precision. Using Bounds as box would have decreased simulation time with no sensible decrease in simulation realism.
Baking the above simulation. Frame 300
Frame 500.

Hints

  • From the developers of the Bullet engine, when designing a simulation:
    • Do not scale objects. If you do, apply scale with CtrlA
    • Keep the masses for the dynamic object similar. If you put an object of 100kg resting on an object of 0.1kg, is not accuracte and potentially instable.
    • Assign the right bounds type. For a cylinder, choose cylinder, even for non-moving objects (the same goes for boxes and other shapes). Convex Hull Polytope can approximate meshes for moving objects and static objects. Static triangle mesh is good for scenarios and terrain.
    • Do not use high values for gravity
    • Do not use too many vertices in objects with Convex Hull Polytope enabled.
    • Do not use very large or very small objects (less than 0.1 units).
    • Do not use degenerate triangles in the meshes (triangles where there is one or two very acute angles).
  • Objects with higher mass do not fall faster, but require a larger force to be accelerated and stopped. Objects with higher mass only fall faster than lighter objects when there is air drag. You can emulate air drag by using Damp. To simulate water environment, use a very high value for damp.
  • Always save before you are going to do IPO Baking. First, because some bakes can take a long time, and may crash you computer while simulating. After you finished, save as to get two files. Second, for big simulations, the size of the files can be very different. Third, version 2.42 has a bug that sometimes does not allow you to erase IPO baked simulations.

Additional information

  • COLLADA Homepage[1]
  • Developers of COLLADA[2]
  • Definition of the COLLADA format[3]
  • Blender Collada import/export plugin development page[4]
  • Bullet engine homepage-[5]
  • Development of integration of rigid body simulation inside the Blender Animation system. -[6]