game physics and paths
By Dave jarvis.
This is a complex, advanced tutorial showing how todo create an animation using Blender game physics engine and paths. A sphere Will be dropped onto a plane, moved along a Path, and then dropped onto a box.
Blender has the ability todo récord physics-based actions todo an object ipo curve. This functionality allows a recorded physics sequence todo be rendered as an animation. Since paths control objects independently of physics, Path motion is not recorded without special intervention. Let us now intervene.
setup
Environment.
1. Press [Control + x] for a new file.
2. Click ok todo confirm.
3. Click sr:2-model and change it todo sr:1-animation:
Layout menú
4. Split the right-hand side of the 3d view window into two new Windows.
5. Set the upper window todo the ipo curve editor.
6. Set the lower window todo the text editor.
7. Remove the outliner window todo increase the 3d view space, resulting in:
Blender Layout.
objects
1. Add » Mesh » vSphere.
2. Click ok todo accept the default options, then [tab] todo exit edit-mode.
3. Press [s], 0.25 then enter todo Shrink the sphere.
4. Press [g], [x], 2 then [enter] todo move the sphere right.
5. Press [g], [y], 2 then [enter] todo move the sphere back.
6. Press [g], [z], 4 then [enter] todo move the sphere up.
7. Add » Mesh » plane, then [tab] todo exit edit-mode.
8. Press [s], 5, then enter todo scale the plane.
9. Press [numpad1] for a front view:
Sphere, box, and plane.
10. Add » curve » Path, then [tab] todo exit edit-mode.
11. Press [g], [y], 2 then [enter] todo move the Path back.
12. Press [g], [z], 0.25 then [enter] todo move the Path up.
This Will align the Path first vertex with the sphere center. After it falls, the sphere Will move along the Path. There are other ways todo align vértices that dont rely on premeditated positioning, as in step 18.
13. Object » mirror » x local.
14. Press [numpad7] for a top view.
15. Press [tab] todo edit the Path.
16. Press [a] todo deselect all vértices.
17. Click RMB todo select the last vertex (left-most).
18. Press [shift+s] then click selection -> cursor.
19. Press [g], [z], 1.5 then [enter] todo move the last vertex up.
20. Click RMB todo select the second-last vertex.
21. Press [g], [x], 1 then [enter] todo align the second-last vertex with the last.
22. Press [g], [z], 2 then [enter]to move the second-last vertex up.
23. Click RMB todo select the first vertex (right-most) and press [shift+s], then click cursor -> selection.
Rotating the scene should show a view similar to:
Properly positioned Path.
materials
1. Press [tab] todo estop editing the Path.
2. Press [f5] for the materiales buttons.
3. Click RMB on the Cube todo only select the Cube.
4. Under the enlaces and pipeline panel, under enlace todo object, rename material todo Cube.
5. Click RMB on the plane todo only select the plane.
6. Under the enlaces and pipeline panel, under enlace todo object, click add new.
7. Rename material todo plane.
récord Path ipo
1. Press [numpad7] for a top view.
2. Add » empty.
3. Click [shift]+RMB on the Path.
The Empty and Path should both be selected.
4. Press [Control + p] for the parenting menú.
5. Click follow Path todo parent the Empty todo the Path.
6. Copy and paste the following code into the text window.7. Change the name of the script todo copy-Path, py.#
# this script is used todo map the ipo curve of an object as it travels.
# th rouge frames of an animation. The algorithm follows:
#
# 1. Calculate number of steps neded todo animate at given framerate.
# 2. Get the selected object.
# 3. Duplicate the selected object (which Will hold the generated ipo).
# 4. Reset the duplicate object.
# 5. Loop over the number of frames at the calculated framerate.
# 6. Set the active frame.
# 7. Position the duplicate object todo the same place as the selected one.
# 8. Copy the selected object location and rotation into the duplicate ipo.
#
# usage:
# (1) select the object that follows a curve.
# (2) load this script in Blender text window.
# (3) move the mouse todo the text window.
# (4) press alt-p.
#
Import Blender.
From Blender import object, scene.
Framespersecond = scene. Getcurrent(), getrenderingcontext(), fps.
Firstframe = 0
Lastframe = 100
Stepsperframe = (lastframe - Firstframe) / framespersecond.
Selected = object. Getselected()[0]
Object. Duplicate ()
Duplicate = object. Getselected()[0]
Duplicate, clrparent()
Duplicate, clearipo ()
For frame in range (firstframe, lastframe, stepsperframe):
Blender. Set(curframe, frame)
Duplicate, setmatrix(selected, getmatrix())
Duplicate, insertipokey(object. Loc)
Duplicate, insertipokey(object. Rot).
8. Save the script as copy-Path, py.
9. Click RMB todo select the empty.
10. Move the mouse todo the text window.
11. Press [Alt+p] todo execute the script.
12.while executing, the script instructs Blender todo scrub th rouge frames at 25 frames per second (default). With 100 frames, only every fourth frame is used. Each frame that is used causes the duplicate object todo have its location and rotation recorded as an ipo key (i, a vertex in that object ipo curve).
13. Move the mouse todo the 3d window.
14. Press [del] then [enter] todo deleete empty.001.
15. Click RMB todo select the empty.
16. Press [Alt+p].
17. Click clear parent todo remove the Path as the Empty parent.
18. Press [shift+downarrow] todo reset the ipo todo frame 0.
If the Empty moves in unexpected ways, it is likely this step was missed.
19. In the ipo window, set the Empty todo use the newly created ipo data block, obipo.
Empty new ipo.
20. Click RMB todo select the Path.
21. Press [m], 2, then [enter] todo move the Path todo another layer.
22. Press [f7] for the object buttons.
23. Click RMB todo select the empty.
24. Under the animation settings panel, set timeoffset todo 180.
This is the number of frames the sphere takes todo hit the plane.
Game Engine actors
1. Press [f4] for the logic buttons.
2. Click RMB todo select the sphere.
3. Click actor.
4. Click dynamic.
5. Click Bound.
6. Set Radius todo 0.75.
7. Change box todo sphere (as the Bound type).
8. Click RMB todo select the plane.
9. Click actor.
10. Click Bound.
Game engine logic
When the sphere collides with the plane the following must happen:
The sphere makes the Empty its parent.
The Empty plays its ipo.
The sphere ipo keys are recorded for each frame.
logic blocks - Empty
1. Click RMB todo select the empty.
2. Click add property.
3. Change the name todo collision.
4. Change the type from its default value float todo bol.
This Will récord the sphere colliding with the plane. Upon collision, the sphere must follow the Empty (which has an ipo that was derived from the Path that was banished todo layer 2).
5. Under the sensors panel, click add (right next todo empty) todo add a sensor todo the empty.
6. Change the name of the sensor to, copy.
This sensor Will help continually copy the value of the sphere collision property.
7. To add another sensor, click add, below sensors.
8. Change the sensor type from always todo property.
9. Change the name of the sensor to, ipo.
10. Set the value of prop todo collision.
11. Set the value of value todo true.
12. Under the controllers panel, click add, todo add a controller.
13. Change the name of the controller todo c, copy.
14. To add another controller, click add, below controllers.
15. Change the name of the controller todo c, ipo.
16. Under the Actuators panel, click add, todo add an actuator.
17. Change the name of the actuador todo a, copy.
18. Change the actuador type from motion todo property.
19. Change the actuator property type from assing todo copy.
20. Set the value of prop todo collision.
21. Set the value of ob todo sphere.
22. Set the value of prop todo collision.
23. To add another actuator, click add, below Actuators.
24. Change the actuador type from motion todo ipo.
25. Change the name of the actuador todo a, ipo.
26. Set the value of end todo 100.
27. Link sensor copy todo controller copy todo actuator copy.
28. Link sensor ipo todo controller ipo todo actuator ipo.
The logic should look similar to:
Logic blocks for the empty.
The logic block instructs Blender todo continually copy the sphere collision property into the Empty property of the same name. Once the collision property is set todo true, the Empty is instructed todo replay its ipo. All that remains is todo set the sphere parent todo the empty, remove the parent relationship when appropriate, and récord the sphere complete ipo curve.
Logic blocks - Sphere - Sensors
1. Click RMB todo select the sphere in the 3d window, then click add property.
2. Change the name todo collision.
3. Change the type from its default value float todo bol.
4. Click add property.
5. Set the name todo time.
6. Change the type from float todo timer.
A timer Will be used todo remove the sphere parent (the empty). It is a los used todo calculate the current frame.
7. Under the sensors panel, click add, below sensors.
8. Change the name to, plane.
9. Change the sensor type todo collision.
10. Toggle m/p on.
11. Set the value of m/p todo plane.
12. Click add, below sensors.
13. Change the name to, time.
14. Change the sensor type todo property.
15. Toggle activate true level triggering on.
16. Change the type from equal todo interval.
17. Set the value of prop: todo time.
18. Set the value of min: todo 4.0.
19. Set the value of max: todo 4.1.
20. Click add, below sensors.
21. Change the name to, start.
22. Toggle activate true level triggering of.
23. Click add, below sensors.
24. Change the name to, récord.
25. Set the value of f: todo 2.
26. Toggle activate true level triggering on.
27. Click add, below sensors.
28. Change the name to, estop.
29. Change the sensor type todo collision.
30. Toggle m/p on.
31. Set the value of m/p todo Cube.
Logic blocks - Sphere - Controllers
1. Under the controllers panel, click add, below controllers, five times.
2. Change the name of the controllers, from top todo bottom as:
I, c, plane.
I, c, time.
I, c, start.
Iv, c, récord.
V, c, estop.
3. Change the default controller type from and todo Python for c, start and c, récord.
4. Create a new text file in the text window by clicking the arrows next todo the datablock name and clicking ad new.
5. Copy and paste the following code into the text window.6. Change the name of the script todo récord-IPO, py.#
# this script is used todo récord the ipo curve of an object as it is.
# assaulted by the game physics engine. It should be executed automátically.
# vía logic blocks. The algorithm follows:
#
# 1. Get the object being controlled by physics.
# 2. Create a new ipo for recording the motion of that object.
# 3. Store the ipo curves for use by the frame recorder.
#
Import Blender.
# get the name of the object being controlled by physics.
#
Gameobject = Gamelogic, getcurrentcontroller(), getowner()
Objectname = gameobject, getname ()[2:]
# create an ipo of type object named recorded ipo.
#
Blenderobject = Blender. Object. Get(objectname)
Ipo = Blender. Ipo. New(object, recorded ipo)
Blenderobject, setipo (ipo)
# get the position of gameobject as ipo curves.
#
Locx = IPO, addcurve (locx)
Locy = IPO, addcurve (locy)
Locz = IPO, addcurve (locz)
# kep a reference todo the curves in a global variable. The variable.
# (gamelogic, rec) is used by a script that récords the position of the.
# game object per frame.
#
Gamelogic, rec = [gameobject, locx, locy, locz].
7. Save the script as récord-IPO, py.
8. Create a new text file in the text window by clicking the arrows next todo the datablock name and clicking ad new, and then change the name todo récord-frame, py and save it as récord-frame, py.
9. Copy and paste the following code into the text window.10. Under the controllers panel, set the value of script: for controller c, start todo récord-IPO, py.#
# this script is used todo récord the position of an object as it travels.
# th rouge each frame, independent of the game physics. If Blender is set.
# todo récord game physics todo IPO, toggle that menú item of. This is a.
# replacement. The algorithm follows:
#
# 1. Get the object being controlled by physics.
# 2. Get the object position (for a given frame).
# 3. Update the recorded curves (estored in the global variable Gamelogic, rec).
#
Import Blender.
From Blender import scene.
Gameobject = Gamelogic, getcurrentcontroller(), getowner()
Position = gameobject, getposition()
Frame = gameobject, time * scene. Getcurrent(), getrenderingcontext(), fps.
# only try todo update the curves if the variable has ben initialized.
#
If hasattr(gamelogic, rec):
# set the curves for the x y, and z axis.
#
Gamelogic, rec[1].addbézier((frame, position[0]))
Gamelogic, rec[1].update ()
Gamelogic, rec[2].addbézier((frame, position[1]))
Gamelogic, rec[2].update ()
Gamelogic, rec[3].addbézier((frame, position[2]))
Gamelogic, rec[3].update ().
11. Likewise, set the value of script: for c, récord todo récord-frame, py.
12. Under the Actuators panel, click add, below Actuators, five times.
13. Change the type of Actuators, from top todo bottom as:
I. Property.
I. Parent.
I. Property.
Iv. Parent.
V. Game.
14. Change the name of the Actuators, from top todo bottom as:
I, a, start, parent.
I, a, parent, empty.
I, a, estop, parent.
Iv, a, parent, none.
V, a, estop.
15. From top todo bottom:
I. Set the prop value of a, start, parent todo collision.
I. Set value of a, start, parent todo true.
I. Set the ob value of a, parent, Empty todo empty.
Iv. Set the prop value of a, estop, parent todo collision.
V. Set value of a, estop, parent todo false.
Vi. Change set parent of a, parent, none todo remove parent.
Vi. Change start new game of a, estop todo quit this game.
16. Link sensors todo controllers todo Actuators:
Logic blocks for the sphere.
render animation
1. Move the mouse cursor over todo the 3d window.
2. Press [p] todo invoke the wrath of the game physics.
3. Press [Alt+a] todo play the animation using ipo curves.
The sphere should fall from Gravity todo the plane, follow the Path, then fall from Gravity todo the box.
4. Render » render animation.
David jarvis.
Url: http://www.davidjarvis, ca/.
www.blenderart.org.