28_Urho2DPhysicsRope.as 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Urho2D physics rope sample.
  2. // This sample demonstrates.
  3. // - Create revolute constraint
  4. // - Create roop constraint
  5. // - Displaying physics debug geometry
  6. #include "Scripts/Utilities/Sample.as"
  7. void Start()
  8. {
  9. // Execute the common startup for samples
  10. SampleStart();
  11. // Create the scene content
  12. CreateScene();
  13. // Create the UI content
  14. CreateInstructions();
  15. // Setup the viewport for displaying the scene
  16. SetupViewport();
  17. // Set the mouse mode to use in the sample
  18. SampleInitMouseMode(MM_FREE);
  19. // Hook up to the frame update events
  20. SubscribeToEvents();
  21. }
  22. void CreateScene()
  23. {
  24. scene_ = Scene();
  25. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  26. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  27. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  28. // optimizing manner
  29. scene_.CreateComponent("Octree");
  30. scene_.CreateComponent("DebugRenderer");
  31. // Create a scene node for the camera, which we will move around
  32. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  33. cameraNode = scene_.CreateChild("Camera");
  34. // Set an initial position for the camera scene node above the plane
  35. cameraNode.position = Vector3(0.0f, 5.0f, -10.0f);
  36. Camera@ camera = cameraNode.CreateComponent("Camera");
  37. camera.orthographic = true;
  38. camera.orthoSize = graphics.height * 0.05f;
  39. camera.zoom = 1.5f * Min(graphics.width / 1280.0f, graphics.height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.5) is set for full visibility at 1280x800 resolution)
  40. // Create 2D physics world component
  41. PhysicsWorld2D@ physicsWorld = scene_.CreateComponent("PhysicsWorld2D");
  42. physicsWorld.drawJoint = true;
  43. // Create ground.
  44. Node@ groundNode = scene_.CreateChild("Ground");
  45. // Create 2D rigid body for gound
  46. RigidBody2D@ groundBody = groundNode.CreateComponent("RigidBody2D");
  47. // Create edge collider for ground
  48. CollisionEdge2D@ groundShape = groundNode.CreateComponent("CollisionEdge2D");
  49. groundShape.SetVertices(Vector2(-40.0f, 0.0f), Vector2(40.0f, 0.0f));
  50. const float y = 15.0f;
  51. RigidBody2D@ prevBody = groundBody;
  52. const uint NUM_OBJECTS = 10;
  53. for (uint i = 0; i < NUM_OBJECTS; ++i)
  54. {
  55. Node@ node = scene_.CreateChild("RigidBody");
  56. // Create rigid body
  57. RigidBody2D@ body = node.CreateComponent("RigidBody2D");
  58. body.bodyType = BT_DYNAMIC;
  59. // Create box
  60. CollisionBox2D@ box = node.CreateComponent("CollisionBox2D");
  61. // Set friction
  62. box.friction = 0.2f;
  63. // Set mask bits.
  64. box.maskBits = 0xFFFF & ~0x0002;
  65. if (i == NUM_OBJECTS - 1)
  66. {
  67. node.position = Vector3(1.0f * i, y, 0.0f);
  68. body.angularDamping = 0.4f;
  69. box.SetSize(3.0f, 3.0f);
  70. box.density = 100.0f;
  71. box.categoryBits = 0x0002;
  72. }
  73. else
  74. {
  75. node.position = Vector3(0.5f + 1.0f * i, y, 0.0f);
  76. box.SetSize(1.0f, 0.25f);
  77. box.density = 20.0f;
  78. box.categoryBits = 0x0001;
  79. }
  80. ConstraintRevolute2D@ joint = node.CreateComponent("ConstraintRevolute2D");
  81. joint.otherBody = prevBody;
  82. joint.anchor = Vector2(i, y);
  83. joint.collideConnected = false;
  84. prevBody = body;
  85. }
  86. ConstraintRope2D@ constraintRope = groundNode.CreateComponent("ConstraintRope2D");
  87. constraintRope.otherBody = prevBody;
  88. constraintRope.ownerBodyAnchor = Vector2(0.0f, y);
  89. constraintRope.maxLength = NUM_OBJECTS - 1.0f + 0.01f;
  90. }
  91. void CreateInstructions()
  92. {
  93. // Construct new Text object, set string to display and font to use
  94. Text@ instructionText = ui.root.CreateChild("Text");
  95. instructionText.text = "Use WASD keys and mouse to move, Use PageUp PageDown to zoom.";
  96. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  97. // Position the text relative to the screen center
  98. instructionText.horizontalAlignment = HA_CENTER;
  99. instructionText.verticalAlignment = VA_CENTER;
  100. instructionText.SetPosition(0, ui.root.height / 4);
  101. }
  102. void SetupViewport()
  103. {
  104. // 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
  105. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  106. // use, but now we just use full screen and default render path configured in the engine command line options
  107. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  108. renderer.viewports[0] = viewport;
  109. }
  110. void MoveCamera(float timeStep)
  111. {
  112. // Do not move if the UI has a focused element (the console)
  113. if (ui.focusElement !is null)
  114. return;
  115. // Movement speed as world units per second
  116. const float MOVE_SPEED = 4.0f;
  117. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  118. if (input.keyDown[KEY_W])
  119. cameraNode.Translate(Vector3::UP * MOVE_SPEED * timeStep);
  120. if (input.keyDown[KEY_S])
  121. cameraNode.Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  122. if (input.keyDown[KEY_A])
  123. cameraNode.Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  124. if (input.keyDown[KEY_D])
  125. cameraNode.Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  126. if (input.keyDown[KEY_PAGEUP])
  127. {
  128. Camera@ camera = cameraNode.GetComponent("Camera");
  129. camera.zoom = camera.zoom * 1.01f;
  130. }
  131. if (input.keyDown[KEY_PAGEDOWN])
  132. {
  133. Camera@ camera = cameraNode.GetComponent("Camera");
  134. camera.zoom = camera.zoom * 0.99f;
  135. }
  136. }
  137. void SubscribeToEvents()
  138. {
  139. // Subscribe HandleUpdate() function for processing update events
  140. SubscribeToEvent("Update", "HandleUpdate");
  141. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  142. UnsubscribeFromEvent("SceneUpdate");
  143. }
  144. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  145. {
  146. // Take the frame time step, which is stored as a float
  147. float timeStep = eventData["TimeStep"].GetFloat();
  148. // Move the camera, scale movement with time step
  149. MoveCamera(timeStep);
  150. PhysicsWorld2D@ physicsWorld = scene_.GetComponent("PhysicsWorld2D");
  151. physicsWorld.DrawDebugGeometry();
  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>";