Game Development Reference
In-Depth Information
Let's see what we have in our simulation:
• A world with a gravity
• A ball that should react to world forces (such as gravity)
• A floor that shouldn't react to world forces
• Some kind of materials, as we expect the ball to bounce on the floor
You are already able to configure a world with gravity, so we are going to start with
the creation of the ball.
1. It doesn't matter if we are creating a sphere or a polygon, the first step to
create a body is:
var bodyDef:b2BodyDef=new b2BodyDef();
b2BodyDef is the body definition, which will hold all data needed to create
our rigid body.
2.
Now it's time to place the body somewhere in the world. As we are working
on a 640 x 480 size, we'll place the ball in the top-center position of the stage,
let's say at (320,30) , shown as follows:
bodyDef.position.Set(10.66,1);
The position property obviously sets the body's position in the world, but
I am sure you are wondering why I told you that I was going to place the
body at (320,30) and instead placed it at (10.66,1) . It's a matter of units of
measurement. While Flash works with pixels , Box2D tries to simulate the real
world and works with meters . There isn't a general rule for converting pixels
to meters, but we can say that the following equation works well:
meters = pixels * 30
So, if we define a variable to help us with the conversion from meters to
pixels, we can use pixels rather than meters when dealing with the Box2D
World. This is more intuitive for all of us who are used to thinking in pixels
when making Flash games.
3. Open the class you have created in the first chapter and change it in the
following way:
package {
import flash.display.Sprite;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
 
Search WWH ::




Custom Search