Hi this is a short tut showing how to get more out of the state machine using the state actuator to invert states.
This will teach you how to make switches that can toggle on and off.
You can get the the latest apricot builds form here.
http://www.zoo-logique.org/3D.Blender/index.php3?zoo=com
–>Builds are very recent but come without the blender player.
http://www.graphicall.org/builds/index.php
–> Not as recent but comes with the blenderplayer.
1.Make simple scene with a ground plane and 3 cubes… like this:
2.To make things easier… give each object a material with unique colours. You can turn on GLSL shading in the game menu if you wish.
Note: You have to do this if you want see the GLSL shaders but you can use the statemachine without shaders as well.
I will be doing that in this step. Make sure you change the draw type to ‘textured’ to see the GLSL pixel shaders in the viewport.
I usually lower or turn off the specular setting at first… I also increase the emit level to begin with.
It lets me see quickly what’s going on in the scene.
3.Next scale up the purple and green cubes.Make sure you apply the scale by pressing CTRL-A and selecting ‘Scale and Rotation to ObData’.
Always do this if you change the scale of an object and BEFORE you run the game engine. If you don’t your bounds will be the wrong size.
4.Next make the red cube a dynamic rigid body… by clicking dynamic and rigid body under the logic panel (the button that looks like pac man).
Make sure you set the bounds to ‘Convex Hull Polytope’…
You can use box bounds as well but it is less accurate but faster…
It depends on the speed you want your game to run at. Convex Hull Polytope is slower but more accurate collisions.
Rename the red cube ‘player1’.
5.Select the Purple and Green boxy pillar things and rename the purple pillar to ‘pickup_01’.
Rename the green boxy pillar to '‘pickup_02’.
In the game logic panel… make both of the pickups actors and ghosts, by clicking on the actor and ghost buttons.
Why make them ghosts? I did this so player one can easily move through the pickups.
You could make them solid as well… but for this demo it’s just easier to make them ghosts.
You don’t have to worry about setting bounds if the object is not dynamic… it usually defaults to the shape of the object.
If do have problems… go to the game menu… and choose turn on ’ Show Physics Visualisation’… then you can look at the objects to see if they have bounds the are the correct shape and size.
6.a screen with the physics visualisation on… As you can see it appears that the bounds are all fitting snuggly around our objects.
7.Next select the red cube/player1 object… and go to the logic panel.
I’m going to make him drive around using the arrow keys.
So add in a ‘keyboard sensor’… then add in an ‘and controller’ then add in a ‘motion actuator’… link them all together.
For the sake of the tut I’m just going to be moving the player on the global axis.
I want the player to move off to the right and hit the green pickup.
So… in the keyboard sensor… put in the up arrow key… in the motion actuator… add in 10 under the X input and uncheck local.
How do I know that putting in 10 will make my object move off to the right… like I want it to?
Well if you look at the objects axis… you will see the X axis going off to the right. If you wanted it to move left you would put in a minus number.
Note: This is just based off of my camera view so moving your camera around will change what is left and right…
So in a real game I would move the object on it’s local axis… or maybe not…
Depends what kinda affect you are going for.
Also because the object is a dynamic rigid body… you have to move it with Force or linV.
Moving it with Dloc seriously messes with the collision detection.
8.Now if you run the game engine… and press the up arrow… which is going to move the player forward… off to the right…
Yes I know I chose the X axis as my front…
The player will slide along and through the green pick up.
9.Right now would be a good time to make a reset button for the game. So select something that won’t change much… like the ground plane…
Then add in a ‘keyboard sensor’… and an ‘and actuator’ and a ‘scene actuator’.
So set the Keyboard sensor to use the ‘r’ key
Set the Scene actuator to restart.
Hook them all together.
Now when you run the game… press ‘r’ at any point to restart the simulation.
This saves you hours of time when you are testing game mechanics.
The ground plane doesn’t have to be an actor unless you want it detected by a ray (I think)
10.So now I’m going to make the cube move backwards and forwards and left and right… taking the postive X axis as my front facing direction.
You can choose any direction for the front in your games.
So add in the Keyboard sensors and actuators etc you need to do this.
Look in the pic below to see how I’ve done it.
I’ve also named all the sensors and actuators.
It’s good to get into the habit of doing this… but it is slow and boring.
It does help you later on when you start messing with things.
I also rotated the view so the camera is slightly more behind the player object so the controls make more sense.
I also added in a small arrow like object… made by scaling one face on the side of a cube smaller.
I then made it an actor and a ghost and parented to player1.
I did this so I can see where the front is supposed to be.
So as you can see in the pic… now we have the player driving around (with the arrow keys) moving through the two pickups.
11.FIIIIIIIIINALY… now we can start playing with the state machine. Okay now we need to stop and think about this.
I want the following to happen… when the player drives into either of the pickups…
Something must happen to the player… maybe he jumps… maybe a filter turns on?
BUT I don’t want it to go on forever… I want it to turn off after some time.
I also don’t want it to turn on and on and on… every time the player moves through it, if it is already on.
I want it to turn on… do it’s thing… then turn off.
But first a few things about states:
I like to think of states as groups of controllers with sensors and actuators attached and that the sensors and actuators can be shared amongst these groups of controllers.
If you look at the picture below you will see that all the controllers are under a bar with little blue squares.
Each one of these squares is a state. There are 30 of them for each object.
If there is a dark dot in one of the squares… it means the state has some controllers, sensors and actuators attached. If it just an empty light blue square this means the state is empty.
If the square has a dot and is shaded dark blue(see image just below)… this means that this state is currently on.
This means that when you run the game, any sensors and actuators connected to the controllers of this state… will function.
If the square has a dot and is light blue(see the second pic just below for an example)… this means the state is currently off.
This also means that when you run the game any sensors and actuators connected to the controllers of this state will not function… until you turn this state ON!
How do you turn states on and off? You use the state actuator.
so if you look in the image below you should see be able to few things:
- It has two states.
- state 1 is on.
- state 2 is off.
- You can see the sensors and actuators for state 2.