
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.
#Gideros box2d keep body upright how to#
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). So if you are developing only for one specific resolution, you can use Device dimensions, otherwise it is suggested to use Logical dimensions with the scale mode you find suitable.The SetAsBox function takes the half-**width** and half-**height** (extents). They basically will be scaled based on the scale mode you choose. No matter what resolution you have, the logical dimensions will always be the same. Meaning on an iPhone 3GS it will return width as 320īut logical dimensions are exactly what you set in your project properties. You should use W = application:getContentWidth(), H = application:getContentHeight() which would return logical dimensions that are used when in scaling mode.īasically it all brings down to these points:ġ) Choose the scaling mode that is proper for your game (Letterbox being most popular)Ģ) Choose logical dimensions for your game and create all the graphics for logical dimensions you set in the project properties (recommended 480x800 or 640x960)ģ) Create backgrounds a little more bigger than logical dimensions to cover whitespaces on devices with different ratiosĤ) Use absolute positioning ( ) for objects that need to stick to sides of the screen as on screen buttons for exampleĥ) (Optionally) prepare bigger graphics in in some fixed ratio coefficient and use Automatic Image Resoltuion feature to automatically load them for bigger devicesĪddition: (Difference between device and logical dimensions)ĭevice dimensions is exactly what device has.
