![]() For this simple example, we don't need much iteration. Likewise, using more iterations decreases performance but improves the quality of your simulation. Using fewer iterations increases performance but accuracy suffers. You can tune this number to your liking, just keep in mind that this has a trade-off between performance and accuracy. The suggested iteration count for Box2D is 8 for velocity and 3 for position. In addition, the position phase may exit iterations early if the errors are small. In the position phase the solver adjusts the positions of the bodies to reduce overlap and joint detachment. In the velocity phase the solver computes the impulses necessary for the bodies to move correctly. There are two phases in the constraint solver: a velocity phase and a position phase. To get a good solution, we need to iterate over all constraints a number of times. However, when we solve one constraint, we slightly disrupt other constraints. ![]() A single constraint can be solved perfectly. The constraint solver solves all the constraints in the simulation, one at a time. In addition to the integrator, Box2D also uses a larger bit of code called a constraint solver. Without further ado, here is the time step. So don't tie the time step to your frame rate (unless you really, really have to). A variable time step produces variable results, which makes it difficult to debug. We also don't like the time step to change much. You can get away with larger time steps, but you will have to be more careful about setting up the definitions for your world. Generally physics engines for games like a time step at least as fast as 60Hz or 1/60 seconds. So we need to pick a time step for Box2D. This goes along with the traditional game loop where we essentially have a flip book of movement on the screen. Integrators simulate the physics equations at discrete points of time. We just have a couple more issues to consider.īox2D uses a computational algorithm called an integrator. Now we are ready to set Newton loose to do his thing. So we have initialized the ground box and a dynamic box. By default bodies are static, so we should set the b2BodyType at construction time to make the body dynamic. The main difference, besides dimensions, is that we must establish the dynamic body's mass properties.įirst we create the body using CreateBody. We can use the same technique to create a dynamic body. If this is violated many things will break Many of the assumptions made in Box2D are based on the rigid body model. The reason is simple: a body with morphing shapes is not a rigid body, but Box2D is a rigid body engine. Moving or modifying a shape that is on a body is not supported. So we don't move a shape around on the body. A fixture does not have a transform independent of the body. A fixture's world transform is inherited from the parent body. So when the body moves, so does the shape. When you attach a shape to a body using a fixture, the shape's coordinates become local to the body. However, you can attach all static fixtures to a single static body. Note that every fixture must have a parent body, even fixtures that are static. It clones the data into a new b2Shape object. GroundBody-> CreateFixture(&groundBox, 0.0f) īox2D does not keep a reference to the shape. ![]() ![]() A static body has zero mass by definition, so the density is not used in this case. The second parameter is the shape density in kilograms per meter squared. Later we will see how to use a fixture definition for customized material properties. We do not have a need to alter the default fixture material properties, so we can pass the shape directly to the body without creating a fixture definition. We finish the ground body in step 4 by creating the shape fixture. Due to the limitations of floating point arithmetic, using Box2D to model the movement of glaciers or dust particles is not a good idea. For example, a barrel is about 1 meter tall. Box2D generally works best when objects are the size of typical real world objects. So you can consider the extents to be in meters. Box2D is tuned for meters, kilograms, and seconds. So in this case the ground box is 100 units wide (x-axis) and 20 units tall (y-axis). The SetAsBox function takes the half-**width** and half-**height** (extents). ![]()
0 Comments
Leave a Reply. |