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!