jesterxl

Corona SDK Pulley Joint Example

Jan 18th, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. require "physics"
  2. physics.start()
  3. physics.setGravity(0, 9.8)
  4. physics.setDrawMode("hybrid")
  5.  
  6. function getBall(x, y, radius, type, props)
  7.     if props == nil then
  8.         props = {density=0.3, friction=0.3, bounce=0.3}
  9.     end
  10.     local ball = display.newCircle(x, y, radius)
  11.     ball:setFillColor(0, 255, 0)
  12.     physics.addBody(ball, props)
  13.     return ball
  14. end
  15.  
  16. function getWall(x, y, width, height)
  17.     local wall = display.newRect( x, y, width, height)
  18.     wall:setFillColor( 255, 0, 0, 100)
  19.     physics.addBody(wall, "static", { friction=0.5, bounce=0.0, density=2 } )
  20.     return wall
  21. end
  22.  
  23. floor = getWall(0, 460, 320, 20)
  24. leftWall = getWall(0, 0, 20, 500)
  25. rightWall = getWall(320, 0, 20, 500)
  26. roof = getWall(0, 0, 460, 20)
  27.  
  28. ball1 = getBall(100, 100, 10)
  29. ball2 = getBall(100, 200, 10, {density=2, friction=0.3, bounce=2})
  30.  
  31. length1Text = display.newText("Hello World!", 0, 0, native.systemFont, 16)
  32. length2Text = display.newText("Hello World!", 0, 0, native.systemFont, 16)
  33.  
  34. length1Text.x = 200
  35. length1Text.y = 300
  36. length2Text.x = length1Text.x
  37. length2Text.y = length1Text.y + 22
  38.  
  39. dragging = false
  40. lastClicked = nil
  41.  
  42. function touch(event)
  43.     length1Text.text = tostring(pulleyJoint.length1)
  44.     length2Text.text = tostring(pulleyJoint.length2)
  45.    
  46.     if pulleyJoint.length2 > 170 or pulleyJoint.length1 > 170 then
  47.         dragging = false
  48.         if lastClicked then
  49.             lastClicked.bodyType = "dynamic"
  50.             lastClicked = nil
  51.         end
  52.     end
  53.    
  54.     local phase = event.phase
  55.     if phase == "began" then
  56.         dragging = true
  57.         lastClicked = event.target
  58.         lastClicked.bodyType = "kinematic"
  59.         -- Stop current motion, if any
  60.         lastClicked:setLinearVelocity( 0, 0 )
  61.         lastClicked.angularVelocity = 0
  62.         return true
  63.     elseif phase == "moved" then
  64.         if dragging == false then return false end
  65.         lastClicked.x = event.x
  66.         lastClicked.y = event.y
  67.         return true
  68.     elseif phase == "ended" or phase == "cancelled" then
  69.         if dragging == true then
  70.             dragging = false
  71.             lastClicked.bodyType = "dynamic"
  72.             lastClicked = nil
  73.             return true
  74.         else
  75.             return false
  76.         end
  77.     end
  78. end
  79.  
  80. ball1:addEventListener("touch", touch)
  81. ball2:addEventListener("touch", touch)
  82.  
  83. 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