Talking about Actionscript 3, Box2D and Flash.
After understanding the meaning of the first lines in Box2D: tutorial for the absolute beginners, it’s time to explain the rest of the script.
Lines 19-27: These lines handle the debug draw. Remember Box2D does not draw anything, it just calculates position, rotation and motion of every object in the world. So it’s up to you to attach real graphic assets to worl objects. Anyway, with some lines you can enable a debug draw mode that will help you during the creation of your script. For more information about debug draw read Understanding Box2D debug draw.
Line 28 declaring final_body
, b2Body
type. b2Body
is the object used to represent one rigid body. A rigid body is a chunk of matter that is so strong that the distance between any two bits of matter on the chunk is completely constant. They are hard like a diamond. Box2D only handles rigid bodies.
Line 29 and 31 declare and create the_body
variable, b2BodyDef
type. b2BodyDef
handles the body definition. With the body definition we can specify the initial position of the body.
Line 30 and 33 declare and create the_box
variable, b2PolygonDef
type. b2PolygonDef
is a polygon definition.
Line 32: setting the initial position of the body. Remember values are in meters and refer to the center of the body.
Line 34: SetAsBox function takes the_box
and turns it into a box, passing the half-width and half-height as parameters. Read carefully… half-width and half-height. In meters.
Line 35: defining the friction of the box
Line 36: defining the density of the box. Setting the density of a body to zero
, like in this case, will make the body static. A static body is not affected by gravity, collisions and so on. It’s just fixed in the stage. This will represent the ground.
Line 37: CreateBody
creates the body previously defined
Line 38: I am creating the polygon shape previously defined with CreateShape
on the body previously created with CreateBody
Line 39: Once the shape is attached, we instruct the body to compute its mass properties from the attached shapes using the method SetMassFromShapes
. This is when the objects really exists.
Lines 40-42: Adding some listeners
Lines 45-58: This function, to be executed at every second, creates a box in the same way I created the ground, with the only exception at line 54 I am setting a density different than zero
, meaning the body is not a static one. Every body with a density will react to collisions, gravity, forces and so on.
Lines 60: We must update the world at every frame. We can do it with Step
function. Step
has two parameters: the first is the time interval, in seconds. It means at every frame I am going to update the world as if 1/30s
passed. The second is the constraint solver. The constraint solver solves all the constraints in the simulation, one at a time. A single constraint can be solved perfectly. However, when we solve one constraint, we slightly disrupt other constraints. To get a good solution, we need to iterate over all constraints a number of times. The suggested iteration count for Box2D is 10.
And now the entire script has been explained.
If you manage to turn this script into something interesting, send me at once and I’ll publish it on the blog.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.