Urho2DPhysicsRope.cpp 7.5 KB

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