require "box2d"
application:setScaleMode("letterbox")
application:setOrientation(application.LANDSCAPE_LEFT)
application:setLogicalDimensions(600,600)
local cw=application:getContentWidth()
local ch=application:getContentHeight()
-- this table holds the dynamic bodies and their sprites
local actors = {}
-- create world
local world = b2.World.new(0, 9.8)
-- create ground body
local ground = world:createBody({})
-- create an edge shape, and attach it to the ground body as a fixture
local shapeb = b2.EdgeShape.new(0, ch, cw, ch)
local shapet = b2.EdgeShape.new(0, 0, cw, 0)
local shapel = b2.EdgeShape.new(0, 0, 0, ch)
local shaper = b2.EdgeShape.new(cw, 0, cw, ch)
ground:createFixture({shape = shapeb, density = 0})
ground:createFixture({shape = shaper, density = 0})
ground:createFixture({shape = shapel, density = 0})
ground:createFixture({shape = shapet, density = 0})
local shape2 = b2.PolygonShape.new()
shape2:setAsBox(400, 10)
local ps1= world:createParticleSystem({ radius=4})
ps1:setTexture(Texture.new("ball.png"))
stage:addChild(ps1)
ps1:createParticleGroup({shape=shape2,
position={x=300,y=590},
color = 0xFF0000,
alpha=1,
flags=16,
density = 0
})
local actors = {}
local boxshape = b2.PolygonShape.new()
boxshape:setAsBox(10, 10)
local fixtureDef = {shape = boxshape, density = 2, restitution = 0.7}
function mouseDown(event)
local bodyDef = {type = b2.DYNAMIC_BODY, position = {x = event.x, y = event.y}}
local body = world:createBody(bodyDef)
body:createFixture(fixtureDef)
local bitmap = Bitmap.new(Texture.new("crate.png", true))
bitmap:setScale(0.3)
bitmap:setAnchorPoint(0.5, 0.5)
stage:addChild(bitmap)
actors[body] = bitmap
end
-- step the world and then update the position and rotation of sprites
local function onEnterFrame()
world:step(1/60, 8, 3)
for body,sprite in pairs(actors) do
sprite:setPosition(body:getPosition())
sprite:setRotation(body:getAngle() * 180 / math.pi)
end
end
local text = TextField.new(nil, "Click to drop crates")
text:setPosition(50, 250)
text:setScale(4)
stage:addChild(text)
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
stage:addEventListener(Event.MOUSE_DOWN, mouseDown)