28_Urho2DPhysicsRope.as 7.9 KB

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