AS3 FAQ

From Box2D Wiki

Contents

Questions

Where is the documentation?

An AS3 version of the manual is available. It is recommended you skim this before posting a particular question. There's also an an API reference, though slightly out of date. Please use the manual and the TestBed, they should answer most queries.

Why is the API so strange?

Box2DAS3 is a port of the C++ version, and has kept the API largely identical. This means it does a lot of things differently to how you might expect. For example most arrays are of fixed size, with a variable elsewhere indicating how many valid items it contains.

How do I do graphics beyond the debug drawing?

How do I attach MovieClips to bodies?

Box2D is just a physics engine, and as such doesn't help with this. The fact it does any drawing at all is just meant to help people getting started. Basically, you need to manually create the movie clips, and manually copy the body's position and rotation information to the MovieClip each frame. One usually stores a reference to the movie clip in the bodies user data. An example of this is provided in the HelloWorld testbed, which should be enough information if you are already clear on the AS3 display routines. If you are not, please go elsewhere to read up on them, there are better forums and sources of help than here. This wiki page may also be of some help, as it describes the basic idea, and goes into some detail about how to improve on it. The "Actor" pattern should be the basis for any solid framework, imho.

How do I handle keyboard input?

As above, the generic answer to this is an AS3 question, not to do with Box2D. But there is an Input class in the TestBed that may help you.

How do I make stuff move?

Most often you want to control your world with b2Body.SetLinearVelocity, b2Body.ApplyForce, and b2Body.SetXForm. You might also want to use b2MouseJoint, or the motor features of other joints. See the later question on if you cannot get motion to work.

How do I make a platformer?

Well, lots of different ways. In fact, no one can agree on how best to do it. I suppose it depends what feel you want from a game. Try searching the forums for various suggestions.

GetLinearVelocityFromWorldPoint/GetLinearVelocityFromLocalPoint does not work.

This is a known bug in v2.0.1, but has already been fixed. Download v2.02 or the latest version from sourceforge. Or you can manually fix the problem, by applying the changes shown here and here (4 lines total).

What are these variables starting with "m_", and why am I getting problems with them?

As explained in the manual, these are private variables and are only intended for internal engine use. Changing them is not guaranteed to do what you think it does. In AS3 these variables were public, and quite often used, but now we have changed them to private, and are hoping to clamp down on usage. You will get compilation errors if you were previously using them, you should replace them with the corresponding Get/Set call which exists in most cases. For example:

body.m_userData = myActor;            -> body.SetUserData (myActor);
dbgDraw.m_drawScale = 20.0;           -> dbgDraw.SetDrawScale (20.0);
dbgDraw.m_fillAlpha = 0.3;            -> dbgDraw.SetFillAlpha (0.3);
dbgDraw.m_lineThickness = 1.0;        -> dbgDraw.SetLineThickness (1.0);
dbgDraw.m_drawFlags = b2DebugDraw...; -> dbgDraw.SetFlags (b2DebugDraw...);

Note, there is a work around for continuing to use them (import and use b2internal namespace), but I do not recommend it.

How can I tell if two bodies are touching easily?

Sorry, you can't. We are aware of this deficiency, and are thinking of how best to correct it (slowly). For now, you are recommended to keep track of information like this using the contact listeners. Or there is a hack involving m_contactList (see the previous question), which works, but with several caveats.

How can I stop two bodies colliding?

Firstly make sure you've read up on Filtering and Sensors in the manual so you know what is possible. But at the moment, it is not possible to filter two bodies based on the details of their collision. Again, there is a hack.

Why aren't my bodies moving?

Here are some of the more common pitfalls:

  • Are you actually making them move in some way, e.g. gravity or ApplyForce?
  • Is the body dynamic (i.e. non-zero mass)? You must call SetMassFromShapes or SetMass.
  • Is it asleep? In some circumstances, you may need call WakeUp, see the manual for details.
  • Is it frozen? Bodies that touch the edge of the world but are not deleted are frozen, and do not move. Make *your world large, and ensure stuff doesn't get near the edge.
  • Is the graphics actually moving stuff? Try using DebugDraw as a double check.
  • Is your sense of scale correct? Ensure you understand the difference between screen pixels and Box2D units. *By default there is a factor of 30. Also try to make shapes of size between 0.1 and 100 units, this is the rough range that Box2D has been tuned for.
  • Is your other sense of scale correct? Are you applying enough force to the mass of your object to move it?
  • Are you using mxmlc? See here
TOOLBOX
LANGUAGES