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 bullets


Objects usually move slow enough to stop before passing through walls. However, some objects might move so fast that they teleport through obstacles. This is especially true for bullet type objects. You can eliminate this problem by letting Box2D know that these objects should be treated as bullets. Some games use a ray-casting technique where you basically determine the point of the bullet's impact on a wall or another object. This usually assumes a bullet trajectory in a straight line, which is not very accurate when compared to the real world.

Getting ready

For this recipe, you'll need to have one dynamic object, bullet, and one static object, wall to build a sample scene. For this purpose, you can use the following code:

local function createBullet(position, radius)
  local body_def = box2d.BodyDef()
  body_def.type = 'dynamic'
  body_def.position = position * box2dScalingFactor
  body_def.angle = 0

  local body = world.createBody(body_def)
  local shape = box2d.CircleShape...