

You may also specify the body's angle in radians, which is not affected by the position of the center of mass. Therefore you specify the position of the body's origin. When you are building the body definition, you may not know where the center of mass is located. For example b2Body stores the linear velocity for the center of mass. Much of Box2D's internal computations use the center of mass position. The center of mass is determined from mass distribution of the attached shapes or is explicitly set with b2MassData. The second point of interest is the center of mass. Fixtures and joints are attached relative to the body's origin. If you create several bodies at the origin, then performance will suffer.Ī body has two main points of interest. This has far better performance than creating the body at the world origin and then moving the body.Ĭaution: Do not create a body at the origin and then move it.

The body definition gives you the chance to initialize the position of the body on creation. You should establish the body type at creation because changing the body type later is expensive. Let's go over some of the key members of the body definition.Īs discussed at the beginning of this chapter, there are three different body types: static, kinematic, and dynamic. This means you can recycle a body definition to create multiple bodies. The body definition holds the data needed to create and initialize a body.īox2D copies the data out of the body definition it does not keep a pointer to the body definition. You should also keep body pointers so you can destroy them when you are done with them.īefore a body is created you must create a body definition ( b2BodyDef).
#Gideros box2d set velocity of object update#
This way you can query the body positions to update the positions of your graphical entities. You usually keep pointers to all the bodies you create. However, you can override the mass properties after a body is constructed. Normally, bodies acquire their mass properties from the fixtures. That means that two fixtures attached to the same rigid body never move relative to each other and fixtures attached to the same body don't collide.įixtures have collision geometry and density. Bodies carry fixtures and move them around in the world. If you try to set the mass of a dynamic body to zero, it will automatically acquire a mass of one kilogram and it won't rotate.īodies are the backbone for fixtures (shapes). A dynamic body always has finite, non-zero mass. A dynamic body can collide with all body types. They can be moved manually by the user, but normally they move according to forces. Kinematic bodies do not collide with other kinematic or static bodies.Ī dynamic body is fully simulated. A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. Kinematic bodies do not respond to forces.

Static bodies do not collide with other static or kinematic bodies.Ī kinematic body moves under simulation according to its velocity. Static bodies can be moved manually by the user. Internally, Box2D stores zero for the mass and the inverse mass. Here are the body type definitions:Ī static body does not move under simulation and behaves as if it has infinite mass. Bodies can be static, kinematic, or dynamic. You can apply forces, torques, and impulses to bodies. The dynamics module is covered in the following chapters.īodies have position and velocity. Therefore, you may want to quickly skim this chapter before reading it closely.

In the following, you may see some references to classes that have not been described yet. There are many dependencies between these classes so it is difficult to describe one class without referring to another. The Dynamics module sits on top of the Common and Collision modules, so you should be somewhat familiar with those by now. The Dynamics module is the most complex part of Box2D and is the part you likely interact with the most.
