You are Here:Home>>Old Posts>>Using Box2DFlashAS3 (AS3 physics engine) Part 1

Using Box2DFlashAS3 (AS3 physics engine) Part 1

Carlos Pinho
By | 2009-05-05T05:37:56+00:00 May 5, 2009|Old Posts|

I recently work on a project using Box2DFlashAS3 library and find it is a great thing we must learn. So, i want to write some beginner tutorials which help others to learn it. I will divided the tutorial in  several parts. In part 1, you will learn some basic concepts and preparation needed before using the engine.

A) Why Physics Engine?

You can do predefined animation in flash by time line or key frame. But this is time consuming. You need a lot of time to do the animation frame by frame. For example, have a scene with a ball dropping on the floor. This may request few hours of time to tweak the position and motion to make it realistic. So, you need a physics engine. A physics engine can simulate realistic physics movement of a object base on mathematical equation.

start
Evermore, it can provide real time interaction between user and the object. Most likely, you can find physics engine use in games.

B) Box2DFlashAS3 physics engine

Box2D is a feature rich 2d rigid body physics engine, written in C++ by Erin Catto. It has been used in many games, including Crayon Physics Deluxe, winner of the 2008 Independent Game Festival Grand Prize. Many ports are available for Flash, Java, C#, Python, JavaScript, D, and fixed point embedded devices.

Box2DFlashAS3 is an open source port of Erin Catto’s powerful c++ physics library Box2D. It provide most of the features included in the original Box2D library.

C) Some concepts must know…

Box2D works with several fundamental objects. We briefly define these objects here and more details are given later in this document.

rigid body (body)

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. In the following discussion we use body interchangably with rigid body.

shape

A 2D piece of collision geometry that is rigidly attached to a body. Shapes have material properties of friction and restitution.

constraint

A constraint is a physical connection that removes degrees of freedom from bodies. In 2D a body has 3 degrees of freedom. If we take a body and pin it to the wall (like a pendulum) we have constrained the body to the wall. At this point the body can only rotate about the pin, so the constraint has removed 2 degrees of freedom.

contact constraint

A special constraint designed to prevent penetration of rigid bodies and to simulate friction and restitution. You will never create a contact constraint, they are created automatically by Box2D.

joint

This is a contraint used to hold two or more bodies together. Box2D supports these joint types: revolute, prismatic, distance, and more. Joints may support limits and motors.

joint limit

A joint limit restricts the range of motion of a joint. For example, the human elbow only allows a certain range of angles.

joint motor

A joint motor drives the motion of the connected bodies according to the joint’s degrees of freedom. For example, you can use a motor to drive the rotation of an elbow.

world

A physics world is a collection of bodies, shapes, and constraints that interact together. Box2D supports the creation of multiple worlds, but this is usually not necessary or desirable.

D) Prepare to use the engine.

Download Box2DFlashAS3.
You can download the Box2DFlashAS3 library from the url below. Most recent version now is 2.02.

http://box2dflash.sourceforge.net/

You will got a zip file with the library source and some examples. Basically, you only need the source folder called “Box2D”. You can extract the files to your working directory and try the demo of the engine by double click the PhysTest.swf.

basic_setip_4

basic_setip_3

Setup class path

If you extract the file in your working, this step is not nessary. But if you place the library elsewhere other than your wokring directory. You can follow the step below to setup class path.

1.Open your working fla.

2.Open publish settings ( Menu>File>Publishing Settings).

3.Selct the Flash section.

basic_setip_1

4.Click “Settings…” button.

5.Click the “+” button and fill in the class path or browser to root directory of the library.

basic_setip_2

6.Click OK and return to the stage.

Next Part, i will doing a basic example on using the engine.

About the Author:

Carlos Pinho
A father, a husband and a geek... Carlos was the founder of projects like The Tech Labs and Flash Enabled Blog. He is the founder of TekTuts He is passionate about technologies. Their main skills are in analytics, transport & logistics, business administration. He also writes about programming resources, trends, strategy and web development.