Physical objects in the Box2D simulation environment contain many customizable properties. You can use this to adapt object behavior to suit your needs.
First of all, you'll need an object to set properties on. For all properties to be effective, you can use a dynamic object:
local body_def = box2d.BodyDef() body_def.type = 'dynamic' body_def.position = Vec(0,0) body_def.angle = 0 local body = world.createBody(body_def) local shape = box2d.CircleShape() shape.radius = 1 local fixture_def = box2d.FixtureDef() fixture_def.shape = box_shape fixture_def.density = 1 local fixture = body.createFixture(fixture_def)
Now that you've got everything prepared, you can set up physical object properties. Keep in mind that you should never change object properties during a simulation cycle as it will lead to simulation errors. You can determine this situation by querying the World
object world.locked
. The world object is locked when you're in the middle of the simulation process.
The body object offers parameters such as linear, angular damping, gravity scale, rotation locking, and many others.
Damping leads to the gradual slowing of object movement and it approximates the movement friction in the real world. Otherwise, the object would move indefinitely. While linear damping applies to linear motion, angular damping applies to rotational movement. You can access these parameters in a form of the body object properties:
body.linearDamping = 0 body.angularDamping = 0.1
Another parameter accessible on the body object is the gravity scale factor. This can be used to artificially decrease or increase the object's weight or make objects float:
-- normal gravity scale is equal to 1, floating objects can use 0 body.gravityScale = 1
When simulating the player's body, it's often desirable to lock object rotation. This is achievable by setting the fixedRotation
property to true
:
body.fixedRotation = true
When simulation takes place, Box2D determines what objects are currently moving or interacting with other objects. Such objects are awake. Other objects are in a resting (idle) state. These are turned into a sleeping state when they are not moving at all or there are no other objects touching them for a certain period of time. This period of time is defined in the Box2D source code, and you should not change it unless you know what you're doing. This feature can greatly increase the physical engine performance.
You can obtain the object awake state with the awake
property:
if not body.awake then -- do something end
The object sleeping can be manually disabled with the allowSleeping
property:
body.allowSleeping = true
Each body object contains information about its mass value in kilograms and inertia in the kg*m2 unit. Body mass affects the object's ability to move in a linear movement. On the other hand, rotational inertia determines the ability to rotate. For instance, figure skaters can decrease their rotational inertia by pulling their arms. This allows them to spin faster.
You can't change these properties directly from the body object. However, these properties are indirectly accessible from the massData
object that you can obtain with the massData
property. The following lines of code will show you how to access and modify these properties:
local object_mass = body.mass local object_inertia = body.inertia local mass_data = body.massData massData.center = Vec2(0, 0) massData.mass = 10.0 massData.inertia = 0.9
So far, you've seen the use of properties that are accessible directly from the body objects. For physical properties of the object material, you'll have to use the fixture
object. You can access the fixture
object from the body object or use the object reference obtained from the createFixture
function:
local fixture = body.fixture
The fixture
object contains properties such as friction, restitution, and density.
Friction determines how much an object will move when its surface is in contact with another object. Objects with friction 1 will glide until damping slows them down:
body.fixture.friction = 0.5
The restitution property affects the object elasticity on impact. For instance, you can use this property to simulate inflatable balls:
body.fixture.restitution = 0.6
Density is usually set in the BodyDef
object during the object's creation. It might be desirable to change this value as part of the object's heating simulation or size expansion:
body.fixture.density = 0.8
When you change the object's density, it's good to call resetMassData
as well. This will apply the density property to the object mass and rotational inertia:
body.resetMassData()
LuaBox2D contains interfaces for each Box2D object. This gives you the ability to experiment with various types of bodies and settings without much trouble as if you've used the Box2D C++ interface. The LuaBox2D library uses UpperCamelCase
for object constructors. All object properties use lowerCamelCase
.
For a more detailed description of all the available object properties, you can refer to the Box2D manual, which you can find online at http://box2d.org/manual.pdf.
3.133.128.145