PhysicsRope2D.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Graphics/Camera.h>
  26. #include <Atomic/Graphics/DebugRenderer.h>
  27. #include <Atomic/Graphics/Graphics.h>
  28. #include <Atomic/Graphics/Octree.h>
  29. #include <Atomic/Graphics/Renderer.h>
  30. #include <Atomic/Input/Input.h>
  31. #include <Atomic/UI/UI.h>
  32. #include <Atomic/Scene/Scene.h>
  33. #include <Atomic/Scene/SceneEvents.h>
  34. #include <Atomic/Atomic2D/CollisionBox2D.h>
  35. #include <Atomic/Atomic2D/CollisionEdge2D.h>
  36. #include <Atomic/Atomic2D/ConstraintRevolute2D.h>
  37. #include <Atomic/Atomic2D/ConstraintRope2D.h>
  38. #include <Atomic/Atomic2D/PhysicsWorld2D.h>
  39. #include <Atomic/Atomic2D/RigidBody2D.h>
  40. #include "PhysicsRope2D.h"
  41. #include <Atomic/DebugNew.h>
  42. static const unsigned NUM_OBJECTS = 10;
  43. PhysicsRope2D::PhysicsRope2D(Context* context) :
  44. Sample(context)
  45. {
  46. }
  47. void PhysicsRope2D::Start()
  48. {
  49. // Execute base class startup
  50. Sample::Start();
  51. // Create the scene content
  52. CreateScene();
  53. // Create the UI content
  54. CreateInstructions();
  55. // Setup the viewport for displaying the scene
  56. SetupViewport();
  57. // Hook up to the frame update events
  58. SubscribeToEvents();
  59. // Set the mouse mode to use in the sample
  60. Sample::InitMouseMode(MM_FREE);
  61. }
  62. void PhysicsRope2D::CreateScene()
  63. {
  64. scene_ = new Scene(context_);
  65. scene_->CreateComponent<Octree>();
  66. scene_->CreateComponent<DebugRenderer>();
  67. // Create camera node
  68. cameraNode_ = scene_->CreateChild("Camera");
  69. // Set camera's position
  70. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, -10.0f));
  71. Camera* camera = cameraNode_->CreateComponent<Camera>();
  72. camera->SetOrthographic(true);
  73. Graphics* graphics = GetSubsystem<Graphics>();
  74. camera->SetOrthoSize((float)graphics->GetHeight() * 0.05f);
  75. camera->SetZoom(1.5f * Min((float)graphics->GetWidth() / 1280.0f, (float)graphics->GetHeight() / 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)
  76. // Create 2D physics world component
  77. PhysicsWorld2D* physicsWorld = scene_->CreateComponent<PhysicsWorld2D>();
  78. physicsWorld->SetDrawJoint(true);
  79. // Create ground
  80. Node* groundNode = scene_->CreateChild("Ground");
  81. // Create 2D rigid body for gound
  82. RigidBody2D* groundBody = groundNode->CreateComponent<RigidBody2D>();
  83. // Create edge collider for ground
  84. CollisionEdge2D* groundShape = groundNode->CreateComponent<CollisionEdge2D>();
  85. groundShape->SetVertices(Vector2(-40.0f, 0.0f), Vector2(40.0f, 0.0f));
  86. const float y = 15.0f;
  87. RigidBody2D* prevBody = groundBody;
  88. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  89. {
  90. Node* node = scene_->CreateChild("RigidBody");
  91. // Create rigid body
  92. RigidBody2D* body = node->CreateComponent<RigidBody2D>();
  93. body->SetBodyType(BT_DYNAMIC);
  94. // Create box
  95. CollisionBox2D* box = node->CreateComponent<CollisionBox2D>();
  96. // Set friction
  97. box->SetFriction(0.2f);
  98. // Set mask bits.
  99. box->SetMaskBits(0xFFFF & ~0x0002);
  100. if (i == NUM_OBJECTS - 1)
  101. {
  102. node->SetPosition(Vector3(1.0f * i, y, 0.0f));
  103. body->SetAngularDamping(0.4f);
  104. box->SetSize(3.0f, 3.0f);
  105. box->SetDensity(100.0f);
  106. box->SetCategoryBits(0x0002);
  107. }
  108. else
  109. {
  110. node->SetPosition(Vector3(0.5f + 1.0f * i, y, 0.0f));
  111. box->SetSize(1.0f, 0.25f);
  112. box->SetDensity(20.0f);
  113. box->SetCategoryBits(0x0001);
  114. }
  115. ConstraintRevolute2D* joint = node->CreateComponent<ConstraintRevolute2D>();
  116. joint->SetOtherBody(prevBody);
  117. joint->SetAnchor(Vector2(float(i), y));
  118. joint->SetCollideConnected(false);
  119. prevBody = body;
  120. }
  121. ConstraintRope2D* constraintRope = groundNode->CreateComponent<ConstraintRope2D>();
  122. constraintRope->SetOtherBody(prevBody);
  123. constraintRope->SetOwnerBodyAnchor(Vector2(0.0f, y));
  124. constraintRope->SetMaxLength(NUM_OBJECTS - 1.0f + 0.01f);
  125. }
  126. void PhysicsRope2D::CreateInstructions()
  127. {
  128. SimpleCreateInstructions("Use WASD keys and mouse/touch to move, Use PageUp PageDown to zoom.");
  129. }
  130. void PhysicsRope2D::SetupViewport()
  131. {
  132. Renderer* renderer = GetSubsystem<Renderer>();
  133. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  134. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  135. renderer->SetViewport(0, viewport);
  136. }
  137. void PhysicsRope2D::MoveCamera(float timeStep)
  138. {
  139. Input* input = GetSubsystem<Input>();
  140. // Movement speed as world units per second
  141. const float MOVE_SPEED = 4.0f;
  142. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  143. if (input->GetKeyDown(KEY_W))
  144. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  145. if (input->GetKeyDown(KEY_S))
  146. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  147. if (input->GetKeyDown(KEY_A))
  148. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  149. if (input->GetKeyDown(KEY_D))
  150. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  151. if (input->GetKeyDown(KEY_PAGEUP))
  152. {
  153. Camera* camera = cameraNode_->GetComponent<Camera>();
  154. camera->SetZoom(camera->GetZoom() * 1.01f);
  155. }
  156. if (input->GetKeyDown(KEY_PAGEDOWN))
  157. {
  158. Camera* camera = cameraNode_->GetComponent<Camera>();
  159. camera->SetZoom(camera->GetZoom() * 0.99f);
  160. }
  161. }
  162. void PhysicsRope2D::SubscribeToEvents()
  163. {
  164. // Subscribe HandleUpdate() function for processing update events
  165. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(PhysicsRope2D, HandleUpdate));
  166. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  167. UnsubscribeFromEvent(E_SCENEUPDATE);
  168. }
  169. void PhysicsRope2D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  170. {
  171. using namespace Update;
  172. // Take the frame time step, which is stored as a float
  173. float timeStep = eventData[P_TIMESTEP].GetFloat();
  174. // Move the camera, scale movement with time step
  175. MoveCamera(timeStep);
  176. PhysicsWorld2D* physicsWorld = scene_->GetComponent<PhysicsWorld2D>();
  177. physicsWorld->DrawDebugGeometry();
  178. }