Showing posts with label simulation. Show all posts
Showing posts with label simulation. Show all posts

Monday, January 18, 2010

BGE Blender 2.50 Part 1

Join my Blender 3D forum where you can network with other Blender friends and get your Blender 3D questions answered.

Download the blend file



This is the first in a series of tutorials on the Blender Game Engine (BGE) in Blender 2.5. The purpose of this tutorial is a quick tour of the BGE user interface in 2.5. If you have used the BGE in 2.4x, the process should be fairly familiar to you. Even if you have never used the BGE, after watching this tutorial you should be comfortable enough to experiment with creating more complicated games. I'm using Blender 2.5 Alpha 0, which you can download from blender.org.

So let's get started. We'll use the default cube, in the default scene. We're going to bounce the cube onto a plane, simulating the ground. To create the plane, press Shift-A, then Mesh, then Plane. Scale the plane 10 times (S - 10 - Enter). Position the plane so that its center is roughly at the center of the cube. Select the cube. Move the cube upwards, in the positive Z direction, about 5 blender units, by pressing the G key, then the Z key, then 5. Now the cube appears to be ready to be dropped.

Blender 2.5 contains a series of views that are designed for things you would commonly want to do. Click on the icon to the left of the Default text. You should see selections such as Animation, Compositing, Scripting, and so on. Select Game Logic. Let's do a litte tour of the Game Logic setup. At the upper left is the outliner. In addition to the Camera and the Lamp, we have the cube, which is the active object, and the plane, which is just called Mesh. To the right of the outliner is the 3D view, set to Top View to start. Let's change to front view and rotate the view a bit so that we can watch the cube drop. With the cursor inside the 3D view, press Num1, or from the View Menu select Num1, to go to Front View. I used the middle mouse button to give the view a bit of an angle from the Front View, so you can see the cube drop.

Blender's game engine uses the Bullet Physics library to simulate gravity, collisions, bouncing, and the physics of the game world. This is actually a different type of physics library than, say, the fluid simulator or soft bodies. One of the challenges of working with the game engine is getting used to the differences. Animation and texturing, as examples, work a bit differently in the BGE than in the 3D modeling part of Blender.

Having said that, to make the cube participate, so to speak, in the game, we need to select the cube and click on the Physics buttons (all the way to the right). Right now, the settings for the BGE are not available to us. It appears as if the 3D physics options, such as Fluid and Cloth, are what's available. To make the BGE settings active, change the the renderer from Blender Render to Blender Game. Now the Blender Game Physics options can be set. To make the cube do its thing in the game engine, change the Physics type to Rigid Body. Note that the Actor check box is checked automatically.

This is enough to make the cube participate in the game. As in 2.4x, with the cursor in the 3D window, press the P key to start the Game Engine. The cube falls onto the plane, and rolls over a bit. Press ESC to go back to the 3D view.

Another way to start the Game Engine is to click on the Camera icon, the first one on the left, in the Properties Window. The buttons here also changed when we changed the view from Blender Render (which shows the image and animation settings) to Blender Game. Now, there's a big Start button. We can press the Start button to start the game as well.

Now we're going to add some interactivity to the game. We'll push the cube to the edge of the plane by nudging it a bit, until eventually the cube falls off into oblivion. To do that, we'll work with the bottom middle window. We'll maximize this window by pressing Control - Down Arrow. Then we'll zoom, using the Num- key, until we can see the Sensor, Controllor, and Actuators button groups. There are 3 Add keys. Click each Add key to add a Sensor, a Controller, and an Actuator. Here's the deal. The Sensor looks for some type of activity, in this case it's going to look for a particular key to be pressed. The Controller can control the logic as to what happens when that key is pressed. The Actuator determines what exactly happens after the sensor and controller logic has been set.

Let's make it so that when the user presses the R key, the cube moves a bit in the X direction. To do that, click on the Always dropdown in the Sensor area. Note that there are all types of sensors. We're going to use the Keyboard sensor. Select Keyboard. Position the mouse at the Key area, and left click. The prompt Press A Key displays. Press the R key. Any key will do.

Now go to the Actuators area. In the first Loc area, enter 0.5. The idea is that when we press the R key, the cube will go .2 Blender Units in the X direction. If you had entered 0.2 in the second area the cube would go .2 Blender Units in the Y direction, and similarly 0.2 in the 3rd area would make the cube go .2 Blender Units in the Z direction. There are other things you could have made the cube do, such as add a force or rotate (torque).

Finally, we need to hook up the Sensor, Controller, and Actuator. Connect the right socket of the Sensor with the left socket of the Controller. Then connect the right socket of the Controller to the left sockete of the Actuator. Now, when the BGE detects the R keypress, the cube will go .2 Blender Units in the X direction.

Let's see if this is what happens. Press Control-down arrow to return this logic window back to its original position. Click the Start button. The cube does what it did before. Now I will press the R key a few times. The cube bounces up a bit because that's what the Bullet Physics calculation might think a cube resting on its point might do as well. Eventually, the cube falls off the plane and bounces into space.

