28_Urho2DPhysicsRope.lua 7.9 KB

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