Book Image

Lua Game Development Cookbook

By : Mario Kasuba, Mário Kašuba
Book Image

Lua Game Development Cookbook

By: Mario Kasuba, Mário Kašuba

Overview of this book

Table of Contents (16 chapters)
Lua Game Development Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Setting up object joints


Joints present a way of two or more connected objects with a certain type of constraint. The Box2D library offers several types of joints between objects. This type of object connection is useful when simulating arms, wheels, or more complex objects such as cars, robots, and so on.

Getting ready

You'll need at least two dynamic objects to use joints. This recipe will use two rectangular bodies that will be used with various types of joint connections. To make things easier, the following sample code offers a simple function that prepares a rectangular body that will be used many times in this recipe:

local makeBox = function(position, size, angle)
  local size = size * box2dScalingFactor
  local bodyDef = box2d.BodyDef()
  bodyDef.type = 'dynamic'
  bodyDef.position = position * box2dScalingFactor 
  bodyDef.angle = math.rad(a)
  
  local body = world.createBody(bodyDef)
  local box = box2d.PolygonShape()
  box.setAsBox(size.x, size.y)

  local fixtureDef = box2d.FixtureDef...