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.
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
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.
A 2D piece of collision geometry that is rigidly attached to a body. Shapes have material properties of friction and restitution.
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.
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.
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.
A joint limit restricts the range of motion of a joint. For example, the human elbow only allows a certain range of angles.
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.
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.
You can download the Box2DFlashAS3 library from the url below. Most recent version now is 2.02.
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.
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.
4.Click “Settings…” button.
5.Click the “+” button and fill in the class path or browser to root directory of the library.
6.Click OK and return to the stage.
Next Part, i will doing a basic example on using the engine.