It looks like you're new here. If you want to get involved, click one of these buttons!
-- Modified code from AppCodingEasy require "box2d" scene = Core.class(Sprite) function scene:init() self.world = b2.World.new(0, 9.8, true) local screenW = application:getContentWidth() local screenH = application:getContentHeight() self:wall(0,screenH/2,10, screenH) self:wall(screenW/2,0,screenW,10) self:wall(screenW,screenH/2,10,screenH) self:wall(screenW/2,screenH,screenW,10) self.boxa = self:createBox(150,150,"red") self.boxb = self:createBox(250,250,"blue") self.boxc = self:createBox(10,15,"black") local ground = self.world:createBody({}) local mouseJoint = nil function self:onMouseDown(event) if self.boxa:hitTestPoint(event.x, event.y) then local jointDef = b2.createMouseJointDef(ground, self.boxa.body, event.x, event.y, 100000) mouseJoint = self.world:createJoint(jointDef) elseif self.boxb:hitTestPoint(event.x, event.y) then local jointDef = b2.createMouseJointDef(ground, self.boxb.body, event.x, event.y, 100000) mouseJoint = self.world:createJoint(jointDef) elseif self.boxc:hitTestPoint(event.x, event.y) then local jointDef = b2.createMouseJointDef(ground, self.boxc.body, event.x, event.y, 100000) mouseJoint = self.world:createJoint(jointDef) end end function self:onMouseMove(event) if mouseJoint ~= nil then mouseJoint:setTarget(event.x, event.y) end end function self:onMouseUp(event) if mouseJoint ~= nil then self.world:destroyJoint(mouseJoint) mouseJoint = nil end end self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self) self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self) --local debugDraw = b2.DebugDraw.new(false) --self.world:setDebugDraw(debugDraw) --self:addChild(debugDraw) self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end function scene:wall(x, y, width, height) local wall = Shape.new() wall:setFillStyle(Shape.SOLID, 0xff0000) wall:beginPath() 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) local body = self.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.8} wall.body = body wall.body.type = "wall" local body = self.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) ---------------------------------- Filter Stuff --------------------------------- local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.8} fixture:setFilterData({categoryBits = 8, maskBits = 7, groupIndex = 0}) --------------------------------------------------------------------------------- wall.body = body wall.body.type = "wall" self:addChild(wall) return wall end function scene:createBox(x, y, color) local block = Shape.new() if color == "red" then print("Red") block:setFillStyle(Shape.SOLID, 0xff0000) filterData = {categoryBits = 1, maskBits = 14, groupIndex = 0} elseif color == "blue" then print("Blue") block:setFillStyle(Shape.SOLID, 0x00ff) filterData = {categoryBits = 2, maskBits = 1, groupIndex = 0} else print("Black") block:setFillStyle(Shape.SOLID, 0x000000) filterData = {categoryBits = 4, maskBits = 1, groupIndex = 0} end block:beginPath(Shape.NON_ZERO) block:moveTo(0, 0) block:lineTo(50, 0) block:lineTo(50, 50) block:lineTo(0, 50) block:lineTo(0, 0) block:endPath() x = y - 10 y = y -10 block:setPosition(x,y) local myBody = self.world:createBody{type = b2.DYNAMIC_BODY} myBody:setPosition(block:getX(), block:getY()) myBody:setAngle(block:getRotation() * math.pi/180) local poly = b2.PolygonShape.new() poly:setAsBox(block:getWidth()/2, block:getHeight()/2, 25, 25, 0) local fixture = myBody:createFixture{shape = poly, density = 0.1, friction = 1.0, restitution = 0.01} fixture:setFilterData(filterData) block.body = myBody block.body.type = "block" print(block.body.type) self:addChild(block) return block end function scene:onEnterFrame() self.world:step(1/60, 8, 3) for i = 1, self:getNumChildren() do local sprite = self:getChildAt(i) if sprite.body then local body = sprite.body local bodyX, bodyY = body:getPosition() sprite:setPosition(bodyX, bodyY) sprite:setRotation(body:getAngle() * 180 / math.pi) end end end local mainScene = scene.new() stage:addChild(mainScene) |
Likes: gorkem, avo, ar2rsawseen, MoKaLux
Dislikes: hosamred, BigWinston
Comments
great tutorial, thanks.
BTW you can put your code inside
<pre lang="lua">
</pre>
to mark it up
cheers
evs
n.b. The ^ above means to the power of so 2 ^ 4 would be 2 to the power of 4 or (2 x 2 x 2 x 2) = 16
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
Thanks
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
As it is mentioned above
- categoryBits and maskBits must be powers of 2.
But, I got confused after reading
categoryBits are fine and all are powers of 2...
I'm getting confused for how to set maskBits..
As I've set categoryBits for all my objects as follows:
1, 2, 4, 8 and 16 but what about maskBits?????
maskBits is the sum of all other categoryBits with which your box2d body should collide., if you make it collide with bodies which has categoryBits of 1, 2 and 4, as the result (sum of all category bits) you will have maskBits 7.
Hope that helps
Something like this:
Likes: ar2rsawseen, MoKaLux