require("mobdebug").start() require "box2d" --swivil application:setOrientation(Application.LANDSCAPE_LEFT) world = b2.World.new(0,10,true) -- function to create bounding walls as a shape function wall(x,y,width,height,name) local wall = Shape.new() wall:beginPath() -- we use (0,0) as center of shape and therefor use half of width and height wall:moveTo(-width/2,-height/2) wall:lineTo(width/2,-height/2) wall:lineTo(width/2,height/2) wall:lineTo(-width/2,height/2) wall:closePath() wall:endPath() wall:setPosition(x,y) -- create box2d physical object local body = world:createBody{type=b2.STATIC_BODY} body:setPosition(wall:getX(),wall:getY()) body:setAngle(wall:getRotation() * math.pi/180) local poly = b2.PolygonShape.new() poly:setAsBox(wall:getWidth()/2,wall:getHeight()/2) local fixture = body:createFixture{shape = poly, density = 1.0,friction = 0.1, restitution = 0.2} wall.body = body wall.body.type = "wall" wall.body.name = name -- add to scene stage:addChild(wall) -- return created object return wall end --Create floor as collision object function impact(x,y,width,height) local impact = Shape.new end -- create our grenade object using an image and make it abide by law of physics function grenade(x,y) local grenade = Bitmap.new(Texture.new("ball.png")) -- reference the center of img for positioning grenade:setAnchorPoint(0.5,0.5) grenade:setPosition(x,y) -- get radius local radius = grenade:getWidth()/2 -- create box2d physical object local body = world:createBody{type = b2.DYNAMIC_BODY} body:setPosition(grenade:getX(),grenade:getY()) body:setAngle(grenade:getRotation() * math.pi/180) local circle = b2.CircleShape.new(0, 0, radius) local fixture = body:createFixture{shape = circle, density = 1.0, friction = 0.1, restitution = 0.2} grenade.body = body grenade.body.type = "grenade" -- add to scene stage:addChild(grenade) -- return created object return grenade end --function explosion(x,y) --get images local explode1 = Bitmap.new(Texture.new("explode1.png")) local explode2 = Bitmap.new(Texture.new("explode2.png")) local explode3 = Bitmap.new(Texture.new("explode3.png")) local explode4 = Bitmap.new(Texture.new("explode4.png")) local explode5 = Bitmap.new(Texture.new("explode5.png")) local explode6 = Bitmap.new(Texture.new("explode6.png")) local explode7 = Bitmap.new(Texture.new("explode7.png")) local explode8 = Bitmap.new(Texture.new("explode8.png")) local explode9 = Bitmap.new(Texture.new("explode9.png")) local explode10 = Bitmap.new(Texture.new("explode10.png")) local explode11 = Bitmap.new(Texture.new("explode11.png")) local explode12 = Bitmap.new(Texture.new("explode12.png")) local explode13 = Bitmap.new(Texture.new("explode13.png")) local explode14 = Bitmap.new(Texture.new("explode14.png")) --set anchor points explode1:setAnchorPoint(0.5,0.5) explode2:setAnchorPoint(0.5,0.5) explode3:setAnchorPoint(0.5,0.5) explode4:setAnchorPoint(0.5,0.5) explode5:setAnchorPoint(0.5,0.5) explode6:setAnchorPoint(0.5,0.5) explode7:setAnchorPoint(0.5,0.5) explode8:setAnchorPoint(0.5,0.5) explode9:setAnchorPoint(0.5,0.5) explode10:setAnchorPoint(0.5,0.5) explode11:setAnchorPoint(0.5,0.5) explode12:setAnchorPoint(0.5,0.5) explode13:setAnchorPoint(0.5,0.5) explode14:setAnchorPoint(0.5,0.5) --create movieclip local explode = MovieClip.new{ {1,10,explode1},{11,20,explode2},{21,30,explode3},{31,40,explode4}, {41,50,explode5},{51,60,explode6},{61,70,explode7},{71,80,explode8}, {81,90,explode9},{91,100,explode10},{101,110,explode11},{111,120,explode12}, {121,130,explode13},{131,140,explode14} } --set position --explode:setPosition(x,y) --explode:gotoAndStop(1) --goto first image and play --explode:gotoAndPlay(1) --create box2d physical object local body = world:createBody{type = b2.STATIC_BODY} body:setPosition(explode:getX(), explode:getY()) body:setAngle(explode:getRotation() * math.pi/180) local poly = b2.PolygonShape.new() poly:setAsBox(explode:getWidth()/2,explode:getHeight()/2) local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.2} explode.body = body explode.body.type = "explode" --add to scene stage:addChild(explode) --return created object -- return explode --end -- Define begin collision event handler function function onBeginContact(event) --getting contact bodies local fixtureA = event.fixtureA local fixtureB = event.fixtureB local bodyA = fixtureA:getBody() local bodyB = fixtureB:getBody() -- check if colliding body is the wall --if bodyA.type and bodyA.type == "grenade" then if event.contact and bodyB.type == "grenade" then --run the explosion --local x,y = bodyA:getX(), bodyA:getY() --world:explosion(x,y) explode:setPosition(stage.grenade:getX(),stage.grenade:getY()) --explode:setPosition(100,100) explode.movieclip:gotoAndStop(1) end end world:addEventListener(Event.BEGIN_CONTACT, onBeginContact) -- Create a mouse joint on mouse down --create empty box2d body for joint since mouse cursur is not a body ... --...we need dumm body to create joint local ground = world:createBody({}) --joint with dummy body local mousejoint = nil function onMouseDown(event) if stage.grenade:hitTestPoint(event.x,event.y) then local jointDef = b2.createMouseJointDef(ground, stage.grenade.body, event.x,event.y,100000) mouseJoint = world:createJoint(jointDef) end end -- Update the target of mouse joint on mouse move function onMouseMove(event) if mouseJoint ~= nil then mouseJoint:setTarget(event.x,event.y) end end -- Destroy the mouse joint on mouse update function onMouseUp(event) if mouseJoint ~= nil then world:destroyJoint(mouseJoint) mouseJoint = nil end end -- Mouse event listeners stage:addEventListener(Event.MOUSE_DOWN, onMouseDown, world.grenade) stage:addEventListener(Event.MOUSE_MOVE, onMouseMove) stage:addEventListener(Event.MOUSE_UP, onMouseUp) -- Create function to run box2d world and move all sprites according to their -- box2d body objects function onEnterFrame() world:step(1/60, 8, 3) for i = 1, stage:getNumChildren() do --get specific sprites local sprite = stage:getChildAt(i) --check if sprite has a body if sprite.body then --update position to match box2d world object's position local body = sprite.body local bodyX, bodyY = body:getPosition() sprite:setPosition(bodyX,bodyY) sprite:setRotation(body:getAngle() * 180 / math.pi) end end end --create world instance with bounding walls and grenade --function init() local screenW = application:getContentWidth() local screenH = application:getContentHeight() --create bounding walls outside the scene wall(0,screenH/2,10,screenH,"wall") wall(screenW/2,0,screenW,10,"wall") wall(screenW,screenH/2,10,screenH,"wall") wall(screenW/2,screenH,screenW,10,"wall") --create grenade stage.grenade = grenade(60,60) --run world stage:addEventListener(Event.ENTER_FRAME, onEnterFrame) --stage:addEventListener("exitBegin", onExitBegin) --end