28_Urho2DPhysicsRope.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. -- Urho2D physics rope sample.
  2. -- This sample demonstrates:
  3. -- - Create revolute constraint
  4. -- - Create roop constraint
  5. -- - Displaying physics debug geometry
  6. require "LuaScripts/Utilities/Sample"
  7. function Start()
  8. -- Execute the common startup for samples
  9. SampleStart()
  10. -- Create the scene content
  11. CreateScene()
  12. -- Create the UI content
  13. CreateInstructions()
  14. -- Setup the viewport for displaying the scene
  15. SetupViewport()
  16. -- Hook up to the frame update events
  17. SubscribeToEvents()
  18. end
  19. function CreateScene()
  20. scene_ = Scene()
  21. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  22. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  23. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  24. -- optimizing manner
  25. scene_:CreateComponent("Octree")
  26. scene_:CreateComponent("DebugRenderer")
  27. -- Create a scene node for the camera, which we will move around
  28. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  29. cameraNode = scene_:CreateChild("Camera")
  30. -- Set an initial position for the camera scene node above the plane
  31. cameraNode.position = Vector3(0.0, 5.0, -10.0)
  32. local camera = cameraNode:CreateComponent("Camera")
  33. camera.orthographic = true
  34. camera.orthoSize = graphics.height * 0.05
  35. -- Create 2D physics world component
  36. local physicsWorld = scene_:CreateComponent("PhysicsWorld2D")
  37. physicsWorld.drawJoint = true
  38. -- Create ground.
  39. local groundNode = scene_:CreateChild("Ground")
  40. -- Create 2D rigid body for gound
  41. local groundBody = groundNode:CreateComponent("RigidBody2D")
  42. -- Create edge collider for ground
  43. local groundShape = groundNode:CreateComponent("CollisionEdge2D")
  44. groundShape:SetVertices(Vector2(-40.0, 0.0), Vector2(40.0, 0.0))
  45. local y = 15.0
  46. local prevBody = groundBody
  47. local NUM_OBJECTS = 10
  48. for i = 0, NUM_OBJECTS - 1 do
  49. local node = scene_:CreateChild("RigidBody")
  50. -- Create rigid body
  51. local body = node:CreateComponent("RigidBody2D")
  52. body.bodyType = BT_DYNAMIC
  53. -- Create box
  54. local box = node:CreateComponent("CollisionBox2D")
  55. -- Set friction
  56. box.friction = 0.2
  57. -- Set mask bits.
  58. box.maskBits = 0xFFFF - 0x0002
  59. if i == NUM_OBJECTS - 1 then
  60. node.position = Vector3(1.0 * i, y, 0.0)
  61. body.angularDamping = 0.4
  62. box:SetSize(3.0, 3.0)
  63. box.density = 100.0
  64. box.categoryBits = 0x0002
  65. else
  66. node.position = Vector3(0.5 + 1.0 * i, y, 0.0)
  67. box:SetSize(1.0, 0.25)
  68. box.density = 20.0
  69. box.categoryBits = 0x0001
  70. end
  71. local joint = node:CreateComponent("ConstraintRevolute2D")
  72. joint.otherBody = prevBody
  73. joint.anchor = Vector2(i, y)
  74. joint.collideConnected = false
  75. prevBody = body
  76. end
  77. local constraintRope = groundNode:CreateComponent("ConstraintRope2D")
  78. constraintRope.otherBody = prevBody
  79. constraintRope.ownerBodyAnchor = Vector2(0.0, y)
  80. constraintRope.maxLength = NUM_OBJECTS - 1.0 + 0.01
  81. end
  82. function CreateInstructions()
  83. -- Construct new Text object, set string to display and font to use
  84. local instructionText = ui.root:CreateChild("Text")
  85. instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.")
  86. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  87. -- Position the text relative to the screen center
  88. instructionText.horizontalAlignment = HA_CENTER
  89. instructionText.verticalAlignment = VA_CENTER
  90. instructionText:SetPosition(0, ui.root.height / 4)
  91. end
  92. function SetupViewport()
  93. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  94. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  95. -- use, but now we just use full screen and default render path configured in the engine command line options
  96. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  97. renderer:SetViewport(0, viewport)
  98. end
  99. function MoveCamera(timeStep)
  100. -- Do not move if the UI has a focused element (the console)
  101. if ui.focusElement ~= nil then
  102. return
  103. end
  104. -- Movement speed as world units per second
  105. local MOVE_SPEED = 4.0
  106. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  107. if input:GetKeyDown(KEY_W) then
  108. cameraNode:Translate(Vector3(0.0, 1.0, 0.0) * MOVE_SPEED * timeStep)
  109. end
  110. if input:GetKeyDown(KEY_S) then
  111. cameraNode:Translate(Vector3(0.0, -1.0, 0.0) * MOVE_SPEED * timeStep)
  112. end
  113. if input:GetKeyDown(KEY_A) then
  114. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  115. end
  116. if input:GetKeyDown(KEY_D) then
  117. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  118. end
  119. if input:GetKeyDown(KEY_PAGEUP) then
  120. local camera = cameraNode:GetComponent("Camera")
  121. camera.zoom = camera.zoom * 1.01
  122. end
  123. if input:GetKeyDown(KEY_PAGEDOWN) then
  124. local camera = cameraNode:GetComponent("Camera")
  125. camera.zoom = camera.zoom * 0.99
  126. end
  127. end
  128. function SubscribeToEvents()
  129. -- Subscribe HandleUpdate() function for processing update events
  130. SubscribeToEvent("Update", "HandleUpdate")
  131. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  132. UnsubscribeFromEvent("SceneUpdate")
  133. end
  134. function HandleUpdate(eventType, eventData)
  135. -- Take the frame time step, which is stored as a float
  136. local timeStep = eventData:GetFloat("TimeStep")
  137. -- Move the camera, scale movement with time step
  138. MoveCamera(timeStep)
  139. local physicsWorld = scene_:GetComponent("PhysicsWorld2D")
  140. physicsWorld:DrawDebugGeometry()
  141. end
  142. -- Create XML patch instructions for screen joystick layout specific to this sample app
  143. function GetScreenJoystickPatchString()
  144. return
  145. "<patch>" ..
  146. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  147. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  148. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  149. " <element type=\"Text\">" ..
  150. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  151. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  152. " </element>" ..
  153. " </add>" ..
  154. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  155. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  156. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  157. " <element type=\"Text\">" ..
  158. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  159. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  160. " </element>" ..
  161. " </add>" ..
  162. "</patch>"
  163. end