27_Urho2DPhysics.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. -- Set the mouse mode to use in the sample
  16. SampleInitMouseMode(MM_FREE)
  17. -- Hook up to the frame update events
  18. SubscribeToEvents()
  19. end
  20. function CreateScene()
  21. scene_ = Scene()
  22. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  23. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  24. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  25. -- optimizing manner
  26. scene_:CreateComponent("Octree")
  27. scene_:CreateComponent("DebugRenderer")
  28. -- Create a scene node for the camera, which we will move around
  29. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  30. cameraNode = scene_:CreateChild("Camera")
  31. -- Set an initial position for the camera scene node above the plane
  32. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  33. local camera = cameraNode:CreateComponent("Camera")
  34. camera.orthographic = true
  35. camera.orthoSize = graphics.height * PIXEL_SIZE
  36. camera.zoom = 1.2 * Min(graphics.width / 1280, graphics.height / 800) -- Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)
  37. -- Create 2D physics world component
  38. scene_:CreateComponent("PhysicsWorld2D")
  39. local boxSprite = cache:GetResource("Sprite2D", "Urho2D/Box.png")
  40. local ballSprite = cache:GetResource("Sprite2D", "Urho2D/Ball.png")
  41. -- Create ground.
  42. local groundNode = scene_:CreateChild("Ground")
  43. groundNode.position = Vector3(0.0, -3.0, 0.0)
  44. groundNode.scale = Vector3(200.0, 1.0, 0.0)
  45. -- Create 2D rigid body for gound
  46. local groundBody = groundNode:CreateComponent("RigidBody2D")
  47. local groundSprite = groundNode:CreateComponent("StaticSprite2D")
  48. groundSprite.sprite = boxSprite
  49. -- Create box collider for ground
  50. local groundShape = groundNode:CreateComponent("CollisionBox2D")
  51. -- Set box size
  52. groundShape.size = Vector2(0.32, 0.32)
  53. -- Set friction
  54. groundShape.friction = 0.5
  55. local NUM_OBJECTS = 100
  56. for i = 1, NUM_OBJECTS do
  57. local node = scene_:CreateChild("RigidBody")
  58. node.position = Vector3(Random(-0.1, 0.1), 5.0 + i * 0.4, 0.0)
  59. -- Create rigid body
  60. local body = node:CreateComponent("RigidBody2D")
  61. body.bodyType = BT_DYNAMIC
  62. local staticSprite = node:CreateComponent("StaticSprite2D")
  63. local shape = nil
  64. if i % 2 == 0 then
  65. staticSprite.sprite = boxSprite
  66. -- Create box
  67. shape = node:CreateComponent("CollisionBox2D")
  68. -- Set size
  69. shape.size = Vector2(0.32, 0.32)
  70. else
  71. staticSprite.sprite = ballSprite
  72. -- Create circle
  73. shape = node:CreateComponent("CollisionCircle2D")
  74. -- Set radius
  75. shape.radius = 0.16
  76. end
  77. -- Set density
  78. shape.density = 1.0
  79. -- Set friction
  80. shape.friction = 0.5
  81. -- Set restitution
  82. shape.restitution = 0.1
  83. end
  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. 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