Showing posts with label blender game engine. Show all posts
Showing posts with label blender game engine. Show all posts

Thursday, February 18, 2010

2.49 Skybox for BGE



Creating a great background is a huge help towards making your Blender game realistic. Unlike in Blender 3D, where you can create a world as a background, in the Blender Game Engine, you need to use an actual textured mesh as your background. This mesh is called a skybox. Inside the skybox is a projection of a 360° environment. When the player (camera) is inside this environment, the scene is rendered with the illusion of being inside a gigantic world. This tutorial shows you how to create such a skybox. It's based on the Build a Skybox tutorial at tutorialsforblender3d.com. We will also animate the camera inside the Blender Game Engine so we can tour our imaginary sky box world.

Tuesday, February 2, 2010

BGE 2.49 Character Creation

Discuss this video at Ira Krakow's Blender 3D Forum!



Creating a character for Blender's Game Engine is different from creating a character in Blender 3D. In Blender 3D, you have a wider range of options for creating detailed characters, such as adding geometry for details, using curves and surfaces, and sculpt mode. These high-polygon models work well for rendering. However, if such a model is placed in the Game Engine, the animation would slow down to an unacceptable level. Some commonly used techniques, such as subsurf, are not available in the BGE. BGE characters should be low-polygon for best performance.

However, they can't be too low-poly, because the characters will look too much like rectangles and squares. In addition, you need enough geometry to do realistic animations in the BGE, such as walk cycles and lattice deformations. So creating a character for the Blender Game Engine is a tradeoff. In this tutorial, which is based on Tony Mullen's discussion in Mastering Blender, a book I recommend if you want a great introduction to Blender in more depth, I show a recipe for creating a character for the BGE. It's more a set of steps than a fully realized character. I leave that to you. I encourage you to use these steps to create your own character and post your result on my Blender 3D Forum at http://forum.irakrakow.com. I used Blender 2.49b.

So let's get started. Start with the default Blender scene. Go into Front View (Num1), the best view for the BGE because it shows the effect of gravity. Make sure you are in Edit mode, with all vertices selected. Press the W key to bring up the Specials Menu. Select Subdivide Smooth, accepting the default of 1.00 by clicking OK. The result is a polygon with 24 faces.

Press the Z key to go into wireframe mode. Deselect the 3D Transform Widget, which gets in the way of this demo. Press the A key to deselect all vertices. Position the cursor in the lower right area of the polygon, and press the B key to box select. Box select the lower right vertices. Press the E key, and select Region, to extrude the region of vertices. Scale the extruded vertices by pressing the S key, and then the Z key, and then the number 0. Finally, with the vertices still selected, press the E key again and extrude the vertices downward to complete the leg.

We're going to model the rest of our character with the Mirror Modifier. To do that, press the A key to deselect the vertices. Box select (B key) the vertices on the left side of the model. Press the X key to delete, and select Vertices. Now we have half the model. We're going to mirror the model along the X axis. Go to the Editing Buttons (F9). From the Modifiers tab, click the Add Modifer button. Select Mirror. You should see a mirror of the right side of the model on the left side. Make sure that the X button and the Do Clipping button are enabled, and that the Merge Limit is 0.001.

Now we're going to do the shoulders and arms. Box select the upper right vertices. Press the E key to extrude the forearm. You can use the S key to scale the forearm either more or less. Press the E key again to extrude another part of the arm, and use the S key to scale it down. Press the R key to rotate a bit as well. Extrude one more time by pressing the E key. Note that the left arm is being created as well, a mirror of the right arm.

Let's bend the knees a bit. Go to side view (Num3). Box select (B key) the knee vertices (the ones in the lower middle of the leg), and move them a bit on the Y axis by pressing the G key, then the Y key, then moving the vertices, then pressing Enter.

Let's add a head. Go to Front View (Num1). Press Alt-A to deselect the vertices. Box select (B key) the upper right vertices. Press the E key, selecting Region, to extrude the head upwards.

The shape of the character is done. We don't need the Mirror Modifier any more. To apply the Mirror Modifier to the model, press Tab to go into Object Mode. Then press the Apply button to add the geometry on the left side to the mesh on the right side. Pressing Tab to go into Edit mode shows that we have a complete character. We need a bit more geometry to make this character work well in the BGE. To do that, press the A key to select all vertices. Then press the W key for the Specials menu. Select Subdivide Smooth. Our character has a few hundred faces, enough for moving around in the BGE but not so complex as will slow down the BGE.

