28_Physics2DRope.as 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Physics2D 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. }
  87. void CreateInstructions()
  88. {
  89. // Construct new Text object, set string to display and font to use
  90. Text@ instructionText = ui.root.CreateChild("Text");
  91. instructionText.text = "Use WASD keys and mouse to move, Use PageUp PageDown to zoom.";
  92. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  93. // Position the text relative to the screen center
  94. instructionText.horizontalAlignment = HA_CENTER;
  95. instructionText.verticalAlignment = VA_CENTER;
  96. instructionText.SetPosition(0, ui.root.height / 4);
  97. }
  98. void SetupViewport()
  99. {
  100. // 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
  101. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  102. // use, but now we just use full screen and default render path configured in the engine command line options
  103. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  104. renderer.viewports[0] = viewport;
  105. }
  106. void MoveCamera(float timeStep)
  107. {
  108. // Do not move if the UI has a focused element (the console)
  109. if (ui.focusElement !is null)
  110. return;
  111. // Movement speed as world units per second
  112. const float MOVE_SPEED = 4.0f;
  113. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  114. if (input.keyDown[KEY_W])
  115. cameraNode.Translate(Vector3::UP * MOVE_SPEED * timeStep);
  116. if (input.keyDown[KEY_S])
  117. cameraNode.Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  118. if (input.keyDown[KEY_A])
  119. cameraNode.Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  120. if (input.keyDown[KEY_D])
  121. cameraNode.Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  122. if (input.keyDown[KEY_PAGEUP])
  123. {
  124. Camera@ camera = cameraNode.GetComponent("Camera");
  125. camera.zoom = camera.zoom * 1.01f;
  126. }
  127. if (input.keyDown[KEY_PAGEDOWN])
  128. {
  129. Camera@ camera = cameraNode.GetComponent("Camera");
  130. camera.zoom = camera.zoom * 0.99f;
  131. }
  132. }
  133. void SubscribeToEvents()
  134. {
  135. // Subscribe HandleUpdate() function for processing update events
  136. SubscribeToEvent("Update", "HandleUpdate");
  137. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  138. UnsubscribeFromEvent("SceneUpdate");
  139. }
  140. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  141. {
  142. // Take the frame time step, which is stored as a float
  143. float timeStep = eventData["TimeStep"].GetFloat();
  144. // Move the camera, scale movement with time step
  145. MoveCamera(timeStep);
  146. PhysicsWorld2D@ physicsWorld = scene_.GetComponent("PhysicsWorld2D");
  147. physicsWorld.DrawDebugGeometry();
  148. }
  149. // Create XML patch instructions for screen joystick layout specific to this sample app
  150. String patchInstructions =
  151. "<patch>" +
  152. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" +
  153. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" +
  154. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" +
  155. " <element type=\"Text\">" +
  156. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  157. " <attribute name=\"Text\" value=\"PAGEUP\" />" +
  158. " </element>" +
  159. " </add>" +
  160. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" +
  161. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" +
  162. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" +
  163. " <element type=\"Text\">" +
  164. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  165. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" +
  166. " </element>" +
  167. " </add>" +
  168. "</patch>";