Choosing the correct vector scaling for the physics engine

The Box2D library uses floating numbers to represent numerical values. The problem with these kind of numbers is that they offer limited precision. This limitation often results in various artefacts such as players walking right above the ground or boxes that can't remain in one place. Box2D uses the MKS (meters, kilograms, and seconds) system to represent the basic units. You should never use a pixel as a base unit. Therefore, you are expected to use the scaling factor to convert Box2D coordinates to pixels on the screen.

Getting ready

First, you should decide what scaling factor you will use. If you decide that 100 pixels will represent 1 meter, the scaling factor will be 100. You can define this factor as a global constant in the Lua language:

box2dScalingFactor = 100

How to do it…

When converting pixel-sized objects or vectors to the Box2D system, the scaling factor will be used to scale down by dividing all those values. The following pseudo code shows how to use scaling when you want to create a new Box2D object with pixel units:

-- definition of crate box dimensions
local crate_box = {
  width = 50,
  height = 50,
}
-- create a crate box physical object in Box2D system
local box2d_crate = box2d_create_box(
  crate_box.width / box2dScalingFactor,
  crate_box.height / box2dScalingFactor
)

The other situation is when you're rendering physical objects on the screen. You'll need to use the scaling factor to multiply the object's dimensions and its world position. The following pseudo code shows how to scale up physical objects from the Box2D system to match pixels and render it on the screen:

local box_position = box2d_crate.body.position * box2dScalingFactor

draw_box(box_position.x, box_position.y, crate_box.width, crate_box.height)

Notice that the box2dScalingFactor value was used on a Vec2 vector object that represents the physical object's position in a world. This way, you use the Vec2 operators to convert a position based on the MKS unit system to pixels. You already know the crate box size in pixels, so you can use that in the draw_box function that draws a nice box in your game.

How it works…

The correct use of the floating point numbers comes from the fact that floating numbers offer a limited precision. The Box2D library uses a 32-bit floating number mainly due to performance reasons. However, you can't express every real number in a floating point variable. Therefore, a floating point math uses rounding to get an approximate value.

The creator of Box2D knows a great deal about this issue and empirically set the usable range of floating point numbers that can be used to simulate the physical environment and object interactions within certain limits. This is true not only for object coordinates but for movement vectors as well.

This is why you should appropriately scale your game world to match this range. Your objects should be within the range of 0.1 and 10 meters in size. This will guarantee a good level of simulation quality.

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

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