We're going to paint our character using Texture Paint. To do that, we first need to mark a seam for unwrapping. Go to Front View (Num1). Position the cursor at the center of the character, at the border of the left and right part, and press Alt-Right click to select the edge loop. Press Ctrl-E to bring up the Edge Specials menu, and select Mark Seam. Split the 3D window to create a UV / Image Editor in the right part of the window. Press the A key to select all vertices. Then press the U key, selecting Unwrap, to unwrap the mesh. In the UV / Image Editor, select Image, then New. The default size is fine. We want to click on the UV Test Grid as well. Then press OK.

Change the view to Textured. At this point, we can change the mode to Texture Paint and actually paint our character. We can play with the brush, opacity, size, blend, and other settings by changing the options in the Paint panel.

At this point, we have a modeled and textured character. Save your Blend file because we will use it in the next video, to rig our character and have it fully participate in the game. Just to prove that our character works in the Blender Game Engine, go to the Logic Buttons (F4). Change the Physics type of our character to Rigid Body (Dynamic would work as well). With the cursor in the 3D window, press the P key. Our character falls int the abyss, pulled by the Bullet Physics calculations. Congratulations, we have made a character for use in the Blender Game Engine. Show off your character at http://forum.irakrakow.com. Subscribe to my Youtube videos. See you next time. Happy Blendering!

Friday, January 29, 2010

BGE Blender 2.49b Part 3

Discuss this video at Ira Krakow's Blender 3D Forum



The purpose of this video is to show some basic sensor and actuator behavior. We'll look at the Always, Touch, and Keyboards sensors. I'll show you some basic techniques for moving an object, how the movement interacts with gravity, how to bounce an object, and how to replace one object with another object. I used Blender 2.49b for the demo.

Setup:
We'll use the default cube. Go to Front View (NUM1), the best for the BGE because it shows the effect of gravity the best, as objects actually fall down in response to simulated gravity. Add a ground plane for the cube to bounce off of (Shift - Add - Mesh - Plane). Scale the plane 8 times (S - 8 - Enter). Move the plane down about 5 Blender Units. It doesn't really matter - you could have moved the cube up as well to get the same effect. Select the cube. Go to the Logic buttons (F4). Select Rigid Body from the Physics types dropdown. This turns on the Actor button as well, so that the cube becomes an actor, with the realistic Rigid Body physics applied, in the Game Engine.

Show:
1) The default sensor is the Always sensor. Click the Add buttons to add a Sensor, a Controller, and an Actuator, to the cube. Wire up the logic bricks, connecting the sensor to the controller and the controller to the actuator. Enter .10 in the LocX area. What this logic block is saying is that, each time the logic blocks are evaluated (the Always sensor - it's not strictly Always, it's when the logic is evaluated), the cube will be moved .10 Blender Units in the X direction. Press P to start the game. Note how the cube moves in the X direction (because of the logic brick), as well as down (because of the rigid body physics) until it touches the plane (which is an obstacle that prevents the cube from falling any further). For a while, the cube moves in the X direction on the plane, until eventually it falls off the plane. Press Esc to end the game.

2) How to make the cube bounce. Enter 0 in the Loc X area. Now enter .1 in the Loc Z area, the third location area. Press P to start the game. Now, on every evaluation of the logic, the cube goes upward in the Z direction .1 Blender unit. At the beginning this causes the cube to go up a bit. Eventually, however, gravity takes over, being more powerful than the displacement in the Z direction, and the cube bounces down, until it hits the plane. After that, the cube bounces up in the Z direction, then gravity takes over, in an infinite cycle. Press ESC to stop the game.

3) Perhaps you want the cube to start bouncing only when it touches the ground, allowing gravity to control the cube fully before it hits the ground. To do that, change the Always sensor to a Touch sensor. Press the P key with the cursor in the 3D window to start the game. Note that the bounce doesn't take place until the plane touches the ground.

4) How to make the go in the X direction. Set the LocZ parameter back to 0. Set the LinV, linear velocity, to 0.10. This makes the cube's speed, in the X direction, become .10 blender units, every time it touches the plane. The cube indeed goes into the X direction that way, but its motion is unrealistic because the cube seems to penetrate into the plane. Perhaps you can do better. Hint: a more realistic way is to use the Servo type of motion. Post your render at http://forum.irakrakow.com to show the cube moving more realistic, without going into the cube. Press ESC to end the game.

