Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "physics"
- physics.start()
- physics.setGravity(0, 9.8)
- physics.setDrawMode("hybrid")
- stage = display.getCurrentStage()
- function getWall(x, y, width, height)
- local wall = display.newRect( x, y, width, height)
- wall:setFillColor( 255, 0, 0, 100)
- physics.addBody(wall, "static", { friction=0.5, bounce=0.0, density=2 } )
- return wall
- end
- function getBall(x, y, radius, type, props)
- if props == nil then
- props = {density=0.3, friction=0.3, bounce=0.3}
- end
- local ball = display.newCircle(x, y, radius)
- ball:setFillColor(0, 255, 0)
- physics.addBody(ball, props)
- return ball
- end
- floor = getWall(0, 460, 320, 20)
- leftWall = getWall(0, 0, 20, 500)
- rightWall = getWall(320, 0, 20, 500)
- roof = getWall(0, 0, 460, 20)
- ball1 = getBall(100, 100, 10, {density=0.3, friction=0.3, bounce=2})
- ball2 = getBall(200, 150, 10, {density=0.3, friction=0.3, bounce=2})
- touchJoint = nil
- function onTouch(event)
- print(tostring(system.getTimer()) .. " phase: ", event.phase)
- if event.phase == "began" then
- dragging = true
- stage:setFocus(ball1)
- ball1.bodyType = "kinetic"
- if touchJoint then
- touchJoint:removeSelf()
- end
- touchJoint = physics.newJoint( "touch", ball1, event.x, event.y )
- --touchJoint.maxForce = 10000000
- touchJoint.frequency = 0.5
- touchJoint.dampingRatio = 0
- return true
- elseif event.phase == "moved" then
- if dragging == false then return false end
- touchJoint:setTarget(event.x, event.y)
- return true
- elseif event.phase == "ended" or event.phase == "cancelled" then
- if dragging == true then
- dragging = false
- ball1.bodyType = "dynamic"
- touchJoint:removeSelf()
- touchJoint = nil
- return true
- else
- return false
- end
- end
- end
- Runtime:addEventListener("touch", onTouch)
Advertisement
Add Comment
Please, Sign In to add comment