Urho2DPhysicsRope.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Engine/Engine.h>
  5. #include <Urho3D/Graphics/Camera.h>
  6. #include <Urho3D/Graphics/DebugRenderer.h>
  7. #include <Urho3D/Graphics/Graphics.h>
  8. #include <Urho3D/Graphics/Octree.h>
  9. #include <Urho3D/Graphics/Renderer.h>
  10. #include <Urho3D/Input/Input.h>
  11. #include <Urho3D/Physics2D/CollisionBox2D.h>
  12. #include <Urho3D/Physics2D/CollisionEdge2D.h>
  13. #include <Urho3D/Physics2D/ConstraintRevolute2D.h>
  14. #include <Urho3D/Physics2D/PhysicsWorld2D.h>
  15. #include <Urho3D/Physics2D/RigidBody2D.h>
  16. #include <Urho3D/Scene/Scene.h>
  17. #include <Urho3D/Scene/SceneEvents.h>
  18. #include <Urho3D/UI/Font.h>
  19. #include <Urho3D/UI/Text.h>
  20. #include "Urho2DPhysicsRope.h"
  21. #include <Urho3D/DebugNew.h>
  22. URHO3D_DEFINE_APPLICATION_MAIN(Urho2DPhysicsRope)
  23. static const unsigned NUM_OBJECTS = 10;
  24. Urho2DPhysicsRope::Urho2DPhysicsRope(Context* context) :
  25. Sample(context)
  26. {
  27. }
  28. void Urho2DPhysicsRope::Start()
  29. {
  30. // Execute base class startup
  31. Sample::Start();
  32. // Create the scene content
  33. CreateScene();
  34. // Create the UI content
  35. CreateInstructions();
  36. // Setup the viewport for displaying the scene
  37. SetupViewport();
  38. // Hook up to the frame update events
  39. SubscribeToEvents();
  40. // Set the mouse mode to use in the sample
  41. Sample::InitMouseMode(MM_FREE);
  42. }
  43. void Urho2DPhysicsRope::CreateScene()
  44. {
  45. scene_ = new Scene(context_);
  46. scene_->CreateComponent<Octree>();
  47. scene_->CreateComponent<DebugRenderer>();
  48. // Create camera node
  49. cameraNode_ = scene_->CreateChild("Camera");
  50. // Set camera's position
  51. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, -10.0f));
  52. auto* camera = cameraNode_->CreateComponent<Camera>();
  53. camera->SetOrthographic(true);
  54. auto* graphics = GetSubsystem<Graphics>();
  55. camera->SetOrthoSize((float)graphics->GetHeight() * 0.05f);
  56. 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)
  57. // Create 2D physics world component
  58. auto* physicsWorld = scene_->CreateComponent<PhysicsWorld2D>();
  59. physicsWorld->SetDrawJoint(true);
  60. // Create ground
  61. Node* groundNode = scene_->CreateChild("Ground");
  62. // Create 2D rigid body for gound
  63. auto* groundBody = groundNode->CreateComponent<RigidBody2D>();
  64. // Create edge collider for ground
  65. auto* groundShape = groundNode->CreateComponent<CollisionEdge2D>();
  66. groundShape->SetVertices(Vector2(-40.0f, 0.0f), Vector2(40.0f, 0.0f));
  67. const float y = 15.0f;
  68. RigidBody2D* prevBody = groundBody;
  69. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  70. {
  71. Node* node = scene_->CreateChild("RigidBody");
  72. // Create rigid body
  73. auto* body = node->CreateComponent<RigidBody2D>();
  74. body->SetBodyType(BT_DYNAMIC);
  75. // Create box
  76. auto* box = node->CreateComponent<CollisionBox2D>();
  77. // Set friction
  78. box->SetFriction(0.2f);
  79. // Set mask bits.
  80. box->SetMaskBits(0xFFFFu & ~0x0002u);
  81. if (i == NUM_OBJECTS - 1)
  82. {
  83. node->SetPosition(Vector3(1.0f * i, y, 0.0f));
  84. body->SetAngularDamping(0.4f);
  85. box->SetSize(3.0f, 3.0f);
  86. box->SetDensity(100.0f);
  87. box->SetCategoryBits(0x0002);
  88. }
  89. else
  90. {
  91. node->SetPosition(Vector3(0.5f + 1.0f * i, y, 0.0f));
  92. box->SetSize(1.0f, 0.25f);
  93. box->SetDensity(20.0f);
  94. box->SetCategoryBits(0x0001);
  95. }
  96. auto* joint = node->CreateComponent<ConstraintRevolute2D>();
  97. joint->SetOtherBody(prevBody);
  98. joint->SetAnchor(Vector2(float(i), y));
  99. joint->SetCollideConnected(false);
  100. prevBody = body;
  101. }
  102. }
  103. void Urho2DPhysicsRope::CreateInstructions()
  104. {
  105. auto* cache = GetSubsystem<ResourceCache>();
  106. auto* ui = GetSubsystem<UI>();
  107. // Construct new Text object, set string to display and font to use
  108. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  109. instructionText->SetText("Use WASD keys and mouse/touch to move, Use PageUp PageDown to zoom.");
  110. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  111. // Position the text relative to the screen center
  112. instructionText->SetHorizontalAlignment(HA_CENTER);
  113. instructionText->SetVerticalAlignment(VA_CENTER);
  114. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  115. }
  116. void Urho2DPhysicsRope::SetupViewport()
  117. {
  118. auto* renderer = GetSubsystem<Renderer>();
  119. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  120. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  121. renderer->SetViewport(0, viewport);
  122. }
  123. void Urho2DPhysicsRope::MoveCamera(float timeStep)
  124. {
  125. // Do not move if the UI has a focused element (the console)
  126. if (GetSubsystem<UI>()->GetFocusElement())
  127. return;
  128. auto* input = GetSubsystem<Input>();
  129. // Movement speed as world units per second
  130. const float MOVE_SPEED = 4.0f;
  131. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  132. if (input->GetKeyDown(KEY_W))
  133. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  134. if (input->GetKeyDown(KEY_S))
  135. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  136. if (input->GetKeyDown(KEY_A))
  137. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  138. if (input->GetKeyDown(KEY_D))
  139. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  140. if (input->GetKeyDown(KEY_PAGEUP))
  141. {
  142. auto* camera = cameraNode_->GetComponent<Camera>();
  143. camera->SetZoom(camera->GetZoom() * 1.01f);
  144. }
  145. if (input->GetKeyDown(KEY_PAGEDOWN))
  146. {
  147. auto* camera = cameraNode_->GetComponent<Camera>();
  148. camera->SetZoom(camera->GetZoom() * 0.99f);
  149. }
  150. }
  151. void Urho2DPhysicsRope::SubscribeToEvents()
  152. {
  153. // Subscribe HandleUpdate() function for processing update events
  154. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Urho2DPhysicsRope, HandleUpdate));
  155. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  156. UnsubscribeFromEvent(E_SCENEUPDATE);
  157. }
  158. void Urho2DPhysicsRope::HandleUpdate(StringHash eventType, VariantMap& eventData)
  159. {
  160. using namespace Update;
  161. // Take the frame time step, which is stored as a float
  162. float timeStep = eventData[P_TIMESTEP].GetFloat();
  163. // Move the camera, scale movement with time step
  164. MoveCamera(timeStep);
  165. auto* physicsWorld = scene_->GetComponent<PhysicsWorld2D>();
  166. physicsWorld->DrawDebugGeometry();
  167. }