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")
- 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
- 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
- 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)
- ball2 = getBall(100, 200, 10, {density=2, friction=0.3, bounce=2})
- length1Text = display.newText("Hello World!", 0, 0, native.systemFont, 16)
- length2Text = display.newText("Hello World!", 0, 0, native.systemFont, 16)
- length1Text.x = 200
- length1Text.y = 300
- length2Text.x = length1Text.x
- length2Text.y = length1Text.y + 22
- dragging = false
- lastClicked = nil
- function touch(event)
- length1Text.text = tostring(pulleyJoint.length1)
- length2Text.text = tostring(pulleyJoint.length2)
- if pulleyJoint.length2 > 170 or pulleyJoint.length1 > 170 then
- dragging = false
- if lastClicked then
- lastClicked.bodyType = "dynamic"
- lastClicked = nil
- end
- end
- local phase = event.phase
- if phase == "began" then
- dragging = true
- lastClicked = event.target
- lastClicked.bodyType = "kinematic"
- -- Stop current motion, if any
- lastClicked:setLinearVelocity( 0, 0 )
- lastClicked.angularVelocity = 0
- return true
- elseif phase == "moved" then
- if dragging == false then return false end
- lastClicked.x = event.x
- lastClicked.y = event.y
- return true
- elseif phase == "ended" or phase == "cancelled" then
- if dragging == true then
- dragging = false
- lastClicked.bodyType = "dynamic"
- lastClicked = nil
- return true
- else
- return false
- end
- end
- end
- ball1:addEventListener("touch", touch)
- ball2:addEventListener("touch", touch)
- pulleyJoint = physics.newJoint( "pulley", ball1, ball2, 150, 50, 170,50, ball1.x,ball1.y, ball2.x,ball2.y, 1.0 )
Advertisement
Add Comment
Please, Sign In to add comment