📜 ⬆️ ⬇️

Physics on Flash. Box2D Engine


Fast, convenient and powerful open source physics engine. Under the cut - links, a small tutorial and an example of use.

Links


The Box2D project itself is located at this address - http://www.box2d.org/ , where you can read about its capabilities, chat on the forum, etc.
Since it was made AS3 port, homepage - http://box2dflash.sourceforge.net/ . It posted a clip showing the capabilities of this engine. By the way, all with Friday)


Use (most basic)


1. Create a "world", all calculations occur only within the limits of this world, an object that goes beyond its borders falls out of calculations. So we do it with a margin (WIDTH and HEIGHT - window sizes):
// world bounding box
var worldAABB : b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-WIDTH, -HEIGHT);
worldAABB.upperBound.Set(2*WIDTH, 2*HEIGHT);

// create world. zero gravity + use "sleeping" for objects
m_world = new b2World(worldAABB, b2Vec2.Make(0, 0), true );


* This source code was highlighted with Source Code Highlighter .

2. Add an object to the world.
The object is characterized by two components.
The first is the geometric representation of the object (its “surface”). Each object can consist of several shapes that are convex polygons or circles (but nothing prevents you from expanding the base shape and writing your own).
Here density, friction force, etc. are given.
// shape definition
var bodyShapeDef : b2PolygonDef = new b2PolygonDef();
// as rectangle
bodyShapeDef.SetAsBox(w/2, h/2);
// density
bodyShapeDef.density = 1.0;
// friction
bodyShapeDef.friction = 0.3;


* This source code was highlighted with Source Code Highlighter .

The second is the object itself, its position, mass, moment of inertia, etc.
Description of the object:
// object definition
var bodyDef : b2BodyDef = new b2BodyDef();
// world position
bodyDef.position.Set(xc, yc);
// linear "friction with world"
bodyDef.linearDamping = 0.3;
// angular "friction with world"
bodyDef.angularDamping = 0.3;


* This source code was highlighted with Source Code Highlighter .

Creating an object (it is automatically registered in the world). Assigning to him a previously created geometric representation, and (very conveniently) automatic calculation of all physical parameters based on its density and shape.
// create object from definition
var body : b2Body = m_world.CreateBody(bodyDef);
// create object shape from shape definition
body.CreateShape(bodyShapeDef);
// calculate mass, center of mass and inertia
// based on shape
body.SetMassFromShapes();


* This source code was highlighted with Source Code Highlighter .

There is a third (optional) component of the object - its presentation on the screen. At the initial stages of development, you can use the b2DebugDraw class, which shows all the objects, the connections between them, etc. But how to connect, for example, your own MovieClip and a physical object?
The most frequently used practice is to use the userData field of the created object.
Remember the on-screen representation in this field:
// save screen object in userData field
body.SetUserData(bodyDisplayObject);

* This source code was highlighted with Source Code Highlighter .

3. Emulation
Everything is simple. The world has a Step function, which takes a step time in milliseconds seconds and the number of iterations that must be done at this step. After emulation we go through all the objects of the world, we look if the userData field contains a screen representation — then we update the position / rotation of this representation in accordance with the fields of the physical object.
// calculate physics
// using 10 steps of physics for each frame
m_world.Step(dt, 10);

// loop for all physic objects
for ( var body : b2Body = m_world.GetBodyList(); body; body = body.GetNext())
{
// if userData is not a shape, then skip this object
if (!(body.GetUserData() is Shape))
continue ;

// update screen object to be equal physical object
body.GetUserData().x = body.GetPosition().x;
body.GetUserData().y = body.GetPosition().y;
body.GetUserData().rotation = body.GetAngle() * 180.0 / Math.PI;
}


* This source code was highlighted with Source Code Highlighter .

Example


Wrote a small application in an attempt to create a snake. 80 rectangles are associated with RevoluteJoint pairs, with a limit of rotation of + -20 degrees. When moving the mouse, the head segment of the snake receives an impulse in the direction of the cursor, as a result the whole snake crawls. There are also objects in the world to crawl around.
By clicking the application switches between own drawing and b2DebugDraw.
application - http://www.rexxar.ru/snake/
source code - http://www.rexxar.ru/snake/src.zip

Another test - shows the point of impact. On click, a new rectangle is created and falls to the ground.
application - http://www.rexxar.ru/phys/ .
')

Conclusion


Analysis of the engine and writing examples took just a few hours, a very simple, convenient, fast and powerful tool for emulating physics. I will use.

Source: https://habr.com/ru/post/76611/


All Articles