Archive

Archive for the ‘Box2D’ Category

Setting Up The Box2D B2World Object

August 26th, 2009 Rslegion No comments

When coding Box2D the first thing that needs to be setup is the Box2D world, known as the b2World.

Inside the b2World object, we set the attributes for:

  • Size – How large we want our b2World to be.
  • Gravity – How strong we want our gravity to be.
  • Sleeping Object – Used to speed our app.

Here is an example of how to set up a b2World:

public function setupWorld ():void
{
// Setup our universe size
var universeSize:b2AABB = new b2AABB();
universeSize.lowerBound.Set (-3000 / RATIO ,-3000 / RATIO);
universeSize.upperBound.Set (3000 / RATIO ,3000 / RATIO);

// Setup our gravity
var gravity:b2Vec2 = new b2Vec2(0 , 9.8);

// Setup sleeping
var ignoreSleeping:Boolean = true;

// Create our world
myWorld = new b2World(universeSize,gravity,ignoreSleeping)
}

Firstly we set our attributes for the size of our world, the universe size is set using a class called the b2AABB. It is basically a rectangle and the parameters UpperBound and LowerBound must be Set. In this example we our applying physics between -3000 px and 3000 px.

After that we must set up our gravity. The gravity is of a b2Vec2 class. The parameters required are X and Y. In this example, as all box2D lengths and in meters,the gravity has been set to 0 meters per second right and 9.8 meters per second down.

Next we have to create a variable to tell box2D whether to ignore sleeping objects. This is mainly just for performance. When an object is not moving, it is classed as sleeping. When an object is sleeping box2D can ignore it for performance increase. Usually, you’ll want this on.

Lastly we must actually create our world. This is of a b2World class and has the parameters:

  • world:b2AABB
  • gravity:b2Vec2
  • doSleep:Boolean

That sums up our tutorial on setting up the Box2D B2World object, in our next tutorial in this series we will look at how to create static objects in box2D

Categories: Actionscript 3, Box2D Tags:

An Introduction To The Box2D Physics Engine

August 22nd, 2009 Rslegion No comments

This is the first of a range of tutorials covering use of the open source Box2D physics engine. This engine the physics engine of choice of AS3 and has more than likely been used to create some of you’re favorite games like ‘Fantastic Contraption’ and ‘Super Stacker’

I know what you’re thinking,

How am I going to learn how to code all these gravity and physics things?

The great part is, all the actual physics of the game have already been coded in the Box2D classes already. All we have to do is create the object, know as bodies in the Box2D world, and set there attributes!

In this tutorial i will cover the basics of Box2D and how it works.

The Difference

If you’re already an competent AS3 Coder, which you should be if you’re learning box2D, there are a few fundamental differences to the way things work.

In normal AS3 coding you have you’re sprites and MovieClips, they:

  • Are Smart
  • Calculate where to move by themselves
  • Have an Event.Enter_Frame attached to them

However sprites in Box2D are completey different, they:

  • Aren’t so smart
  • Go where box2D tells them to go

That’s the main difference when coding Box2D, its mainly because the engine is ported directly from the world of C++

In the next tutorial, i will cover how to set up Box2D so it knows where abouts on our flash movies that physics should be applied. Also know as the B2World.