Moving objects

Dynamic objects can be moved primarily by using forces. Moving objects by setting their position manually is not recommended because you can easily miss an object collision this way. However, you can set an object's position at the start of your game to adjust the initial object location.

Getting ready

For this recipe, you'll need a dynamic object with a nonzero mass and density:

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 = shape
fixture_def.density = 1.5
fixture_def.friction = 0.3
fixture_def.restitution = 0.2

local fixture = body.createFixture(fixture_def)

How to do it…

In the real world, you have to use a force to move objects. There are two ways in which you can apply a force to move objects in Box2D—the continual force and impulses.

The continual force

The continual force affects object movement gradually for a longer time. Depending on the previous state of an object's movement, this can take some time for the object to achieve the desired speed. You can either choose to apply force to certain points of an object or use the object's center. If you choose other to apply a force on a point other than the object's center, it will generate a torque and the object will rotate:

local point_on_object = Vec2(0.5, 0.5)
local linear_force = Vec2(1, 0)
local rotational_moment = 20

body.applyForce(linear_force, point_on_object, true)
body.applyForceToCenter(linear_force, true)

You can use the applyTorque function to change the angular velocity. The base unit for the rotational moment is N*m:

body.applyTorque(rotational_moment, true)

The last parameter of this function determines whether to wake the body object after the force has been applied. This is usually set to true.

Impulses

Impulses result in a sudden change of movement direction or speed. An impulse, in general, is a force applied over a time interval. They are usually used with a very large force over a very short time span. When the time is short enough, it can be approximated as an instantaneous change of velocity. Similarly, as in a case with the continual force, you can apply an impulse to a certain point of the object:

body.applyLinearImpulse(linear_force, point_on_object, true)
body.applyAngularImpulse(rotational_moment, true)

Object velocity information

Sometimes you'll need to get the current object velocity. For instance, this information is useful in the motion blur effect for fast moving objects. There are two types of velocity you can use: linear and angular velocity. Linear velocity is used for translation movement and it's represented by the Vec2 vector.

Angular velocity tells you how fast the object rotates and uses units of radians per second:

local linear_velocity = body.linearVelocity
local angular_velocity = body.angularVelocity

You can use these properties to set the current object velocity as well but it results in unnatural movement. However, it can be useful when setting up an initial object velocity:

body.linearVelocity = Vec2(1, 0)
body.angularVelocity = 2

How it works…

Movement in Box2D is simulated in small discreet steps. The size of these steps varies with the time spent on frame rendering.

There are a few problems that a rise from discrete physics simulation. The most notable one is object tunneling when an object moves so fast that it literally teleports through the wall. Box2D eliminated this problem with continuous collision detection or CCD. Because CCD increases the processing time for each frame, it's turned off by default for all objects.

The maximum velocity of objects is 2 meters per time-step. This might seem small but take in to account that you usually use from 30 to 60 time-step frames per second, which gives you a maximum object speed from 60 to 120 meters per second or from 216 to 432 kms per hour. This limit exists to achieve better accuracy. To get greater speeds, you'll need to lower your physical world scale.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.189.251