Rope using Box2d Revolt Jonits

1 vote · 0 comments

this is a simple example of creating a rope using moai and box2d

raw ·
copy
· download
-- screen dimensions Screen = { w = 480*2, h = 360*2 } -- stage dimensions Stage = { w = 480*2, h = 360*2 } -- open sim window MOAISim.openWindow( 'platformer_test', Screen.w, Screen.h ) -- setup viewport viewport = MOAIViewport.new() viewport:setSize( Screen.w, Screen.h ) viewport:setScale( Stage.w, Stage.h ) layer = MOAILayer2D.new () layer:setViewport ( viewport ) layer:setClearColor ( 0.1,0.1,0.1,1 ) MOAISim.pushRenderPass ( layer ) world = MOAIBox2DWorld.new () world:setGravity ( -0, -9.8 ) world:setUnitsToMeters ( 1/30, 10 ) layer:setBox2DWorld ( world ) world:start () roupe = world:addBody ( MOAIBox2DBody.STATIC ) roupe:setTransform ( 0, 0 ) roupeFix = roupe:addRect ( -400, -2, 400, 2 ) roupeFix:setFilter(ROUPE) function revoluteJoint(bodyA,bodyB,anchorA,anchorB) world:addRevoluteJoint(bodyA, bodyB, anchorA.x,anchorA.y) end links= 16 ropeLength = 120/links w=10 h=ropeLength*2 local link for i=1,links do if i == 1 then link = world:addBody( MOAIBox2DBody.DYNAMIC ) link:setTransform(0,-ropeLength-2*ropeLength*(i-1)) link.fixture = link:addRect (-w/2,-h/2,w/2,h/2 ) link.fixture:setDensity(1); link.fixture:setFriction(1); link.fixture:setRestitution(0.1); link:resetMassData(); revoluteJoint(roupe,link,{x=0,y=0}) else local newLink = world:addBody ( MOAIBox2DBody.DYNAMIC ); newLink:setTransform(0,-ropeLength-2*ropeLength*(i-1)) newLink.fixture = newLink:addRect (-w/2,-h/2,w/2,h/2 ) newLink.fixture:setDensity(1); newLink.fixture:setFriction(1); newLink.fixture:setRestitution(0.1); newLink:resetMassData(); revoluteJoint(link,newLink,{x=0,y=-(i-1)*ropeLength*2}); link=newLink; end end steelBall = world:addBody( MOAIBox2DBody.DYNAMIC ) steelBall:setTransform(0,-links*ropeLength*2) steelBall.fixture = steelBall:addCircle (0,0,10) steelBall.fixture:setDensity(1); steelBall.fixture:setFriction(1); steelBall.fixture:setRestitution(0.1); steelBall:resetMassData(); revoluteJoint(link,steelBall,{x=0,y=-2*ropeLength*(links)}); steelBall:setTransform(200,20) function pointerCallback ( x, y ) local oldX = mouseX local oldY = mouseY mouseX, mouseY = layer:wndToWorld ( x, y ) if pick ~= nil then pick.body:setTransform(mouseX, mouseY) end end local mouseJoint = nil function clickCallback ( down ) if down then pick= {} pick.body = steelBall else pick=nil end end if MOAIInputMgr.device.pointer then -- mouse input MOAIInputMgr.device.pointer:setCallback ( pointerCallback ) MOAIInputMgr.device.mouseLeft:setCallback ( clickCallback ) else -- touch input MOAIInputMgr.device.touch:setCallback ( function ( eventType, idx, x, y, tapCount ) pointerCallback ( x, y ) if eventType == MOAITouchSensor.TOUCH_DOWN then clickCallback ( true ) elseif eventType == MOAITouchSensor.TOUCH_UP then clickCallback ( false ) end end ) end

Be the first to comment

Sign in with OpenID