27_Urho2DPhysics.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. -- Urho2D physics sample.
  2. -- This sample demonstrates:
  3. -- - Creating both static and moving 2D physics objects to a scene
  4. -- - Displaying physics debug geometry
  5. require "LuaScripts/Utilities/Sample"
  6. function Start()
  7. -- Execute the common startup for samples
  8. SampleStart()
  9. -- Create the scene content
  10. CreateScene()
  11. -- Create the UI content
  12. CreateInstructions()
  13. -- Setup the viewport for displaying the scene
  14. SetupViewport()
  15. -- Hook up to the frame update events
  16. SubscribeToEvents()
  17. end
  18. function CreateScene()
  19. scene_ = Scene()
  20. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  21. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  22. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  23. -- optimizing manner
  24. scene_:CreateComponent("Octree")
  25. scene_:CreateComponent("DebugRenderer")
  26. -- Create a scene node for the camera, which we will move around
  27. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  28. cameraNode = scene_:CreateChild("Camera")
  29. -- Set an initial position for the camera scene node above the plane
  30. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  31. local camera = cameraNode:CreateComponent("Camera")
  32. camera.orthographic = true
  33. camera.orthoSize = graphics.height * PIXEL_SIZE
  34. -- Create 2D physics world component
  35. scene_:CreateComponent("PhysicsWorld2D")
  36. local boxSprite = cache:GetResource("Sprite2D", "Urho2D/Box.png")
  37. local ballSprite = cache:GetResource("Sprite2D", "Urho2D/Ball.png")
  38. -- Create ground.
  39. local groundNode = scene_:CreateChild("Ground")
  40. groundNode.position = Vector3(0.0, -3.0, 0.0)
  41. groundNode.scale = Vector3(200.0, 1.0, 0.0)
  42. -- Create 2D rigid body for gound
  43. local groundBody = groundNode:CreateComponent("RigidBody2D")
  44. local groundSprite = groundNode:CreateComponent("StaticSprite2D")
  45. groundSprite.sprite = boxSprite
  46. -- Create box collider for ground
  47. local groundShape = groundNode:CreateComponent("CollisionBox2D")
  48. -- Set box size
  49. groundShape.size = Vector2(0.32, 0.32)
  50. -- Set friction
  51. groundShape.friction = 0.5
  52. local NUM_OBJECTS = 100
  53. for i = 1, NUM_OBJECTS do
  54. local node = scene_:CreateChild("RigidBody")
  55. node.position = Vector3(Random(-0.1, 0.1), 5.0 + i * 0.4, 0.0)
  56. -- Create rigid body
  57. local body = node:CreateComponent("RigidBody2D")
  58. body.bodyType = BT_DYNAMIC
  59. local staticSprite = node:CreateComponent("StaticSprite2D")
  60. local shape = nil
  61. if i % 2 == 0 then
  62. staticSprite.sprite = boxSprite
  63. -- Create box
  64. shape = node:CreateComponent("CollisionBox2D")
  65. -- Set size
  66. shape.size = Vector2(0.32, 0.32)
  67. else
  68. staticSprite.sprite = ballSprite
  69. -- Create circle
  70. shape = node:CreateComponent("CollisionCircle2D")
  71. -- Set radius
  72. shape.radius = 0.16
  73. end
  74. -- Set density
  75. shape.density = 1.0
  76. -- Set friction
  77. shape.friction = 0.5
  78. -- Set restitution
  79. shape.restitution = 0.1
  80. end
  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. end
  140. -- Create XML patch instructions for screen joystick layout specific to this sample app
  141. function GetScreenJoystickPatchString()
  142. return
  143. "<patch>" ..
  144. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  145. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  146. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  147. " <element type=\"Text\">" ..
  148. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  149. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  150. " </element>" ..
  151. " </add>" ..
  152. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  153. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  154. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  155. " <element type=\"Text\">" ..
  156. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  157. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  158. " </element>" ..
  159. " </add>" ..
  160. "</patch>"
  161. end