27_Urho2DPhysics.as 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "Scripts/Utilities/Sample.as"
  6. void Start()
  7. {
  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. }
  19. void CreateScene()
  20. {
  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.0f, 0.0f, -10.0f);
  33. Camera@ camera = cameraNode.CreateComponent("Camera");
  34. camera.orthographic = true;
  35. camera.orthoSize = graphics.height * PIXEL_SIZE;
  36. // Create 2D physics world component
  37. scene_.CreateComponent("PhysicsWorld2D");
  38. Sprite2D@ boxSprite = cache.GetResource("Sprite2D", "Urho2D/Box.png");
  39. Sprite2D@ ballSprite = cache.GetResource("Sprite2D", "Urho2D/Ball.png");
  40. // Create ground.
  41. Node@ groundNode = scene_.CreateChild("Ground");
  42. groundNode.position = Vector3(0.0f, -3.0f, 0.0f);
  43. groundNode.scale = Vector3(200.0f, 1.0f, 0.0f);
  44. // Create 2D rigid body for gound
  45. RigidBody2D@ groundBody = groundNode.CreateComponent("RigidBody2D");
  46. StaticSprite2D@ groundSprite = groundNode.CreateComponent("StaticSprite2D");
  47. groundSprite.sprite = boxSprite;
  48. // Create box collider for ground
  49. CollisionBox2D@ groundShape = groundNode.CreateComponent("CollisionBox2D");
  50. // Set box size
  51. groundShape.size = Vector2(0.32f, 0.32f);
  52. // Set friction
  53. groundShape.friction = 0.5f;
  54. const uint NUM_OBJECTS = 100;
  55. for (uint i = 0; i < NUM_OBJECTS; ++i)
  56. {
  57. Node@ node = scene_.CreateChild("RigidBody");
  58. node.position = Vector3(Random(-0.1f, 0.1f), 5.0f + i * 0.4f, 0.0f);
  59. // Create rigid body
  60. RigidBody2D@ body = node.CreateComponent("RigidBody2D");
  61. body.bodyType = BT_DYNAMIC;
  62. StaticSprite2D@ staticSprite = node.CreateComponent("StaticSprite2D");
  63. if (i % 2 == 0)
  64. {
  65. staticSprite.sprite = boxSprite;
  66. // Create box
  67. CollisionBox2D@ box = node.CreateComponent("CollisionBox2D");
  68. // Set size
  69. box.size = Vector2(0.32f, 0.32f);
  70. // Set density
  71. box.density = 1.0f;
  72. // Set friction
  73. box.friction = 0.5f;
  74. // Set restitution
  75. box.restitution = 0.1f;
  76. }
  77. else
  78. {
  79. staticSprite.sprite = ballSprite;
  80. // Create circle
  81. CollisionCircle2D@ circle = node.CreateComponent("CollisionCircle2D");
  82. // Set radius
  83. circle.radius = 0.16f;
  84. // Set density
  85. circle.density = 1.0f;
  86. // Set friction.
  87. circle.friction = 0.5f;
  88. // Set restitution
  89. circle.restitution = 0.1f;
  90. }
  91. }
  92. }
  93. void CreateInstructions()
  94. {
  95. // Construct new Text object, set string to display and font to use
  96. Text@ instructionText = ui.root.CreateChild("Text");
  97. instructionText.text = "Use WASD keys and mouse to move, Use PageUp PageDown to zoom.";
  98. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  99. // Position the text relative to the screen center
  100. instructionText.horizontalAlignment = HA_CENTER;
  101. instructionText.verticalAlignment = VA_CENTER;
  102. instructionText.SetPosition(0, ui.root.height / 4);
  103. }
  104. void SetupViewport()
  105. {
  106. // 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
  107. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  108. // use, but now we just use full screen and default render path configured in the engine command line options
  109. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  110. renderer.viewports[0] = viewport;
  111. }
  112. void MoveCamera(float timeStep)
  113. {
  114. // Do not move if the UI has a focused element (the console)
  115. if (ui.focusElement !is null)
  116. return;
  117. // Movement speed as world units per second
  118. const float MOVE_SPEED = 4.0f;
  119. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  120. if (input.keyDown['W'])
  121. cameraNode.Translate(Vector3(0.0f, 1.0f, 0.0f) * MOVE_SPEED * timeStep);
  122. if (input.keyDown['S'])
  123. cameraNode.Translate(Vector3(0.0f, -1.0f, 0.0f) * MOVE_SPEED * timeStep);
  124. if (input.keyDown['A'])
  125. cameraNode.Translate(Vector3(-1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  126. if (input.keyDown['D'])
  127. cameraNode.Translate(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  128. if (input.keyDown[KEY_PAGEUP])
  129. {
  130. Camera@ camera = cameraNode.GetComponent("Camera");
  131. camera.zoom = camera.zoom * 1.01f;
  132. }
  133. if (input.keyDown[KEY_PAGEDOWN])
  134. {
  135. Camera@ camera = cameraNode.GetComponent("Camera");
  136. camera.zoom = camera.zoom * 0.99f;
  137. }
  138. }
  139. void SubscribeToEvents()
  140. {
  141. // Subscribe HandleUpdate() function for processing update events
  142. SubscribeToEvent("Update", "HandleUpdate");
  143. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  144. UnsubscribeFromEvent("SceneUpdate");
  145. }
  146. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  147. {
  148. // Take the frame time step, which is stored as a float
  149. float timeStep = eventData["TimeStep"].GetFloat();
  150. // Move the camera, scale movement with time step
  151. MoveCamera(timeStep);
  152. }
  153. // Create XML patch instructions for screen joystick layout specific to this sample app
  154. String patchInstructions =
  155. "<patch>" +
  156. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" +
  157. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" +
  158. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" +
  159. " <element type=\"Text\">" +
  160. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  161. " <attribute name=\"Text\" value=\"PAGEUP\" />" +
  162. " </element>" +
  163. " </add>" +
  164. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" +
  165. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" +
  166. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" +
  167. " <element type=\"Text\">" +
  168. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  169. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" +
  170. " </element>" +
  171. " </add>" +
  172. "</patch>";