Let's add some more interactivity. Add an icosphere next to the cube (Shift-A, Mesh, Icosphere), accepting the defaults. Position the icosphere under the cube, between the cube and the plane. Go to the Physics tab. Give it a Rigid Body physics preoperty. Let's make the icosphere go .2 Blender Units in the -Y direction when we press the Space Bar. Maximize the bottom middle window, the one with the Sensor, Controller, and Actuator tabs. Press the 3 Add buttons for Sensor, Controller, and Actuator. Select the Keyboard sensor. Move the cursor to the Key area and left click. When prompted to press a key, press the Space Bar.

Press the P key in the 3D window to start the game. If you positioned the icosphere underneath the cube, watch the cube hit the icosphere and then react as if it was affected by the collision. Depending on how large the plane is and how big the cube and icosphere are, you might get different effects. The cube and or the icosphere might fall off the plane. Pressing the space bar causes the icosphere to move.

I hope this gives you a start with using the Game Engine in Blender 2.5. Press the SUBSCRIBE button on Youtube to make sure you won't miss my future BGE and Blender tutorials. Happy Blendering!

Wednesday, October 28, 2009

2.49b Particle Objects



Blender's particle system is flexible and powerful. It can be used for simulating effects like fire, hair, fur, rain. Particles can be emitted from any kind of mesh object. The purpose of this video is to acquaint you with the basic controls for a particle emitter. We'll use the plane, which is the simplest to show. Switch to animation view, with 100 frames.

1) Delete the default cube (Right click, then X, then confirm delete). Add a plane (Space - Add - Mesh - Plane). Subdivide the plane once (W key, then subdivide). Then go to the SR1-Animation view.

2) Go to the Object buttons (F7), then click the Particles button, all the way to the right. We will look at emitter type particles, where the number of particles and their direction change over time. Emitters are good for animating rain and fire, where raindrops and smoke particles move over time. The hair type is for more static simulations, like hair and fur.

3) Particles are emitted from faces of the plane. The default is 1000 particles over 100 frames = 10 particles per frame.

4) Increase the number of particles to 10000. You see that they're emitting from each face by default. Can change to emit from vertices, as well as volume.

5) Particles are most often emitted from the "normals", the direction that's perpendicular from the face. The Normals setting controls how high the particles are emitted. Set the normal to 1 to show how height is controlled. Press F12 to render.

6) Random adds a bit of randomness to the emitting direction. You can mix normals and random and press F12 to rendeer.

7) AccX, AccY, and AccZ can add an extra push in the respective direction. Set AccX to .5, which gives bias in the X direction of .5 blender units. This is good for fire and smoke effects. Press Alt-A to see the path of the animation.

8) Emitting also works in negative direction. For rain, if you float a plane above the scene, and make it a particle emitter, this would be the start of a rain simulation. You can increase the randomness as well. Set Normals to -.5 and AccX to -.3. Properly textured, this could simulate a heavy rainstorm.

9) Start and End are the start and end frames for the particles. Life is how long the particle lives. Increasing the life makes the particles stay around longer. Start can be negative, which means that the particles are alive at the beginning of the animation.

Increase the life to 100. The particles stay around longer. For fireworks, you might want to vary the life and add some randomness to the emitter.

10) Start can be a negative number also. A negative start number means the simulation began before the first frame is rendered, like the fire is already going at frame 1.

11) Go to a frame 41 and press F12 to render. The particles render as halos, and the plane does not render. The emitter does not render by default.

12) Add a material, F5, shading, make the particles red (R=1, G=0, and B=0). Then press F12 render.

13) To change the halo effects, press the Halo button in the Render Pipeline panel. In the Shaders panel, you can change the halo size. You can also customize the halo - Rings, lines, stars.

Render (F12) for
1) Halo with 4 rings and .5 size.
2) Halo with size .1, 3 stars, and 4 lines

14) Many material settings can be animated. Look at Halo Size. Go to IPO window. Select the Material type curves. Then select HaSize. Create an IPO curve, with Control-Left Click, creating IPO points for size. This creates a Bezier curve for the halo size, which will make them emit with different sizes over time. Here's the resulting video.

These are the basic controls and look for emitter type particles, which work well for fire and rain. We've barely scratched the surface. I hope this gives you a good start towards understanding Blender's partcle system. Happy blending!

Friday, October 23, 2009

2.49b Fluid Simulation



The purpose of this video is to show how the objects in Blender's fluid simulator work. We'll look at domains, obstacles, inflow, outflow, and of course fluid objects. Blender's fluid simulator at first seems very challenging. However, if you think about how fluids behave in real life - think of water pouring out of a faucet into a sink or a bathtub, the fluid simulator starts to become more logical. In this tutorial we're going to focus on the objects in Blender's fluid simulation.

Let's start with the most basic fluid simulation. At a minimum, the fluid simulator needs two things: a fluid, which can be free flowing like a waterfall or muddy like water in a pond; and a domain, which is an area in which the fluid lives. Be warned. This doesn't look exactly like water dripping from a faucet, but it does look like some icky gook falling down in a fluid like manner.

