27_Urho2DPhysics.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. 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)
  35. -- Create 2D physics world component
  36. scene_:CreateComponent("PhysicsWorld2D")
  37. local boxSprite = cache:GetResource("Sprite2D", "Urho2D/Box.png")
  38. local ballSprite = cache:GetResource("Sprite2D", "Urho2D/Ball.png")
  39. -- Create ground.
  40. local groundNode = scene_:CreateChild("Ground")
  41. groundNode.position = Vector3(0.0, -3.0, 0.0)
  42. groundNode.scale = Vector3(200.0, 1.0, 0.0)
  43. -- Create 2D rigid body for gound
  44. local groundBody = groundNode:CreateComponent("RigidBody2D")
  45. local groundSprite = groundNode:CreateComponent("StaticSprite2D")
  46. groundSprite.sprite = boxSprite
  47. -- Create box collider for ground
  48. local groundShape = groundNode:CreateComponent("CollisionBox2D")
  49. -- Set box size
  50. groundShape.size = Vector2(0.32, 0.32)
  51. -- Set friction
  52. groundShape.friction = 0.5
  53. local NUM_OBJECTS = 100
  54. for i = 1, NUM_OBJECTS do
  55. local node = scene_:CreateChild("RigidBody")
  56. node.position = Vector3(Random(-0.1, 0.1), 5.0 + i * 0.4, 0.0)
  57. -- Create rigid body
  58. local body = node:CreateComponent("RigidBody2D")
  59. body.bodyType = BT_DYNAMIC
  60. local staticSprite = node:CreateComponent("StaticSprite2D")
  61. local shape = nil
  62. if i % 2 == 0 then
  63. staticSprite.sprite = boxSprite
  64. -- Create box
  65. shape = node:CreateComponent("CollisionBox2D")
  66. -- Set size
  67. shape.size = Vector2(0.32, 0.32)
  68. else
  69. staticSprite.sprite = ballSprite
  70. -- Create circle
  71. shape = node:CreateComponent("CollisionCircle2D")
  72. -- Set radius
  73. shape.radius = 0.16
  74. end
  75. -- Set density
  76. shape.density = 1.0
  77. -- Set friction
  78. shape.friction = 0.5
  79. -- Set restitution
  80. shape.restitution = 0.1
  81. end
  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:GetFloat("TimeStep")
  138. -- Move the camera, scale movement with time step
  139. MoveCamera(timeStep)
  140. end
  141. -- Create XML patch instructions for screen joystick layout specific to this sample app
  142. function GetScreenJoystickPatchString()
  143. return
  144. "<patch>" ..
  145. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  146. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  147. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  148. " <element type=\"Text\">" ..
  149. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  150. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  151. " </element>" ..
  152. " </add>" ..
  153. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  154. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  155. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  156. " <element type=\"Text\">" ..
  157. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  158. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  159. " </element>" ..
  160. " </add>" ..
  161. "</patch>"
  162. end