Now click the little Add button on the LinV row, at the right. What that does is add .10 Blender Units to the speed of the cube. It accelerates quickly along the X axis until it falls from the plane. Press Esc to end the game.

5) Here's an interesting effect. We can replace one mesh with another while the Game Engine runs. We'll use the Keyboard sensor to trigger this effect. We will replace the cube with Suzanne. Here's how it's done. Go to Level 2 by clicking on the second rectangle. Add Suzanne (Space - Add - Mesh - Monkey). Click back on Level 1. The Game Engine only shows the objects on Level 1. We'll substitute the cube with Suzanne when we hit the R key (for replace, I guess). First, change the Sensor to a Keyboard sensor by selecting Keyboard from the Sensor Type dropdown. Left click in the Key area. In the rectangle, where it says Press A Key, move the cursor. Then press the R key. Now the key area displays the letter R. Whatever happens will be triggered when the user presses the R key.

In the Actuator area, select Edit Object. Below is a popup menu which lets you select the type of object editing you want. Select Replace Mesh. In the Obj: field, enter Suzanne. Wire the sensor to the controller and the controller to the actuator. Run the game by moving the cursor into the 3D window and pressing the P key. Press the R key. Suzanne magically replaces the cube. This is a great technique if, say, you want, say, your Cinderella mesh to be replaced by a pumpkin, as in the story. Press Esc to end the game.

6) Now suppose you want the cube back. We can show and hide the details about the sensor and actuator by clicking on the little arrow at the right. This is useful when you want to show a lot of logic bricks in a small area. So click these arrows to hide the details about the sensor and actuator. Let's make the C key restore the cube back. To do that, we need another set of logic bricks. Click the Add buttons on the Sensor, Controllor, and Actuator tabs. Set the sensor type to Keyboard. Left click in the Key area. In the rectangle where it says Press a Key, press the C key. Now the key area displays the letter C.

In the Actuator area, select Edit Object. In the popup menu which lets you select the type of object editing you want, select Replace Mesh. In the Obj: field, enter Cube. Wire the sensor to the controller and the controller to the actuator. Run the game by moving the cursor into the 3D window and pressing the P key. Let the cube fall to the ground. Press the R key first, replacing the cube with the monkey. Press the C key next, replacing the monkey with te cube. Pretty neat...you can make objects appear and disappear.

One last thing I want to point out before my 10 minutes runs out. Don't worry. I'll show more stuff in the next part. It's a good idea to name your sensors something meaningful instead of Sensor and Sensor1, the default names. To do this, expand the details for each sensor. Name the first sensor Monkey. Then click the arrow to hide the details. Expand the details for the second sensor. Name the second sensor Cube. Now you can tell what each sensor does.

I hope this gives you a better idea of how to wire up logic bricks to do basic object movement, as well as how to replace one object with another under Game Engine control. Leave your comments at http://forum.irakrakow.com. Happy Blendering!

Tuesday, January 26, 2010

2.49b BGE (Animation) Part 2



The purpose of this video is to compare animation in Blender 3D modeling with animation in the Blender Game Engine. The animation results are different because the BGE uses a different physics engine, Bullet Physics, than the 3D animation engine. As you will see, because the default frame rates are different, animations generated in the BGE are faster (60 frames per second) than in Blender 3D (25 frames per second). I will show how to visualize animations in both, how to synchronize the animations between the BGE and Blender 3D, and the basic visualization settings in the BGE that will let you fine tune your game animations.

I am using Blender 2.49b because, as far as I can tell, this is the only Blender version where you don't need Python scripting to synchronize the animations. Versions before 2.49 required you to have a Python script to set the default frame rate in the BGE. Blender 2.50, Alpha 0, has not fully implemented BGE animation yet. In 2.50 alpha 0, you can run a game and show visualizations, but you can't do animations in Blender 3D and have them show up in the BGE, or vice versa. Both of these can be done in 2.49b. I'll show you how to do both and why it's a good idea to be able to move smoothly back and forth between Blender 3D and the Blender BGE. So 2.49b seems to be the way to go if you want to do animation with the Blender Game Engine.

By the way, I have not seen this type of discussion anywhere, either as a video, in documentation, or in a book. The closest is Tony Mullen's book Bounce, Tumble, and Splash, which is an excellent introduction to Blender physics libraries such as the fluid simulator, soft bodies, cloth, boids, particles, and so on. However, he uses an older Blender version which needs a Python hack to synchronize the animation speeds between Blender 3D and the Blender BGE. My conclusions are the result of pure experimentation. If you have any comments on this, please join my Blender 3D forum at http://forum.irakrakow.com, and comment on this video.