So start Blender. Usually we delete the default cube. This time, however, we will use the default cube. It will be the domain, the area in which the the fluid lives. Go to Front View, by pressing NUM1. The blue Z arrow will point up. We need this because fluids are affected by gravity. In Blender, Z is up and -Z is down.

Let's make the domain a bit higher along the z axis. Press the S key to scale, then the Z key, then scale the cube up 4 blender units. Grab it, with the G key, and move it along the Z direction (Z key) so the domain sits on the X axis. Before we do anything with the cube, duplicate it (Shift-D) and move it away from the original cube. You'll see later why this is a good idea.



Just to track things, let's name our objects. Select the original cube. Press the N key to bring up the Transform Properties window. Rename the cube to Domain. Make sure the cube is in object mode. Press Tab if the cube is in Edit mode.

To tell the fluid simulator that the cube is the domain, press the Object buttons (F7), then the Physics button, the second button in second group of buttons. Press the Fluid button the enable the fluid simulator, on the extreme right. Click the Domain button.



Press the Z key to go into wireframe mode. You can see objects inside of other objects in wireframe mode.

We need to add a fluid. Add an icosphere (Space - Add - Mesh - Icosphere), accepting the default of 2 subdivisions. Scale it down using the S key, so that the icosphere looks like a drop and is positioned at the top part of the cube. Make sure that the icosphere is inside the cube. Check out all the views and rotate, to make sure.

In the Transform Properties window, rename the icosphere from Sphere to Fluid. Let's make the fluid source green, just so we can see it. Press F5 to add a material. To make the fluid source green, set R to 0, G to 1, and B to 0.

Select the domain and go to the material buttons. Make the domain color red by setting R to 1, G to 0, and B to 0. Weird things happen to the domain during the fluid simulation, as you'll see.

To tell fluid simulator that the icosphere is the fluid, make sure the icosphere is in Object mode. Then press the Object buttons (F7) and the Physics button. Press the Fluid button to enable the fluid simulator. Click the Fluid button.



We're set up to run the fluid simulator. The cube is the domain and the icosphere is the fluid. To run it, select the cube. In the fluid simulator buttons for the cube, press the big BAKE button. The cube turns into a blob and starts falling down like a drop. The falling stops where the cube used to be, as if the cube was still there, although it doesn't show.

Believe it or not, we've created a fluid animation. Press Alt-A to see it. Press Esc to stop the animation. The animation is done over 250 frames by default. To create an animation, press F10 (Scene buttons). Set the Output Directory to the directory where you want to store the animation. In the Output panel, navigate to the directory and then select Select Output Pictures. In the Format panel, select the type of video you want to create. The default is jpeg. I like the .avi format, with the CamStudio 1.4 Lossless Codec. I use CamStudio to capture screen shots for these tutorials. If you choose uncompressed the video will take a lot of space. Click the ANIM button and wait. The animation is being created, all 250 frames of it.

Depending on your computer, the animation can take a long time, up to 10 minutes.

I paused the video because it's kind of boring to watch the animation being created frame by frame. Here's the result.



Some things to note. The fluid looks too geometric to be believable. We can help this by smoothing the fluid. Select the domain object, which controls the color of the fluid as it falls. Go to the edit buttons. Press the Set Smooth button. Also, subsurfing the fluid will help. Add a subsurf modifier at Level 2. Select the domain. Press the BAKE button. For the sake of speed, let's reduce the number of frames to 50. At 25 frames per second, this is a 2 second video, but it illustrates the point.

Select the Fluid object. Go to the Physics buttons and change its type from Fluid to Inflow. Here's the result.


The fluid is more, well, fluid-like. An inflow is a fluid object that adds fluid. Blender allows more than one fluid source, i.e. more than one inflow, like more than one faucet or shower head. However, only one domain is allowed.

We're going to add an obstacle. Add a cube below the path of the fluid. Select the cube, making sure it's in Object Mode. Click on the Object buttons, then the Physics buttons. Go to the Fluid simulator. Enable Fluid and click on Obstacle. Running the fluid simulator produces the following result.


Finally, we're going to add an outflow, which takes fluid away from the simulator. An outflow is like a bathtub or faucet drain. To do this, add a plane above the cube, in the path of the fluid. Select the plane, in object mode. Go to the Fluid simulator, as before. Enable Fluid and click the Outflow button. Select the domain and press the Bake button. The animation should look as shown, The fluid disappears before it hits the obstacle.

Remember the cube domain that we duplicated before? To make the simulation realistic, position it on the domain boundaries. You'll need to make it transparent and add materials and textures, no doubt. And probably you will want to apply a realistic water, oil, or mucky texture to your fluids. But that's it. We've done a basic fluid simulation, with a fluid and a domain. We've added an obstacle - in real life, something like a bathtub or sink, as well as an outflow, like a drain. I hope this gives you the basic idea of how it works. Happy Blending.