Game Development Reference
In-Depth Information
b2Vec2 is a 2D column vector that is a data type, which will store x and y
components of a vector. As you can see, the constructor has two arguments,
both numbers, representing the x and y components. This way we are
defining the gravity variable as a vector with x=0 (which means no
horizontal gravity) and y=-9.81 (which approximates Earth gravity).
Physics says the speed of an object falling freely near the Earth's surface
increases by about 9.81 meters per second squared, which might be thought
of as "meters per second, per second". So assuming there isn't any air
resistance, we are about to simulate a real-world environment. Explaining
the whole theory of a falling body is beyond the scope of this topic, but you
can get more information by searching for "equations for a falling body" on
Google or Wikipedia.
2.
You can set your game on the move with the following line:
var gravity:b2Vec2=new b2Vec2(0,1.63);
You can also simulate a no gravity environment with the arguments set at
(0,0) :
var gravity:b2Vec2=new b2Vec2(0,0);
Working with no gravity is also useful if you want to work in a
top-down environment.
3.
We also need to tell if bodies inside the world are allowed to sleep when they
come to rest, that is when they aren't affected by forces. A sleeping body does
not require simulation, it just rests in its position as its presence does not
affect anything in the world, allowing Box2D to ignore it, and thus speeding
up the processing time and letting us achieve a better performance. So I
always recommend to put bodies to sleep when possible.
Sleeping bodies won't sleep forever and they'll wake up as
soon as a collision occurs or a force is applied directly on them.
4. Add the following line, which is just a simple Boolean variable definition:
var sleep:Boolean=true;
5. And finally, we are ready to create our first world:
var world:b2World = new b2World(gravity,sleep);
6.
Now we have a container to manage all the bodies and perform our
dynamic simulation.
 
Search WWH ::




Custom Search