Using vector math

The Box2D library uses two-dimensional vectors in a form of its own native data type called b2Vec2. It's used in many places of the Box2D library so it's better to stick with this one.

This data type can be used to specify the position in a 2D space, directional vector with unit size or a speed vector. Keep in mind that Box2D doesn't know the difference between the uses of this vector.

Getting ready

This recipe expects the user to have a basic knowledge of vector math.

The LuaBox2D library contains the b2Vec2 object interface called Vec2. You can create one simply by calling its constructor function. Almost all object constructors are available via the LuaBox2D interface:

local box2d = require 'LuaBox2D'
local Vec2 = box2d.Vec2

This will define a shortcut for the Vec2 object constructor.

How to do it…

You can create a new Vec2 vector object by calling the constructor in one of the following ways:

-- create zero vector
local vector1 = Vec2()
-- create a vector with x=1 and y=2
local vector2 = Vec2(1, 2)

The first case uses the Vec2 object constructor without a parameter that will create a vector with both coordinates set to zero. This is also known as a zero vector. The second case uses the constructor parameters to set initial coordinate values.

Each Vec2 vector object contains two coordinates: x and y. You can access and modify them directly as shown in the following sample code:

local vector = Vec2(1, 2)
vector.x = 10.5
vector.y = 13.25 + vector.x

The Vec2 object provides the following set of mathematical operations: addition, subtraction, negation, scalar multiplication, dot product, and cross product. All operations are immutable, which means that each mathematical operation on vectors results in a new Vec2 object. Unused vector objects are automatically cleaned with a garbage collection mechanism. This behavior is a part of the Lua language design.

The following table will show the usage of basic mathematical operations on the Vec2 object where v1 and v2 variables present valid Vec2 objects:

Operation

Lua notation

The addition of two Vec2 vectors

v1 + v2

The addition of the Vec2 vector with a scalar number

v1 + 5.5

The subtraction of two Vec2 vectors

v1 - v2

The subtraction of the Vec2 vector with a scalar number

v1 - 5.5

The Vec2 vector negation

-v1

The scalar multiplication of the Vec2 vector

v1 * 5.5

The dot product of two Vec2 vectors

v1 * v2

The cross product of two Vec2 vectors

v1 % v2

There are other operations available on vectors such as: vector equality comparison, the minimum and maximum of two vectors, vector normalization, vector length determination, the distance between two positional vectors, and the clamping of a vector.

Operation

Lua notation

Compare two vectors

(v1 == v2)

Get a minimum of two vectors

v1.min(v2)

Get a maximum of two vectors

v1.max(v2)

Get the vector length

v1.length

or

#v1

Obtain a distance between two positional vectors

v1.distanceFrom(v2)

Clamp a vector

v1.clamp(Vec2(0,0), Vec2(1,1))

How it works…

The Vec2 object presents an interface between the Lua and Box2D world for the b2Vec2 object. Its purpose is to make using 2D vectors easier.

Keep in mind that the Vec2 object adds a small overhead because it must create the b2Vec2 object before use, and with each call of the mathematical operator, it has to do an input validation.

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

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