Game Development Reference
In-Depth Information
Physics
Gideros Studio offers a wrapper on Box2D for using physics. To use physics in Gideros Studio, you
use require "box2d" , and that creates a local variable called b2 that is of type Box2D , and offers all of
the physics-related functions.
The next thing that you need to do is create a physics world in which the physics objects will reside.
The following code shows an example.
Note In Gideros Studio, physics objects need to be updated to allow the dynamic physics bodies to be
updated; typically this is done using an enter_frame event.
local fixture = {shape = shape, density = 1, friction = 0.3}
local x, y = 100, 10
bodyD = {type = b2.DYNAMIC_BODY, position={x=x, y=y}}
body = world:createBody(bodyD)
body:createFixture(fixture)
function onEnterFrame()
world:step(1/60, 8, 3)
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
When you run the program, notice that nothing happens. This is because we need to place an object
on the screen that is updated as the physics body moves.
So, we can add an image that shall represent this physics body and also update it on the screen.
local img = Bitmap.new(Texture.new("image2.png"))
img:setAnchorPoint(0.5,0.5)
stage:addChild(img)
Then we redefine the onEnterFrame function as follows:
function onEnterFrame()
world:step(1/60, 8, 3)
img:setPosition(body:getPosition())
end
 
Search WWH ::




Custom Search