Your first Blender game in 15 minutes

Just last week, I took three students with me to attend the 10th International IT Seminar for Students in Madrid, Spain. Our initial plan had called for two teachers, four students, and a demo of Arduino robotics, but due to financial constraints, we had to redesign at the last moment. This led me to take a team well versed in Blender and Unity instead. Usually, the faculty members give a lecture first in the morning and then the students lead the other students in workshops. But since we were only four, I pitched in and did a workshop on creating your first Blender game, a labyrinth for a ball with physics governing the movement, in 15 minutes. And this is how you do it.

Let’s say you want to have a labyrinth field, which you can tilt with the arrow keys, and with holes the ball can fall through, which signifies a fail. All you need is a plane, a sphere, and a second plane below the labyrinth so you can easily detect the ball falling off the labyrinth. And you need the Blender Game Engine, to make it all work, but fortunately, it is included in Blender for the same price, e.g. for free. You design the pieces in the default view, but when you’re done, you go into the Game Logic view, and this allows you to attach the sensors, controllers, and actuators to the pieces so you can make it move.

The labyrinth

The labyrinth
The labyrinth

This is your playing field. It is a plane 2 x 2 Blender units in size, but the size is irrelevant. Above the field, you can see the ball, and below it, in green, the detection plane. The making of the plane is rather trivial – all you do is Shift+A and add a plane, then subdivide it a few times to get the squares to delete some from, and raise the edge by selecting the outermost faces and extruding up 0.1 Blender units. Remember to rename the plane to Labyrinth, or something else recognizable.

Also, add a UV sphere, and make it small enough to fit through the holes. Place it slightly above the labyrinth plane, as you can see in this image. Call it Ball. The ball by default is not entirely spherical, as it is formed out of squares, but if you want, you can use the Subdivision Surface modifier to make it fully round.

The plane below is necessary (but not the only way) for detecting the ball falling through the hole. The plane is stationary but it is collision-enabled, so you can attach sounds or actions to it when the ball touches it. Call this plane Detector or something logical.

 

The physics

Physics tab in Properties
Physics tab in Properties

Now you need to set up the physics. The Physics tab is the rightmost one on the Properties window. When you click it, you can set the physical properties of objects (virtually of course).

So, with the labyrinth plane selected, click Static. This makes the plane impermeable for the sphere.

The sphere needs to be of the type Rigid, because it has to be a hard object, and to listen to the physics engine’s commands as you tilt the table. Click the sphere and make it a Rigid in the list. For now, you need to change nothing else – the physics engine is vast and worth a few blog posts on its own.

Click the Detector plane and make its type Static too.

You can now test the game. Change the render engine from Blender Render to Game Engine in the middle of the top of the screen, place the cursor in the 3D view, and press P. The ball drops on the plane, and doesn’t pass through it, but stays on the plane. It won’t do anything else, because the labyrinth plane is level, but if it falls through , you know to go and check the settings.

The game logic

The game logic window
The game logic window

The interesting stuff happens when you change the screen layout to Game Logic. It is on the top of the screen, where it usually says Default. The Game Logic brings you this view with the Outliner on the left, the 3D view in the middle, the text editor on the right, and the actual cool stuff at the bottom: the Logic Editor. On the right bottom you have the Properties window.

Sensor list
Sensor list

The Logic Editor lets you pick an object and then attach sensors, logic, and actions to it. In our case, you want the UP key to tilt the plane top up, the LEFT key to the left, etc. This is achieved by selecting the plane, and then, in the Logic Editor, clicking Add Sensor. This opens a long list of sensors you can pick from, and in our case, we need the Keyboard. While you are at the list, take a moment to appreciate the variety of things you can monitor. Oh hey – every time you add anything in Blender, rename it at the time of creation. Otherwise you will have things like

Keyboard.003 instead of KeyLeft or something intelligible. Then press the key you want to access. Let’s do the UP key first, so rename the sensor as KeyUP, then find the Key button and click on it. Now you can press the key you want to watch for, ie. the UP arrow. This time you don’t need to do anything else here as we are not scripting at all.

Controller list
Controller list

Then, click Add Controller. This to me is a little confusing, as the things you are adding are things like AND, OR, NAND, XOR, and other logic pieces. It is usual to use just the AND because that connects the sensor to the actuator directly – when the sensor is triggered, the action is also initiated. But again, rename the controller to KeyUP. The little triangle in the top left corner lets you to compress the dialog to just the title, so you will soon see the benefit of naming things correctly.

Actuator list
Actuator list

Last, click on Add Actuator. The Actuator list is even longer than the other lists, so you can command Blender to do pretty much anything. We need to attach motion to the plane, so select Motion. Leave it as Simple motion. What you want the UP key to do is to rotate the plane along the Y axis when you press it. To do this, find the actuator Motion, and in it, enter the value -0.2 degrees into the Y axis Rot field. A larger value will tilt the labyrinth faster, making the game harder.

The sensors, controllers, and actuators need to be connected to work. To do this, see the black dot on the right side of each element, and drag from that dot to the ring on the left side of the next element. This is a technique much used in Blender: for example, in the Cycles render engine, you have a similar system of creating nodes of properties and then connecting them by dragging between connectors. This is the look of your setup after you have done all three phases:

Connecting the nodes
Connecting the nodes

When you now press P, or go to the Game menu on the op and select Start Game Engine, the ball will drop on the labyrinth, and when you press the UP arrow, your board will tilt and the ball will run across the labyrinth. When you now add three more of each element, using the same structures, you can get all four arrow keys to work. The DOWN arrow should be set up to rotate the field along the Y axis for 0.2 degrees, LEFT on the X axis for -0.2 degrees, and the RIGHT key on the X axis for 0.2 degrees. Remember that if you choose to change one of the tilts for speed, you have to edit the remaining three values as well.

To end the game, press ESC. You can also create another keyboard sensor, perhaps for the R key, and then wire it to an actuator of the type Scene. In it, there is an option for restarting the game.

I will write another entry on the blog regarding the plane below the field that can recognize the ball falling through and play a sound. You can use the same methodology then to create targets on the field for example, to make the game more interesting. Of course, all the elements in the game engine are scriptable, so you can write code for creating a scoring system, or whatever you like.

The video tutorial

Loading

0 thoughts on “Your first Blender game in 15 minutes”

  1. Pingback: Quora

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.