Here are the steps:

a) Setup: Go into Animation View by selecting it from the different views available. Go into Front View, the best view for the BGE because of the gravity simulation, by pressing Num1 or View Front from the menu. Start by deleting the default cube (Select, right click, then confirm with the Delete key), Add an icosphere (Add - Mesh - Icosphere), accepting the defaults. Add a plane (Space - Add - Mesh - Plane). Tilt the plane 45 degrees (R - 45 - Enter) to tilt it. This will make the icosphere fall off the plane when we press P to play the game in the Blender Game Engine.

b) Before running the BGE, we need to make the icosphere an Actor, with rigid body physics. The plane will be an obstacle for the sphere, which will react according to gravity by falling off the plane. Movement of the sphere down the plane is dependent on gravity and the calculations of the Bullet Physics engine, as well as the mass of the object and other things. Press the P key with the 3D cursor in the 3D window to run the game. Press Esc to stop the game.

c) We can generate IPO curves from running the game. Check the Record Game Physics to IPO check box. Press the P key to run the game. Note how the IPO curves are generated in the IPO Curve Editor.

d) Here's how to fine tune collisions in the game engine. Select the icosphere. Select Bounds, and then Sphere. Check the Show Physics Visualization menu item from the Game menu. The visualization shows the rectangular area which is the collision area of the plane, as well as the spherical area which is the collision boundary for the sphere. This is how you check for valid collisions in the game engine. Press the P key to run the game. Press Esc to stop the game.

e) Let's see how our animation runs in the 3D part of Blender. Run the animation with Alt-A. Note how the animation runs much slower. Why is this? This is because the 3D animation is at 25 frames per second. Go to Scene buttons and find the FPS indicator. The BGE runs at 60 frames per second. In the World buttons, and this is ONLY in 2.49, in the Mist/Stars/Physics panel, the Physics part refers to the Blender Game Engine. The dropdown shows Bullet, the default engine. There's also Servo Physics. We'll leave the engine at Bullet. The FPS is at 60. We can change it to 25. Then rerun the BGE by pressing the P key. Press the ESC key and new IPO curves are generated.

f) We can also change Gravity. The default is earth's gravity at 9.8. Let's decrease it to 2, simulating a planet with less gravity. Press the P key to rerun the BGE. Press ESC to end the game and generate new curves.

g) We'll change gravity back to earth, 9.8. We're going to clear out the IPO curves we created. Press the A key in the IPO Curve Editor to select all the curves. Press the Delete key and confirm the deletion.

Another way to generate IPOs, which bypasses actually running the game, is to press Ctrl-Alt-Shift-P with your objects in the position as if you were going to run a game. Go to Frame 1. Position the icosphere above the plane. Disable Record Game Physics to IPO in the Game Menu. Instead, press Ctrl-Alt-Shift-P. The Bullet Physics engine runs, generating the IPO curves. Press Alt-A. Change gravity to 16. Press Ctl-Alt-Shift-P. Then press Alt-A. We now have the IPOs for gravity in a bigger planet, like Jupiter.

h) Let's go the other way, from 3D to the Game engine. We'll create a simple animation in Blender 3D. First, get rid of the previous IPO curves by going to the IPO Curve Editor, pressing the A key to select all the curves, and pressing Delete. Set keyframes from 1 to 100. Start with the ball. Keyframe it at Frame 1 by pressing the I key and selecting LocRot. Go to Frame 100. Move it 5 or so Blender Units up. Keyframe it by pressing the I key and selecting LocRot. Press Alt-A. Now we have an animation in Blender 3D over 100 frames.

i) We can get the Game Engine to run this animation using Logic Bricks. Do the Always Sensor with the IPO actuator. Select the Play type. Connect the sensor to the controller and the controller to the actuator, as usual. Make the end frame the same as the end frame in the 3D engine. Then run the BGE, to show that the BGE can run the IPO curves created in the IPO Curve Editor.

I hope you now have a better idea of how animation works in Blender 3D as compared with the animation of the Blender Game Engine. They're totally separate, yet, with more understanding of how Bullet Physics and gravity work, they can work well together. I look forward to seeing your animations, both in the BGE and in 3D, on my Blender 3D forum at http://forum.irakrakow.com. And don't forget to subscribe to my videos as well. Happy Blendering!

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!