28_Urho2DPhysicsRope.as 7.7 KB

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