28_Urho2DPhysicsRope.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. camera.zoom = 1.5 * Min(graphics.width / 1280, graphics.height / 800) -- Set zoom according to user's resolution to ensure full visibility (initial zoom (1.5) is set for full visibility at 1280x800 resolution)
  36. -- Create 2D physics world component
  37. local physicsWorld = scene_:CreateComponent("PhysicsWorld2D")
  38. physicsWorld.drawJoint = true
  39. -- Create ground.
  40. local groundNode = scene_:CreateChild("Ground")
  41. -- Create 2D rigid body for gound
  42. local groundBody = groundNode:CreateComponent("RigidBody2D")
  43. -- Create edge collider for ground
  44. local groundShape = groundNode:CreateComponent("CollisionEdge2D")
  45. groundShape:SetVertices(Vector2(-40.0, 0.0), Vector2(40.0, 0.0))
  46. local y = 15.0
  47. local prevBody = groundBody
  48. local NUM_OBJECTS = 10
  49. for i = 0, NUM_OBJECTS - 1 do
  50. local node = scene_:CreateChild("RigidBody")
  51. -- Create rigid body
  52. local body = node:CreateComponent("RigidBody2D")
  53. body.bodyType = BT_DYNAMIC
  54. -- Create box
  55. local box = node:CreateComponent("CollisionBox2D")
  56. -- Set friction
  57. box.friction = 0.2
  58. -- Set mask bits.
  59. box.maskBits = 0xFFFF - 0x0002
  60. if i == NUM_OBJECTS - 1 then
  61. node.position = Vector3(1.0 * i, y, 0.0)
  62. body.angularDamping = 0.4
  63. box:SetSize(3.0, 3.0)
  64. box.density = 100.0
  65. box.categoryBits = 0x0002
  66. else
  67. node.position = Vector3(0.5 + 1.0 * i, y, 0.0)
  68. box:SetSize(1.0, 0.25)
  69. box.density = 20.0
  70. box.categoryBits = 0x0001
  71. end
  72. local joint = node:CreateComponent("ConstraintRevolute2D")
  73. joint.otherBody = prevBody
  74. joint.anchor = Vector2(i, y)
  75. joint.collideConnected = false
  76. prevBody = body
  77. end
  78. local constraintRope = groundNode:CreateComponent("ConstraintRope2D")
  79. constraintRope.otherBody = prevBody
  80. constraintRope.ownerBodyAnchor = Vector2(0.0, y)
  81. constraintRope.maxLength = NUM_OBJECTS - 1.0 + 0.01
  82. end
  83. function CreateInstructions()
  84. -- Construct new Text object, set string to display and font to use
  85. local instructionText = ui.root:CreateChild("Text")
  86. instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.")
  87. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  88. -- Position the text relative to the screen center
  89. instructionText.horizontalAlignment = HA_CENTER
  90. instructionText.verticalAlignment = VA_CENTER
  91. instructionText:SetPosition(0, ui.root.height / 4)
  92. end
  93. function SetupViewport()
  94. -- 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
  95. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  96. -- use, but now we just use full screen and default render path configured in the engine command line options
  97. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  98. renderer:SetViewport(0, viewport)
  99. end
  100. function MoveCamera(timeStep)
  101. -- Do not move if the UI has a focused element (the console)
  102. if ui.focusElement ~= nil then
  103. return
  104. end
  105. -- Movement speed as world units per second
  106. local MOVE_SPEED = 4.0
  107. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  108. if input:GetKeyDown(KEY_W) then
  109. cameraNode:Translate(Vector3(0.0, 1.0, 0.0) * MOVE_SPEED * timeStep)
  110. end
  111. if input:GetKeyDown(KEY_S) then
  112. cameraNode:Translate(Vector3(0.0, -1.0, 0.0) * MOVE_SPEED * timeStep)
  113. end
  114. if input:GetKeyDown(KEY_A) then
  115. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  116. end
  117. if input:GetKeyDown(KEY_D) then
  118. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  119. end
  120. if input:GetKeyDown(KEY_PAGEUP) then
  121. local camera = cameraNode:GetComponent("Camera")
  122. camera.zoom = camera.zoom * 1.01
  123. end
  124. if input:GetKeyDown(KEY_PAGEDOWN) then
  125. local camera = cameraNode:GetComponent("Camera")
  126. camera.zoom = camera.zoom * 0.99
  127. end
  128. end
  129. function SubscribeToEvents()
  130. -- Subscribe HandleUpdate() function for processing update events
  131. SubscribeToEvent("Update", "HandleUpdate")
  132. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  133. UnsubscribeFromEvent("SceneUpdate")
  134. end
  135. function HandleUpdate(eventType, eventData)
  136. -- Take the frame time step, which is stored as a float
  137. local timeStep = eventData["TimeStep"]:GetFloat()
  138. -- Move the camera, scale movement with time step
  139. MoveCamera(timeStep)
  140. local physicsWorld = scene_:GetComponent("PhysicsWorld2D")
  141. physicsWorld:DrawDebugGeometry()
  142. end
  143. -- Create XML patch instructions for screen joystick layout specific to this sample app
  144. function GetScreenJoystickPatchString()
  145. return
  146. "<patch>" ..
  147. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  148. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  149. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  150. " <element type=\"Text\">" ..
  151. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  152. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  153. " </element>" ..
  154. " </add>" ..
  155. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  156. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  157. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  158. " <element type=\"Text\">" ..
  159. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  160. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  161. " </element>" ..
  162. " </add>" ..
  163. "</patch>"
  164. end