http://www.ladeng6666.com/blog/category/box2d/
What the hell is B2debugdraw?
We have learned a lot about box2d, but we have not studied the B2debugdraw class that we use every day. A few days ago, a netizen asked, today we will learn how to use it.
As is known to all, box2d is a 2D physics engine. The so-called engine, like the motor of a car, we also call it the engine, is the core of the entire braking system. But only the motor engine, the car is not moving, but also have wheels (nonsense). The wheels are not mounted directly on the motor, they are connected to the motor by the axle.
We know that when you create a box2d app, you must have a Sprite object. The relationship between this Sprite object and the B2debugdraw, box2d engine is like the relationship between wheels, axles, and cars. Without the axle, the motor and the wheel cannot be connected, and the car will not move. Similarly, without the B2debugdraw class, the box2d and the sprite can't even get up, we can't see the physical simulation process.
Take a look at the illustration below
You got it. Well, let's just say something more specific.
B2body is the core object of box2d, open the b2body.as source file (such as the following code), you can see that it is not a subclass of Displayobject, it means that we cannot add it to the stage through Addchild (). ActionScript
1 2 3 4 5 6 7 8 9 Ten each |
//Constructor /** * @private */ public function B2body (bd:b2bodydef, World:b2world) { m_flags = 0 ; if (bd. Bullet) & nbsp { m_flags |= e_bulletflag; // ............................. |
In fact, box2d only integrates various algorithms, physically simulates the B2body object, and stores the results of the calculation in this object. So theoretically, we can access these properties of b2body (such as x, Y, vertex, etc.) and then draw the simulation results with the AS3 drawing API. But this is very cumbersome, we have to traverse all the rigid bodies, joints, and their coordinates, angles, vertices and other information. Anyway, I don't want to do these moves. So box2d prepared for US B2debugdraw class, as long as the B2debugdrawsprite () method to bind a Sprite object, box2d help us in this sprite, with the drawing API (such as LineTo, Beginfill, etc.) Draws a simulated graphic.
In addition, B2debugdraw can also use the Setflag () method to selectively draw the contents of box2d objects. This can save CPU overhead. The Setflag () method has a 16-input parameter, which can only be a few constants defined in B2debugdraw: