This example is regarding the .isLimitEnabled and
:setRotationLimits
The hammer is in a 3 o’clock position ready to strike a gong. The lever is in an 8 o clock position which should rotate the hammer to strike the gong. A circle falls onto the lever triggering the whole thing.
[lua]
display.setStatusBar( display.HiddenStatusBar )
local physics = require(“physics”)
physics.start(true)
physics.setGravity(0, 9.8)
centerX = display.contentWidth/2
centerY = display.contentHeight/2
–SOUNDS
gong_sound = audio.loadSound( “sounds/Single_Gong.mp3” )
gongs_sound = audio.loadSound( “sounds/ Gong.mp3” )
audio.setVolume( .3 )
–IMAGES
local bg = display.newRect( centerX, centerY, display.contentWidth, display.contentHeight )
local fastener = display.newCircle( display.contentWidth/2, -5, 25 )
fastener:setFillColor( 0,0,0 )
local gongRope = display.newImage( “images/rope.png”, centerX, centerY*.25 )
local gong = display.newImage( “images/Burmabell.png”, centerX, centerY-175)
local timerCircle = display.newCircle( centerX, centerY+100, 30 )
timerCircle:setFillColor( 1, 0, 0, .4 )
leverGroup = display.newGroup( )
local hammer = display.newImage( “images/hammer.png”, timerCircle.x+100, timerCircle.y-95 )
hammer.rotation = 45
leverGroup:insert( hammer)
local floor = display.newRect( 0, display.contentHeight, display.contentWidth*2, 10 )
floor:setFillColor( 0,0,0 )
local post = display.newRect( centerX-350, centerY+200, 40, 630 )
post:setFillColor( 1,0,0 )
local lever = display.newRect( timerCircle.x-150, timerCircle.y, 300, 20 )
lever:setFillColor (.8, .5, .3)
leverGroup:insert(lever)
local ball = display.newCircle( post.x+4.5, post.y-329, 35 )
ball:setFillColor (0, .5, 1)
local ticker = display.newRect( post.x-29, post.y+305, 65, 20 )
ticker:setFillColor (0, .5, 0)
–PHYSICS
physics.addBody( fastener, “static”)
physics.addBody( gongRope, “dynamic”, {density=.1, friction=0, bounce=.3 } )
physics.addBody( gong, “dynamic”, {density=.2, friction=.5, bounce=.8 } )
physics.addBody( hammer, “dynamic”, {density=.8, friction=.3, bounce=.8 })
physics.addBody( timerCircle, “static” )
physics.addBody( post, “static” )
physics.addBody( lever, “dynamic”, {density=.3, friction=.3, bounce=.5 })
physics.addBody( ball, “dynamic”, {density=1, friction=.3, bounce=3 })
physics.addBody( ticker, “static”)
physics.addBody( floor, “static”)
–JOINTS
local stringJoint = physics.newJoint( “pivot”, fastener, gongRope, fastener.x, fastener.y )
local gongJoint = physics.newJoint( “weld”, gongRope, gong, gongRope.x, gongRope.y)
local hammerJoint = physics.newJoint( “pivot”, timerCircle, hammer, timerCircle.x, timerCircle.y-85)
hammerJoint.isLimitedEnabled=true
hammerJoint:setRotationLimits(0,45)
local lev_Ham_Joint = physics.newJoint(“pivot”, timerCircle, lever, timerCircle.x, timerCircle.y-25 )
lev_Ham_Joint.isLimitedEnabled=true
lev_Ham_Joint:setRotationLimits(180,360)
– local lev_hammerJoint = physics.newJoint( “pivot”, hammer, lever, timerCircle.x, timerCircle.y)
– lev_hammerJoint.isLimitedEnabled=true
– lev_hammerJoint:setRotationLimits(-180,180)
local function moveTicker()
transition.to(ticker, {time=10000, y=(ball.y-20) })
end
moveTicker()
local function playGong (event)
if event.phase == “began” then
audio.play(gong_sound)
print( “It gonged!” )
end
end
gong:addEventListener( “collision”, playGong )
physics.setDrawMode(“normal”)
[/lua]