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!