Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Lua Game Development Cookbook

You're reading from   Lua Game Development Cookbook Over 70 recipes that will help you master the elements and best practices required to build a modern game engine using Lua

Arrow left icon
Product type Paperback
Published in Jul 2015
Publisher
ISBN-13 9781849515504
Length 360 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mário Kašuba Mário Kašuba
Author Profile Icon Mário Kašuba
Mário Kašuba
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Basics of the Game Engine 2. Events FREE CHAPTER 3. Graphics – Common Methods 4. Graphics – Legacy Method with OpenGL 1.x–2.1 5. Graphics – Modern Method with OpenGL 3.0+ 6. The User Interface 7. Physics and Game Mechanics 8. Artificial Intelligence 9. Sounds and Networking Index

Moving objects


Dynamic objects can be moved primarily by using forces. Moving objects by setting their position manually is not recommended because you can easily miss an object collision this way. However, you can set an object's position at the start of your game to adjust the initial object location.

Getting ready

For this recipe, you'll need a dynamic object with a nonzero mass and density:

local body_def = box2d.BodyDef()
body_def.type = 'dynamic'
body_def.position = Vec(0,0)
body_def.angle = 0

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

local fixture_def = box2d.FixtureDef()
fixture_def.shape = shape
fixture_def.density = 1.5
fixture_def.friction = 0.3
fixture_def.restitution = 0.2

local fixture = body.createFixture(fixture_def)

How to do it…

In the real world, you have to use a force to move objects. There are two ways in which you can apply a force to move objects in Box2D—the continual force and impulses.

The continual force

The continual...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime