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...