Ragdolls.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/AnimatedModel.h>
  26. #include <Atomic/Graphics/Camera.h>
  27. #include <Atomic/Graphics/DebugRenderer.h>
  28. #include <Atomic/Graphics/Graphics.h>
  29. #include <Atomic/Graphics/Light.h>
  30. #include <Atomic/Graphics/Material.h>
  31. #include <Atomic/Graphics/Octree.h>
  32. #include <Atomic/Graphics/Renderer.h>
  33. #include <Atomic/Graphics/Zone.h>
  34. #include <Atomic/Input/Input.h>
  35. #include <Atomic/IO/File.h>
  36. #include <Atomic/IO/FileSystem.h>
  37. #include <Atomic/Physics/CollisionShape.h>
  38. #include <Atomic/Physics/PhysicsWorld.h>
  39. #include <Atomic/Physics/RigidBody.h>
  40. #include <Atomic/Resource/ResourceCache.h>
  41. #include <Atomic/Scene/Scene.h>
  42. #include <Atomic/UI/UI.h>
  43. #include <Atomic/Atomic2D/StaticSprite2D.h>
  44. #include <Atomic/Atomic2D/AnimationSet2D.h>
  45. #include <Atomic/Atomic2D/AnimatedSprite2D.h>
  46. #include <Atomic/Atomic2D/Sprite2D.h>
  47. #include <Atomic/Graphics/RenderPath.h>
  48. #include <Atomic/Resource/XMLFile.h>
  49. #include "CreateRagdoll.h"
  50. #include "Ragdolls.h"
  51. #include <Atomic/DebugNew.h>
  52. Ragdolls::Ragdolls(Context* context) :
  53. Sample(context),
  54. drawDebug_(false)
  55. {
  56. // Register an object factory for our custom CreateRagdoll component so that we can create them to scene nodes
  57. context->RegisterFactory<CreateRagdoll>();
  58. massCount = 2;
  59. speedCount = 2;
  60. sizeCount = 2;
  61. bulletMass.Push(1.0f);
  62. bulletMass.Push(10.0f);
  63. bulletMass.Push(50.0f);
  64. bulletMass.Push(100.0f);
  65. bulletMass.Push(300.0f);
  66. bulletMass.Push(666.0f);
  67. bulletMass.Push(1000.0f);
  68. bulletSpeed.Push(10.0f);
  69. bulletSpeed.Push(22.0f);
  70. bulletSpeed.Push(41.0f);
  71. bulletSpeed.Push(72.0f);
  72. bulletSpeed.Push(116.0f);
  73. bulletSpeed.Push(200.0f);
  74. bulletSpeed.Push(450.0f);
  75. bulletSize.Push(0.25f);
  76. bulletSize.Push(0.5f);
  77. bulletSize.Push(1.25f);
  78. bulletSize.Push(3.25f);
  79. bulletSize.Push(5.25f);
  80. bulletSize.Push(7.25f);
  81. bulletSize.Push(11.25f);
  82. bulletSize.Push(16.25f);
  83. bulletArc = 0.25f;
  84. }
  85. void Ragdolls::Start()
  86. {
  87. // Execute base class startup
  88. Sample::Start();
  89. // Create the scene content
  90. CreateScene();
  91. // add an overlay HUD that tells you settings
  92. CreateHUD();
  93. // Create the UI content
  94. CreateInstructions();
  95. // Setup the viewport for displaying the scene
  96. SetupViewport();
  97. // Hook up to the frame update and render post-update events
  98. SubscribeToEvents();
  99. // Set the mouse mode to use in the sample
  100. Sample::InitMouseMode(MM_ABSOLUTE);
  101. RestartJacks ();
  102. UpdateSpeedHud (speedCount);
  103. UpdateMassHud (massCount);
  104. UpdateSizeHud (sizeCount);
  105. GetSubsystem<Input>()->SetMouseVisible (false);
  106. }
  107. void Ragdolls::CreateScene()
  108. {
  109. ResourceCache* cache = GetSubsystem<ResourceCache>();
  110. scene_ = new Scene(context_);
  111. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  112. // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must
  113. // exist before creating drawable components, the PhysicsWorld must exist before creating physics components.
  114. // Finally, create a DebugRenderer component so that we can draw physics debug geometry
  115. scene_->CreateComponent<Octree>();
  116. scene_->CreateComponent<PhysicsWorld>();
  117. scene_->CreateComponent<DebugRenderer>();
  118. // Create a Zone component for ambient lighting & fog control
  119. Node* zoneNode = scene_->CreateChild("Zone");
  120. Zone* zone = zoneNode->CreateComponent<Zone>();
  121. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  122. zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
  123. zone->SetFogColor(Color(0.5f, 0.5f, 0.7f));
  124. zone->SetFogStart(100.0f);
  125. zone->SetFogEnd(300.0f);
  126. // Create a directional light to the world. Enable cascaded shadows on it
  127. Node* lightNode = scene_->CreateChild("DirectionalLight");
  128. lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f));
  129. Light* light = lightNode->CreateComponent<Light>();
  130. light->SetLightType(LIGHT_DIRECTIONAL);
  131. light->SetCastShadows(true);
  132. light->SetShadowBias(BiasParameters(0.00025f, 0.5f));
  133. // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  134. light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));
  135. // Create a floor object, 500 x 500 world units. Adjust position so that the ground is at zero Y
  136. Node* floorNode = scene_->CreateChild("Floor");
  137. floorNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f));
  138. floorNode->SetScale(Vector3(500.0f, 1.0f, 500.0f));
  139. StaticModel* floorObject = floorNode->CreateComponent<StaticModel>();
  140. floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  141. floorObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
  142. // Make the floor physical by adding RigidBody and CollisionShape components
  143. RigidBody* body = floorNode->CreateComponent<RigidBody>();
  144. // We will be spawning spherical objects in this sample. The ground also needs non-zero rolling friction so that
  145. // the spheres will eventually come to rest
  146. body->SetRollingFriction(0.15f);
  147. CollisionShape* shape = floorNode->CreateComponent<CollisionShape>();
  148. // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the
  149. // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.)
  150. shape->SetBox(Vector3::ONE);
  151. // Create the camera. Limit far clip distance to match the fog. Note: now we actually create the camera node outside
  152. // the scene, because we want it to be unaffected by scene load / save
  153. cameraNode_ = new Node(context_);
  154. Camera* camera = cameraNode_->CreateComponent<Camera>();
  155. camera->SetFarClip(300.0f);
  156. // Set an initial position for the camera scene node above the floor
  157. cameraNode_->SetPosition(Vector3(0.0f, 3.0f, -20.0f));
  158. }
  159. void Ragdolls::CreateInstructions()
  160. {
  161. SimpleCreateInstructions (
  162. "Use WASD keys and mouse/touch to move\n"
  163. "LMB to spawn physics objects\n"
  164. "F5 to save scene, F7 to load\n"
  165. "Space to toggle physics debug geometry"
  166. );
  167. }
  168. void Ragdolls::SetupViewport()
  169. {
  170. Renderer* renderer = GetSubsystem<Renderer>();
  171. ResourceCache* cache = GetSubsystem<ResourceCache>();
  172. renderer->SetNumViewports(2); // use 2 viewports, 1 for 3d and 1 for the 2d hud
  173. Viewport* viewport2_ = new Viewport(context_, hudScene, hudCamera ); // hud orthographic viewport, scene and camera
  174. RenderPath *overlayRenderPath = new RenderPath();
  175. overlayRenderPath->Load(cache->GetResource<XMLFile>("PostProcess/FrontPath.xml")); //special renderpath that does not clear
  176. viewport2_->SetRenderPath(overlayRenderPath); // apply to hud viewport, so the background is transparent
  177. renderer->SetViewport(0, new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>())); // perspective viewport, scene and camera
  178. renderer->SetViewport(1, viewport2_); // and add in the HUD viewport
  179. }
  180. void Ragdolls::MoveCamera(float timeStep)
  181. {
  182. Input* input = GetSubsystem<Input>();
  183. // Movement speed as world units per second
  184. const float MOVE_SPEED = 20.0f;
  185. // Mouse sensitivity as degrees per pixel
  186. const float MOUSE_SENSITIVITY = 0.1f;
  187. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  188. IntVector2 mouseMove = input->GetMouseMove();
  189. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  190. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  191. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  192. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  193. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  194. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  195. if (input->GetKeyDown(KEY_W))
  196. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  197. if (input->GetKeyDown(KEY_S))
  198. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  199. if (input->GetKeyDown(KEY_A))
  200. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  201. if (input->GetKeyDown(KEY_D))
  202. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  203. // "Shoot" a physics object with left mousebutton
  204. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  205. SpawnObject();
  206. // Check for loading / saving the scene
  207. if (input->GetKeyPress(KEY_F5))
  208. {
  209. File saveFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/Ragdolls.xml", FILE_WRITE);
  210. scene_->SaveXML(saveFile);
  211. }
  212. if (input->GetKeyPress(KEY_F7))
  213. {
  214. File loadFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/Ragdolls.xml", FILE_READ);
  215. scene_->LoadXML(loadFile);
  216. }
  217. // Toggle physics debug geometry with space
  218. if (input->GetKeyPress(KEY_SPACE))
  219. drawDebug_ = !drawDebug_;
  220. if (input->GetKeyPress (KEY_U)) {
  221. massCount++;
  222. if ( massCount > 5 )
  223. massCount = 0;
  224. UpdateMassHud (massCount);
  225. }
  226. if (input->GetKeyPress (KEY_I)) {
  227. speedCount++;
  228. if ( speedCount> 5 )
  229. speedCount = 0;
  230. UpdateSpeedHud (speedCount);
  231. }
  232. if (input->GetKeyPress (KEY_O)) {
  233. sizeCount++;
  234. if ( sizeCount > 5 )
  235. sizeCount = 0;
  236. UpdateSizeHud (sizeCount);
  237. }
  238. if (input->GetKeyPress (KEY_P)) {
  239. if ( bulletArc == 0.0 )
  240. bulletArc = 0.25f;
  241. else if ( bulletArc > 0.0 )
  242. bulletArc = 0.0f;
  243. }
  244. if (input->GetKeyPress (KEY_R)) {
  245. RestartJacks ();
  246. }
  247. UpdateFps ();
  248. CleanUpSome ();
  249. }
  250. void Ragdolls::SpawnObject()
  251. {
  252. ResourceCache* cache = GetSubsystem<ResourceCache>();
  253. Node* boxNode = scene_->CreateChild("Sphere");
  254. boxNode->SetPosition(cameraNode_->GetPosition());
  255. boxNode->SetRotation(cameraNode_->GetRotation());
  256. boxNode->SetScale(bulletSize[sizeCount]);
  257. StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
  258. boxObject->SetModel(cache->GetResource<Model>("Models/Sphere.mdl"));
  259. boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml"));
  260. boxObject->SetCastShadows(true);
  261. RigidBody* body = boxNode->CreateComponent<RigidBody>();
  262. body->SetMass(bulletMass[massCount]);
  263. body->SetRollingFriction(0.15f);
  264. CollisionShape* shape = boxNode->CreateComponent<CollisionShape>();
  265. shape->SetSphere(1.0f);
  266. const float OBJECT_VELOCITY = bulletSpeed[speedCount];
  267. // Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component
  268. // to overcome gravity better
  269. body->SetLinearVelocity(cameraNode_->GetRotation() * Vector3(0.0f, bulletArc, 1.0f) * OBJECT_VELOCITY);
  270. }
  271. void Ragdolls::SubscribeToEvents()
  272. {
  273. // Subscribe HandleUpdate() function for processing update events
  274. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(Ragdolls, HandleUpdate));
  275. // Subscribe HandlePostRenderUpdate() function for processing the post-render update event, during which we request
  276. // debug geometry
  277. SubscribeToEvent(E_POSTRENDERUPDATE, ATOMIC_HANDLER(Ragdolls, HandlePostRenderUpdate));
  278. }
  279. void Ragdolls::HandleUpdate(StringHash eventType, VariantMap& eventData)
  280. {
  281. using namespace Update;
  282. // Take the frame time step, which is stored as a float
  283. float timeStep = eventData[P_TIMESTEP].GetFloat();
  284. // Move the camera, scale movement with time step
  285. MoveCamera(timeStep);
  286. }
  287. void Ragdolls::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  288. {
  289. // If draw debug mode is enabled, draw physics debug geometry. Use depth test to make the result easier to interpret
  290. if (drawDebug_)
  291. scene_->GetComponent<PhysicsWorld>()->DrawDebugGeometry(true);
  292. }
  293. // support for 2D hud and new features
  294. void Ragdolls::CreateHUD()
  295. {
  296. float hscal = 0.4f;
  297. //
  298. // create a 2nd viewport and scene for a hud with sprites.
  299. //
  300. hudScene = new Scene(context_);
  301. hudScene->CreateComponent<Octree>();
  302. // Create camera node
  303. Node* hudCam = hudScene->CreateChild("HudCamera");
  304. // Set camera's position
  305. hudCam->SetPosition( Vector3(0.0f, 0.0f, -10.0f));
  306. hudCamera = hudCam->CreateComponent<Camera>();
  307. hudCamera->SetOrthographic(true);
  308. Graphics* graphics = GetSubsystem<Graphics>();
  309. hudCamera->SetOrthoSize ((float)graphics->GetHeight () * 0.01f ); //PIXEL_SIZE
  310. ResourceCache *cache = GetSubsystem<ResourceCache>();
  311. // add a crosshair in the center of the screen
  312. Sprite2D* sprite = cache->GetResource<Sprite2D>("Textures/NinjaSnowWar/Sight.png");
  313. Node* targetSprite_ = hudScene->CreateChild("targetSprite");
  314. targetSprite_->SetPosition2D(Vector2(0,0));
  315. targetSprite_->SetScale2D(Vector2(0.75f, 0.75f));
  316. StaticSprite2D* staticSprite = targetSprite_->CreateComponent<StaticSprite2D>();
  317. staticSprite->SetSprite(sprite); // Set sprite
  318. staticSprite->SetBlendMode( BLEND_ALPHA ); // Set blend mode
  319. staticSprite->SetAlpha(0.3f);
  320. // borrow the spinning coin from the 2DSprite example to show what the possibilities are
  321. float halfWidth = graphics->GetWidth() * 0.5f * 0.01;
  322. float halfHeight = graphics->GetHeight() * 0.5f * 0.01;
  323. // Get animation set
  324. AnimationSet2D* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/GoldIcon.scml");
  325. if (animationSet == NULL) return;
  326. Node* spriteNode2 = hudScene->CreateChild("AnimatedSprite2D");
  327. AnimatedSprite2D* animatedSprite = spriteNode2->CreateComponent<AnimatedSprite2D>();
  328. animatedSprite->SetAnimationSet(animationSet); // Set animation
  329. animatedSprite->SetAnimation("idle", LM_DEFAULT);
  330. spriteNode2->SetPosition2D( Vector2(halfWidth - 0.4f, halfHeight - 0.4f));
  331. // (bullet) mass, speed size feature huds
  332. filler.Push ( cache->GetResource<Sprite2D>("Textures/hudfill1.png") );
  333. filler.Push ( cache->GetResource<Sprite2D>("Textures/hudfill2.png") );
  334. filler.Push ( cache->GetResource<Sprite2D>("Textures/hudfill3.png") );
  335. filler.Push ( cache->GetResource<Sprite2D>("Textures/hudfill4.png") );
  336. filler.Push ( cache->GetResource<Sprite2D>("Textures/hudfill5.png") );
  337. filler.Push ( cache->GetResource<Sprite2D>("Textures/hudfill6.png") );
  338. Sprite2D* spritem = cache->GetResource<Sprite2D>("Textures/hudmass.png");
  339. Node* hudm = hudScene->CreateChild("hudMass");
  340. hudm->SetScale2D( Vector2(hscal,hscal));
  341. hudm->SetPosition2D( Vector2( 0 - (halfWidth/3.0), halfHeight - 0.4));
  342. StaticSprite2D* hudSpritem = hudm->CreateComponent<StaticSprite2D>();
  343. hudSpritem->SetSprite(spritem);
  344. hudSpritem->SetAlpha(0.9);
  345. hudSpritem->SetBlendMode( BLEND_ALPHA);
  346. hudSpritem->SetOrderInLayer(3);
  347. Node* hudfm = hudm->CreateChild("hudMassFill");
  348. hudfm->SetScale2D( Vector2(1,1));
  349. hudfm->SetPosition2D( Vector2( 0, 0));
  350. StaticSprite2D* hudSpritefm = hudfm->CreateComponent<StaticSprite2D>();
  351. hudSpritefm->SetSprite(filler[0]);
  352. hudSpritefm->SetAlpha(0.9);
  353. hudSpritefm->SetBlendMode( BLEND_ALPHA);
  354. hudSpritefm->SetOrderInLayer(-3);
  355. Sprite2D* sprites = cache->GetResource<Sprite2D>("Textures/hudspeed.png");
  356. Node* huds = hudScene->CreateChild("hudSpeed");
  357. huds->SetScale2D( Vector2(hscal,hscal));
  358. huds->SetPosition2D( Vector2( 0, halfHeight - 0.4));
  359. StaticSprite2D* hudSprites = huds->CreateComponent<StaticSprite2D>();
  360. hudSprites->SetSprite(sprites);
  361. hudSprites->SetAlpha(0.9);
  362. hudSprites->SetBlendMode( BLEND_ALPHA);
  363. hudSprites->SetOrderInLayer(3);
  364. Node* hudsm = huds->CreateChild("hudSpeedFill");
  365. hudsm->SetScale2D( Vector2(1,1));
  366. hudsm->SetPosition2D( Vector2( 0, 0));
  367. StaticSprite2D* hudSpritesm = hudsm->CreateComponent<StaticSprite2D>();
  368. hudSpritesm->SetSprite(filler[0]);
  369. hudSpritesm->SetAlpha(0.9);
  370. hudSpritesm->SetBlendMode( BLEND_ALPHA);
  371. hudSpritesm->SetOrderInLayer(-3);
  372. Sprite2D* spritez = cache->GetResource<Sprite2D>("Textures/hudsize.png");
  373. Node* hudz = hudScene->CreateChild("hudSize");
  374. hudz->SetScale2D( Vector2(hscal,hscal));
  375. hudz->SetPosition2D( Vector2( 0 + (halfWidth/3.0), halfHeight - 0.4));
  376. StaticSprite2D* hudSpritez = hudz->CreateComponent<StaticSprite2D>();
  377. hudSpritez->SetSprite(spritez);
  378. hudSpritez->SetAlpha(0.9);
  379. hudSpritez->SetBlendMode( BLEND_ALPHA);
  380. hudSpritez->SetOrderInLayer(3);
  381. Node* hudzm = hudz->CreateChild("hudSizeFill");
  382. hudzm->SetScale2D( Vector2(1,1));
  383. hudzm->SetPosition2D( Vector2( 0, 0));
  384. StaticSprite2D* hudSpritezm = hudzm->CreateComponent<StaticSprite2D>();
  385. hudSpritezm->SetSprite(filler[0]);
  386. hudSpritezm->SetAlpha(0.9);
  387. hudSpritezm->SetBlendMode( BLEND_ALPHA);
  388. hudSpritezm->SetOrderInLayer(-3);
  389. }
  390. void Ragdolls::RestartJacks()
  391. {
  392. PODVector< Node * >allnodes;
  393. scene_->GetChildren (allnodes, true);
  394. for ( int ii=0; ii<allnodes.Size() ; ii++ )
  395. {
  396. if ( allnodes[ii]->GetName().Compare ("Sphere") == 0) allnodes[ii]->Remove ();
  397. else if ( allnodes[ii]->GetName().Compare ("Stuffing") == 0 ) allnodes[ii]->Remove ();
  398. else if ( allnodes[ii]->GetName().Compare ("Jack") == 0 ) allnodes[ii]->Remove ();
  399. }
  400. ResourceCache *cache = GetSubsystem<ResourceCache>();
  401. // Create animated models, you dont know ... jack
  402. for (int z = -1; z <= 1; ++z)
  403. {
  404. for (int x = -4; x <= 4; ++x)
  405. {
  406. Node* modelNode = scene_->CreateChild("Jack");
  407. modelNode->SetPosition(Vector3(x * 5.0f, 0.0f, z * 5.0f));
  408. modelNode->SetRotation(Quaternion(0.0f, 180.0f, 0.0f));
  409. AnimatedModel* modelObject = modelNode->CreateComponent<AnimatedModel>();
  410. modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl"));
  411. modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml"));
  412. modelObject->SetCastShadows(true);
  413. // Set the model to also update when invisible to avoid staying invisible when the model should come into
  414. // view, but does not as the bounding box is not updated
  415. modelObject->SetUpdateInvisible(true);
  416. // Create a rigid body and a collision shape. These will act as a trigger for transforming the
  417. // model into a ragdoll when hit by a moving object
  418. RigidBody* body = modelNode->CreateComponent<RigidBody>();
  419. // The Trigger mode makes the rigid body only detect collisions, but impart no forces on the
  420. // colliding objects
  421. body->SetTrigger(true);
  422. CollisionShape* shape = modelNode->CreateComponent<CollisionShape>();
  423. // Create the capsule shape with an offset so that it is correctly aligned with the model, which
  424. // has its origin at the feet
  425. shape->SetCapsule(0.7f, 2.0f, Vector3(0.0f, 1.0f, 0.0f));
  426. // Create a custom component that reacts to collisions and creates the ragdoll
  427. modelNode->CreateComponent<CreateRagdoll>();
  428. }
  429. }
  430. }
  431. void Ragdolls::CleanUpSome()
  432. {
  433. Node* cam = cameraNode_; // note - the camera isnt in the scene
  434. if ( cam == NULL )
  435. return;
  436. PODVector< Node * >allnodes;
  437. scene_->GetChildren (allnodes, true);
  438. for ( int ii=0; ii<allnodes.Size() ; ii++ )
  439. {
  440. if ( allnodes[ii]->GetName().Compare ("Sphere") == 0 )
  441. {
  442. if (( allnodes[ii]->GetWorldPosition() - cam->GetWorldPosition() ).Length() > 270.0f)
  443. {
  444. allnodes[ii]->Remove ();
  445. }
  446. }
  447. }
  448. }
  449. void Ragdolls::UpdateFps ()
  450. {
  451. Engine* eng = GetSubsystem<Engine>();
  452. String mystr = "FPS: " + String(eng->GetFps());
  453. mystr += "\nUse WASD keys and mouse/touch to move\n";
  454. mystr += "LMB to spawn physics objects\n";
  455. mystr += "U=Mass, I=Speed, O=Size, R=Restart\n";
  456. mystr += "Space to toggle physics debug geometry";
  457. SetInstructions ( mystr );
  458. }
  459. void Ragdolls::UpdateMassHud ( int value )
  460. {
  461. Node* xNode = hudScene->GetChild("hudMass", true);
  462. if (xNode) {
  463. Node* fillx = xNode->GetChild("hudMassFill");
  464. if (fillx) {
  465. StaticSprite2D* hudSprite = fillx->GetComponent<StaticSprite2D>();
  466. hudSprite->SetSprite(filler[value]);
  467. }
  468. }
  469. }
  470. void Ragdolls::UpdateSpeedHud ( int value )
  471. {
  472. Node* xNode = hudScene->GetChild("hudSpeed", true);
  473. if (xNode) {
  474. Node* fillx = xNode->GetChild("hudSpeedFill");
  475. if (fillx) {
  476. StaticSprite2D* hudSprite = fillx->GetComponent<StaticSprite2D>();
  477. hudSprite->SetSprite(filler[value]);
  478. }
  479. }
  480. }
  481. void Ragdolls::UpdateSizeHud ( int value )
  482. {
  483. Node* xNode = hudScene->GetChild("hudSize", true);
  484. if (xNode) {
  485. Node* fillx = xNode->GetChild("hudSizeFill");
  486. if (fillx) {
  487. StaticSprite2D* hudSprite = fillx->GetComponent<StaticSprite2D>();
  488. hudSprite->SetSprite(filler[value]);
  489. }
  490. }
  491. }