From BlenderWiki

< Doc:Tutorials | Game Engine | BSoD
Revision as of 17:37, 13 August 2009 by Camb416 (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Image:LeftArrowHeader.png
[[{{{2}}}|      ]]>
Image:RightArrowHeader.png
[[{{{3}}}|      ]]>
Blender Summer of Documentation: Contents | Manual | Blender Version 2.42

[edit] Making the default cube move, using physics

One of the most powerful features of the Blender GE is the built-in physics engine (Bullet).

By using forces to move an object around the scene, the physics system will automatically handle complex interactions such as resolving collisions with other objects in the scene. For many game types, using Physics will solve more complex issues, but it does require slightly more work to set it up.


We will now create a basic game from scratch using physics. The scene will consist of a sphere, which we will move around the scene using physical forces.


Before we start, reset the scene in Blender to get back to the defaults and set up the 3D view in Blender to look like the image below.

Set up Blender to look like this for the following tutorial
  • Select the File -> New menu, and click on the Erase All option.
  • Rotate the scene using the middle mouse button  MMB Image:Template-MMB.png
  • Change the view into perspective mode using View -> Perspective
  • Change the 3D view to use the Textured shading mode (/draw type)
  • Open the Game (/Logic) panel

[edit] Setting up the game scene

To start creating the game scene, delete the cube by pressing Del.

  • Add in a sphere using Add -> Mesh -> UVSphere.
  • When asked, select the default 32 segments and 32 rings as the setup of the sphere.
  • Exit Edit mode straight away on the sphere by pressing Tab, to return to Object mode.
  • Also, press Alt R to clear the rotation on the object (The object must still be selected).
The Scene.

[edit] Making a model physical within the GE

In the game panel, you will see an unselected Actor button.

Click on this button to enable it for the selected model. (Picture is no longer accurate.)

Select the Dynamic option from the drop-down menu to the left of the "Actor" button you just enabled - This will tell the GE that the model is a physical object. Then, turn on No Sleeping (explained below). OR

Select the Rigid Body option from the drop-down menu. The physical object will automatically rotate correctly using the GE physics engine. If this isn't selected (i.e. Dynamic is selected instead}}, the object will be able to move, but not rotate. No sleeping - The physical object will never be de-activated (also known as sleeping).


Now, press P to enter the GE. You will notice that, even though we haven't added in any game engine logic blocks, the ball starts moving. This is because gravity is affecting the ball, so it falls down. This illustrates one of the features of interacting within a physical world. Press Esc to go back to Blender. Press P again and you'll notice that the same thing happens. Press Esc to return to Blender.


We need to add something for the ball to fall onto, for example a ground object.

  • Add a plane model to the scene using Add -> Mesh -> Plane.

Use the 3D Transform Manipulator to move the plane underneath the sphere, and press P. You will notice that the physical sphere now falls due to gravity, but will land on the plane below it and come to a rest.

We will now scale the plane up (S), so that we have plenty of room to move the ball around within.

Change the 3D transform manipulator from move mode into scale mode.

Grab the scaling handles and size the plane so that it is around 10 times larger in the X and Y directions.

The animating image below illustrates the sequence of steps indicated above.

Adding and placing a ground object

[edit] Moving the physical object within the GE

We will now apply physical forces to the sphere, to make it move around the 3D environment.


IMPORTANT - Make sure the Sphere model is selected. If you have just created the ground plane model, it will currently be selected, so you will have to right-click on the sphere to re-select it.


Add in a new sensor, controller and actuator object in the game panel, and connect them together by clicking and dragging between the dots.

In the motion actuator, we will need to set the values in the Force section in order to move the physical object around the scene.

In the force section, set the 2nd value (Y) to 1.

Now press P. You will then see that the ball falling onto the ground / plane object, and starting to roll.

After the ball rolls a certain distance, you will notice that it starts rolling back on itself. This is due to the fact that we are applying the force locally, along the balls Y axis. as the ball rotates, it's Y axis also rotates, as seen below. Press Esc to return to Blender.

In order to fix this, we will change the force movement from local to global. To do this, click on the L at the right of the Force section to deselect it. Press P again, and you will see that the ball continually moves in the correct direction.

You may not have noticed how the physics engine is working in the background.

As we apply a sideways force to the ball, it will start to roll. This rolling is caused by friction between the surface of the ball and the ground.

Also, as the ball reaches the end of the plane, it will realistically tip off the end of the object, and continues falling.

These are a few of the advantages of using physics within the GE.

Press Esc to return to Blender.

[edit] Controlling the sphere using the arrow keys

We will now take control of the sphere using the arrow keys.

Change the Always sensor to Keyboard, and set it to use the arrow key.

Now, add and connect additional sensors, controllers and actuators, where...

  • The keyboard sensor controls the motion actuator with a -1 in the Y (2nd) location of the Force section (with L for Local de-selected.)
  • The keyboard sensor controls the motion actuator with a -1 in the X (1st) location of the Force section (with L for Local de-selected.)
  • The keyboard sensor controls the motion actuator with a 1 in the X (1st) location of the Force section (with L for Local de-selected.)

When you press P again, you will be able to control the ball and move it around the ground plane. Press Esc to return to Blender.

To make the game panel screen more readable, give the various sensors and actuators suitable labels, such as "up key" for the first sensor, and "push up" to the first actuator.

Also, try changing the values in all of the motion actuators from 1 to 2, and from -1 to -2. Press P now, and you will see the difference in the speed of the ball when using the arrow keys.

[edit] Adding some obstacles into the level

Add a cube to the environment, using Add -> Mesh -> Cube. Drag the arrows of the 3D transform manipulator to place the box somewhere on the surface of the plane object. Repeat this step to add a few more objects to the surface of the plane, including cylinder and monkey objects.

Now, press P. You will see that the ball will automatically collide off the objects that you just added. Again, this is one of the advantages of using physics within the GE. Press Esc to return to Blender.

You might want to try adding in planes and scaling them to make ramps and jumps.

If you have experience with editing models in Blender, you can spend some time now creating a more complex level layout. If you don't have experience modelling in Blender, hopefully this tutorial will have given you an interest in Blender and learning more, including how to edit models. You can see some links to tutorials on editing at the end of this tutorial.

[edit] Making some of the objects physical

Select the cube that you have added into the new scene. In the Game Panel, select the following options, the same ones that were set for the main sphere (except that No Sleeping is not selected this time, to allow the objects to settle and rest / sleep - when an object is sleeping, it takes less time to compute within the physics system)

  • Actor
  • Dynamic

OR

  • Actor
  • Rigid Body

(I recommend making one of the objects a Dynamics object and another a Rigid Bodies object, so that you can better discern the two options.)

Now, press P to play the game, and move the main sphere into the cube. You will see that the cube now gets knocked out of the way.

However, the box moves in a very odd way - it actually moves as if it was a sphere.

Currently the physics system will assume that a newly added rigid body will have a spherical collision shape. Press Esc to return to Blender.

Boundary Display pulldown.

You will notice that there is a Bounds button below the Actor area. Click this button, and an additional dropdown will appear, with the default setting of Box.

In the case of the cube, a Box collision type will work fine. However if you have a more complex shape, you will want to select the Convex Hull Polytope option.

Select some of the other objects you have added to the scene, and carry out the same steps as above, selecting Convex Hull Polytope as the bounds type of the object.

Press P again to play the current level, and roll into the various physical objects to move them out of the way. Press Esc to return to Blender.

--Mal_CanDo 17:00, 18 August 2006 (GMT)








Redirects to fix

  • BSoD/Introduction to the Game Engine → Doc:Tutorials/Game Engine/BSoD
  • BSoD/Introduction to the Game Engine/Moving the cube using no physics → Doc:Tutorials/Game Engine/BSoD/Moving the cube using no physics
  • Blender Summer of Documentation → BSoD
  • Manual/Manual → Manual