Physics2D.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/Resource/ResourceCache.h>
  32. #include <Atomic/Scene/Scene.h>
  33. #include <Atomic/Scene/SceneEvents.h>
  34. #include <Atomic/Atomic2D/CollisionBox2D.h>
  35. #include <Atomic/Atomic2D/CollisionCircle2D.h>
  36. #include <Atomic/Atomic2D/Drawable2D.h>
  37. #include <Atomic/Atomic2D/PhysicsWorld2D.h>
  38. #include <Atomic/Atomic2D/RigidBody2D.h>
  39. #include <Atomic/Atomic2D/Sprite2D.h>
  40. #include <Atomic/Atomic2D/StaticSprite2D.h>
  41. #include "Physics2D.h"
  42. #include <Atomic/DebugNew.h>
  43. static const unsigned NUM_OBJECTS = 100;
  44. Physics2D::Physics2D(Context* context) :
  45. Sample(context)
  46. {
  47. }
  48. void Physics2D::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. // Set the mouse mode to use in the sample
  61. Sample::InitMouseMode(MM_FREE);
  62. }
  63. void Physics2D::CreateScene()
  64. {
  65. scene_ = new Scene(context_);
  66. scene_->CreateComponent<Octree>();
  67. scene_->CreateComponent<DebugRenderer>();
  68. // Create camera node
  69. cameraNode_ = scene_->CreateChild("Camera");
  70. // Set camera's position
  71. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  72. Camera* camera = cameraNode_->CreateComponent<Camera>();
  73. camera->SetOrthographic(true);
  74. Graphics* graphics = GetSubsystem<Graphics>();
  75. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  76. camera->SetZoom(1.2f * 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.2) is set for full visibility at 1280x800 resolution)
  77. // Create 2D physics world component
  78. /*PhysicsWorld2D* physicsWorld = */scene_->CreateComponent<PhysicsWorld2D>();
  79. ResourceCache* cache = GetSubsystem<ResourceCache>();
  80. Sprite2D* boxSprite = cache->GetResource<Sprite2D>("Urho2D/Box.png");
  81. Sprite2D* ballSprite = cache->GetResource<Sprite2D>("Urho2D/Ball.png");
  82. // Create ground.
  83. Node* groundNode = scene_->CreateChild("Ground");
  84. groundNode->SetPosition(Vector3(0.0f, -3.0f, 0.0f));
  85. groundNode->SetScale(Vector3(200.0f, 1.0f, 0.0f));
  86. // Create 2D rigid body for gound
  87. /*RigidBody2D* groundBody = */groundNode->CreateComponent<RigidBody2D>();
  88. StaticSprite2D* groundSprite = groundNode->CreateComponent<StaticSprite2D>();
  89. groundSprite->SetSprite(boxSprite);
  90. // Create box collider for ground
  91. CollisionBox2D* groundShape = groundNode->CreateComponent<CollisionBox2D>();
  92. // Set box size
  93. groundShape->SetSize(Vector2(0.32f, 0.32f));
  94. // Set friction
  95. groundShape->SetFriction(0.5f);
  96. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  97. {
  98. Node* node = scene_->CreateChild("RigidBody");
  99. node->SetPosition(Vector3(Random(-0.1f, 0.1f), 5.0f + i * 0.4f, 0.0f));
  100. // Create rigid body
  101. RigidBody2D* body = node->CreateComponent<RigidBody2D>();
  102. body->SetBodyType(BT_DYNAMIC);
  103. StaticSprite2D* staticSprite = node->CreateComponent<StaticSprite2D>();
  104. if (i % 2 == 0)
  105. {
  106. staticSprite->SetSprite(boxSprite);
  107. // Create box
  108. CollisionBox2D* box = node->CreateComponent<CollisionBox2D>();
  109. // Set size
  110. box->SetSize(Vector2(0.32f, 0.32f));
  111. // Set density
  112. box->SetDensity(1.0f);
  113. // Set friction
  114. box->SetFriction(0.5f);
  115. // Set restitution
  116. box->SetRestitution(0.1f);
  117. }
  118. else
  119. {
  120. staticSprite->SetSprite(ballSprite);
  121. // Create circle
  122. CollisionCircle2D* circle = node->CreateComponent<CollisionCircle2D>();
  123. // Set radius
  124. circle->SetRadius(0.16f);
  125. // Set density
  126. circle->SetDensity(1.0f);
  127. // Set friction.
  128. circle->SetFriction(0.5f);
  129. // Set restitution
  130. circle->SetRestitution(0.1f);
  131. }
  132. }
  133. }
  134. void Physics2D::CreateInstructions()
  135. {
  136. SimpleCreateInstructions("Use WASD keys to move, use PageUp PageDown keys to zoom.");
  137. }
  138. void Physics2D::SetupViewport()
  139. {
  140. Renderer* renderer = GetSubsystem<Renderer>();
  141. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  142. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  143. renderer->SetViewport(0, viewport);
  144. }
  145. void Physics2D::MoveCamera(float timeStep)
  146. {
  147. Input* input = GetSubsystem<Input>();
  148. // Movement speed as world units per second
  149. const float MOVE_SPEED = 4.0f;
  150. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  151. if (input->GetKeyDown(KEY_W))
  152. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  153. if (input->GetKeyDown(KEY_S))
  154. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  155. if (input->GetKeyDown(KEY_A))
  156. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  157. if (input->GetKeyDown(KEY_D))
  158. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  159. if (input->GetKeyDown(KEY_PAGEUP))
  160. {
  161. Camera* camera = cameraNode_->GetComponent<Camera>();
  162. camera->SetZoom(camera->GetZoom() * 1.01f);
  163. }
  164. if (input->GetKeyDown(KEY_PAGEDOWN))
  165. {
  166. Camera* camera = cameraNode_->GetComponent<Camera>();
  167. camera->SetZoom(camera->GetZoom() * 0.99f);
  168. }
  169. }
  170. void Physics2D::SubscribeToEvents()
  171. {
  172. // Subscribe HandleUpdate() function for processing update events
  173. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(Physics2D, HandleUpdate));
  174. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  175. UnsubscribeFromEvent(E_SCENEUPDATE);
  176. }
  177. void Physics2D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  178. {
  179. using namespace Update;
  180. // Take the frame time step, which is stored as a float
  181. float timeStep = eventData[P_TIMESTEP].GetFloat();
  182. // Move the camera, scale movement with time step
  183. MoveCamera(timeStep);
  184. }