CharacterBaseTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Character/CharacterBaseTest.h>
  5. #include <Jolt/Physics/PhysicsScene.h>
  6. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  7. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  8. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  9. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  10. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  11. #include <Jolt/Physics/Constraints/HingeConstraint.h>
  12. #include <Jolt/Core/StringTools.h>
  13. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  14. #include <Application/DebugUI.h>
  15. #include <Layers.h>
  16. #include <Utils/Log.h>
  17. #include <Renderer/DebugRendererImp.h>
  18. JPH_IMPLEMENT_RTTI_ABSTRACT(CharacterBaseTest)
  19. {
  20. JPH_ADD_BASE_CLASS(CharacterBaseTest, Test)
  21. }
  22. const char *CharacterBaseTest::sScenes[] =
  23. {
  24. "PerlinMesh",
  25. "PerlinHeightField",
  26. "ObstacleCourse",
  27. "Terrain1",
  28. "Terrain2",
  29. };
  30. const char *CharacterBaseTest::sSceneName = "ObstacleCourse";
  31. // Scene constants
  32. static const Vec3 cRotatingPosition(-5, 0.15f, 15);
  33. static const Quat cRotatingOrientation = Quat::sIdentity();
  34. static const Vec3 cVerticallyMovingPosition(0, 2.0f, 15);
  35. static const Quat cVerticallyMovingOrientation = Quat::sIdentity();
  36. static const Vec3 cHorizontallyMovingPosition(5, 1, 15);
  37. static const Quat cHorizontallyMovingOrientation = Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI);
  38. static const Vec3 cRampPosition(15, 2.2f, 15);
  39. static const Quat cRampOrientation = Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI);
  40. static const Vec3 cRampBlocksStart = cRampPosition + Vec3(-3.0f, 3.0f, 1.5f);
  41. static const Vec3 cRampBlocksDelta = Vec3(2.0f, 0, 0);
  42. static const float cRampBlocksTime = 5.0f;
  43. static const Vec3 cSmallBumpsPosition = Vec3(-5.0f, 0, 2.5f);
  44. static const float cSmallBumpHeight = 0.05f;
  45. static const float cSmallBumpWidth = 0.01f;
  46. static const float cSmallBumpDelta = 0.5f;
  47. static const Vec3 cLargeBumpsPosition = Vec3(-10.0f, 0, 2.5f);
  48. static const float cLargeBumpHeight = 0.3f;
  49. static const float cLargeBumpWidth = 0.1f;
  50. static const float cLargeBumpDelta = 2.0f;
  51. static const Vec3 cStairsPosition = Vec3(-15.0f, 0, 2.5f);
  52. static const float cStairsStepHeight = 0.3f;
  53. static const Vec3 cNoStairsPosition = Vec3(-15.0f, 0, 10.0f);
  54. static const float cNoStairsStepHeight = 0.3f;
  55. static const float cNoStairsStepDelta = 0.05f;
  56. static const Vec3 cMeshWallPosition = Vec3(-20.0f, 0, -27.0f);
  57. static const float cMeshWallHeight = 3.0f;
  58. static const float cMeshWallWidth = 2.0f;
  59. static const float cMeshWallStepStart = 0.5f;
  60. static const float cMeshWallStepEnd = 4.0f;
  61. static const int cMeshWallSegments = 25;
  62. void CharacterBaseTest::Initialize()
  63. {
  64. if (strcmp(sSceneName, "PerlinMesh") == 0)
  65. {
  66. // Default terrain
  67. CreateMeshTerrain();
  68. }
  69. else if (strcmp(sSceneName, "PerlinHeightField") == 0)
  70. {
  71. // Default terrain
  72. CreateHeightFieldTerrain();
  73. }
  74. else if (strcmp(sSceneName, "ObstacleCourse") == 0)
  75. {
  76. // Default terrain
  77. CreateFloor(350.0f);
  78. {
  79. // Create ramps with different inclinations
  80. Ref<Shape> ramp = RotatedTranslatedShapeSettings(Vec3(0, 0, -2.5f), Quat::sIdentity(), new BoxShape(Vec3(1.0f, 0.05f, 2.5f))).Create().Get();
  81. for (int angle = 0; angle < 18; ++angle)
  82. mBodyInterface->CreateAndAddBody(BodyCreationSettings(ramp, Vec3(-15.0f + angle * 2.0f, 0, -10.0f), Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(10.0f * angle)), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  83. }
  84. {
  85. // Create wall consisting of vertical pillars
  86. // Note: Convex radius 0 because otherwise it will be a bumpy wall
  87. Ref<Shape> wall = new BoxShape(Vec3(0.1f, 2.5f, 0.1f), 0.0f);
  88. for (int z = 0; z < 30; ++z)
  89. mBodyInterface->CreateAndAddBody(BodyCreationSettings(wall, Vec3(0.0f, 2.5f, 2.0f + 0.2f * z), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  90. }
  91. {
  92. // Kinematic blocks to test interacting with moving objects
  93. Ref<Shape> kinematic = new BoxShape(Vec3(1, 0.15f, 3.0f));
  94. mRotatingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cRotatingPosition, cRotatingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  95. mVerticallyMovingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cVerticallyMovingPosition, cVerticallyMovingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  96. mHorizontallyMovingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cHorizontallyMovingPosition, cHorizontallyMovingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  97. }
  98. {
  99. // A rolling sphere towards the player
  100. BodyCreationSettings bcs(new SphereShape(0.2f), Vec3(0.0f, 0.2f, -1.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  101. bcs.mLinearVelocity = Vec3(0, 0, 2.0f);
  102. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  103. bcs.mMassPropertiesOverride.mMass = 10.0f;
  104. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  105. }
  106. {
  107. // Dynamic blocks to test player pushing blocks
  108. Ref<Shape> block = new BoxShape(Vec3::sReplicate(0.5f));
  109. for (int y = 0; y < 3; ++y)
  110. {
  111. BodyCreationSettings bcs(block, Vec3(5.0f, 0.5f + float(y), 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  112. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  113. bcs.mMassPropertiesOverride.mMass = 10.0f;
  114. mBodyInterface->CreateAndAddBody(bcs, EActivation::DontActivate);
  115. }
  116. }
  117. {
  118. // Dynamic block on a static step (to test pushing block on stairs)
  119. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(0.5f, 0.15f, 0.5f)), Vec3(10.0f, 0.15f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  120. BodyCreationSettings bcs(new BoxShape(Vec3::sReplicate(0.5f)), Vec3(10.0f, 0.8f, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  121. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  122. bcs.mMassPropertiesOverride.mMass = 10.0f;
  123. mBodyInterface->CreateAndAddBody(bcs, EActivation::DontActivate);
  124. }
  125. {
  126. // Dynamic spheres to test player pushing stuff you can step on
  127. float h = 0.0f;
  128. for (int y = 0; y < 3; ++y)
  129. {
  130. float r = 0.4f - 0.1f * y;
  131. h += r;
  132. BodyCreationSettings bcs(new SphereShape(r), Vec3(15.0f, h, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  133. h += r;
  134. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  135. bcs.mMassPropertiesOverride.mMass = 10.0f;
  136. mBodyInterface->CreateAndAddBody(bcs, EActivation::DontActivate);
  137. }
  138. }
  139. {
  140. // A seesaw to test character gravity
  141. BodyID b1 = mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(1.0f, 0.2f, 0.05f)), Vec3(20.0f, 0.2f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  142. BodyCreationSettings bcs(new BoxShape(Vec3(1.0f, 0.05f, 5.0f)), Vec3(20.0f, 0.45f, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  143. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  144. bcs.mMassPropertiesOverride.mMass = 10.0f;
  145. BodyID b2 = mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  146. // Connect the parts with a hinge
  147. HingeConstraintSettings hinge;
  148. hinge.mPoint1 = hinge.mPoint2 = Vec3(20.0f, 0.4f, 0.0f);
  149. hinge.mHingeAxis1 = hinge.mHingeAxis2 = Vec3::sAxisX();
  150. mPhysicsSystem->AddConstraint(mBodyInterface->CreateConstraint(&hinge, b1, b2));
  151. }
  152. {
  153. // A board above the character to crouch and jump up against
  154. float h = 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching + 0.1f;
  155. for (int x = 0; x < 2; ++x)
  156. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(1.0f, h, 0.05f)), Vec3(25.0f, h, x == 0? -0.95f : 0.95f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  157. BodyCreationSettings bcs(new BoxShape(Vec3(1.0f, 0.05f, 1.0f)), Vec3(25.0f, 2.0f * h + 0.05f, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  158. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  159. bcs.mMassPropertiesOverride.mMass = 10.0f;
  160. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  161. }
  162. {
  163. // A floating static block
  164. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(0.5f)), Vec3(30.0f, 1.5f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  165. }
  166. {
  167. // Create ramp
  168. BodyCreationSettings ramp(new BoxShape(Vec3(4.0f, 0.1f, 3.0f)), cRampPosition, cRampOrientation, EMotionType::Static, Layers::NON_MOVING);
  169. mBodyInterface->CreateAndAddBody(ramp, EActivation::DontActivate);
  170. // Create blocks on ramp
  171. Ref<Shape> block = new BoxShape(Vec3::sReplicate(0.5f));
  172. BodyCreationSettings bcs(block, cRampBlocksStart, cRampOrientation, EMotionType::Dynamic, Layers::MOVING);
  173. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  174. bcs.mMassPropertiesOverride.mMass = 10.0f;
  175. for (int i = 0; i < 4; ++i)
  176. {
  177. mRampBlocks.emplace_back(mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate));
  178. bcs.mPosition += cRampBlocksDelta;
  179. }
  180. }
  181. // Create three funnels with walls that are too steep to climb
  182. Ref<Shape> funnel = new BoxShape(Vec3(0.1f, 1.0f, 1.0f));
  183. for (int i = 0; i < 2; ++i)
  184. {
  185. Quat rotation = Quat::sRotation(Vec3::sAxisY(), JPH_PI * i);
  186. mBodyInterface->CreateAndAddBody(BodyCreationSettings(funnel, Vec3(5.0f, 0.1f, 5.0f) + rotation * Vec3(0.2f, 0, 0), rotation * Quat::sRotation(Vec3::sAxisZ(), -DegreesToRadians(40.0f)), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  187. }
  188. for (int i = 0; i < 3; ++i)
  189. {
  190. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 2.0f / 3.0f * JPH_PI * i);
  191. mBodyInterface->CreateAndAddBody(BodyCreationSettings(funnel, Vec3(7.5f, 0.1f, 5.0f) + rotation * Vec3(0.2f, 0, 0), rotation * Quat::sRotation(Vec3::sAxisZ(), -DegreesToRadians(40.0f)), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  192. }
  193. for (int i = 0; i < 4; ++i)
  194. {
  195. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI * i);
  196. mBodyInterface->CreateAndAddBody(BodyCreationSettings(funnel, Vec3(10.0f, 0.1f, 5.0f) + rotation * Vec3(0.2f, 0, 0), rotation * Quat::sRotation(Vec3::sAxisZ(), -DegreesToRadians(40.0f)), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  197. }
  198. // Create small bumps
  199. {
  200. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cSmallBumpHeight, 0.5f * cSmallBumpWidth), 0.0f), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  201. for (int i = 0; i < 10; ++i)
  202. {
  203. step.mPosition = cSmallBumpsPosition + Vec3(0, 0.5f * cSmallBumpHeight, cSmallBumpDelta * i);
  204. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  205. }
  206. }
  207. // Create large bumps
  208. {
  209. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cLargeBumpHeight, 0.5f * cLargeBumpWidth)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  210. for (int i = 0; i < 5; ++i)
  211. {
  212. step.mPosition = cLargeBumpsPosition + Vec3(0, 0.5f * cLargeBumpHeight, cLargeBumpDelta * i);
  213. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  214. }
  215. }
  216. // Create stairs
  217. {
  218. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cStairsStepHeight, 0.5f * cStairsStepHeight)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  219. for (int i = 0; i < 10; ++i)
  220. {
  221. step.mPosition = cStairsPosition + Vec3(0, cStairsStepHeight * (0.5f + i), cStairsStepHeight * i);
  222. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  223. }
  224. }
  225. // Create stairs with too little space between the steps
  226. {
  227. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cNoStairsStepHeight, 0.5f * cNoStairsStepHeight)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  228. for (int i = 0; i < 10; ++i)
  229. {
  230. step.mPosition = cNoStairsPosition + Vec3(0, cNoStairsStepHeight * (0.5f + i), cNoStairsStepDelta * i);
  231. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  232. }
  233. }
  234. // Create mesh with walls at varying angles
  235. {
  236. TriangleList triangles;
  237. Vec3 p1(0.5f * cMeshWallWidth, 0, 0);
  238. Vec3 h(0, cMeshWallHeight, 0);
  239. for (int i = 0; i < cMeshWallSegments; ++i)
  240. {
  241. float delta = cMeshWallStepStart + i * (cMeshWallStepEnd - cMeshWallStepStart) / (cMeshWallSegments - 1);
  242. Vec3 p2 = Vec3((i & 1)? 0.5f * cMeshWallWidth : -0.5f * cMeshWallWidth, 0, p1.GetZ() + delta);
  243. triangles.push_back(Triangle(p1, p1 + h, p2 + h));
  244. triangles.push_back(Triangle(p1, p2 + h, p2));
  245. p1 = p2;
  246. }
  247. MeshShapeSettings mesh(triangles);
  248. mesh.SetEmbedded();
  249. BodyCreationSettings wall(&mesh, cMeshWallPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  250. mBodyInterface->CreateAndAddBody(wall, EActivation::DontActivate);
  251. }
  252. }
  253. else
  254. {
  255. // Load scene
  256. Ref<PhysicsScene> scene;
  257. if (!ObjectStreamIn::sReadObject((String("Assets/") + sSceneName + ".bof").c_str(), scene))
  258. FatalError("Failed to load scene");
  259. scene->FixInvalidScales();
  260. for (BodyCreationSettings &settings : scene->GetBodies())
  261. {
  262. settings.mObjectLayer = Layers::NON_MOVING;
  263. settings.mFriction = 0.5f;
  264. }
  265. scene->CreateBodies(mPhysicsSystem);
  266. }
  267. // Create capsule shapes for all stances
  268. mStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cCharacterHeightStanding, cCharacterRadiusStanding)).Create().Get();
  269. mCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cCharacterHeightCrouching, cCharacterRadiusCrouching)).Create().Get();
  270. }
  271. void CharacterBaseTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  272. {
  273. // Update scene time
  274. mTime += inParams.mDeltaTime;
  275. // Determine controller input
  276. Vec3 control_input = Vec3::sZero();
  277. if (inParams.mKeyboard->IsKeyPressed(DIK_LEFT)) control_input.SetZ(-1);
  278. if (inParams.mKeyboard->IsKeyPressed(DIK_RIGHT)) control_input.SetZ(1);
  279. if (inParams.mKeyboard->IsKeyPressed(DIK_UP)) control_input.SetX(1);
  280. if (inParams.mKeyboard->IsKeyPressed(DIK_DOWN)) control_input.SetX(-1);
  281. if (control_input != Vec3::sZero())
  282. control_input = control_input.Normalized();
  283. // Rotate controls to align with the camera
  284. Vec3 cam_fwd = inParams.mCameraState.mForward;
  285. cam_fwd.SetY(0.0f);
  286. cam_fwd = cam_fwd.NormalizedOr(Vec3::sAxisX());
  287. Quat rotation = Quat::sFromTo(Vec3::sAxisX(), cam_fwd);
  288. control_input = rotation * control_input;
  289. // Check actions
  290. bool jump = false;
  291. bool switch_stance = false;
  292. for (int key = inParams.mKeyboard->GetFirstKey(); key != 0; key = inParams.mKeyboard->GetNextKey())
  293. {
  294. if (key == DIK_RSHIFT)
  295. switch_stance = true;
  296. else if (key == DIK_RCONTROL)
  297. jump = true;
  298. }
  299. HandleInput(control_input, jump, switch_stance, inParams.mDeltaTime);
  300. // Animate bodies
  301. if (!mRotatingBody.IsInvalid())
  302. mBodyInterface->MoveKinematic(mRotatingBody, cRotatingPosition, Quat::sRotation(Vec3::sAxisY(), JPH_PI * Sin(mTime)), inParams.mDeltaTime);
  303. if (!mHorizontallyMovingBody.IsInvalid())
  304. mBodyInterface->MoveKinematic(mHorizontallyMovingBody, cHorizontallyMovingPosition + Vec3(3.0f * Sin(mTime), 0, 0), cHorizontallyMovingOrientation, inParams.mDeltaTime);
  305. if (!mVerticallyMovingBody.IsInvalid())
  306. mBodyInterface->MoveKinematic(mVerticallyMovingBody, cVerticallyMovingPosition + Vec3(0, 1.75f * Sin(mTime), 0), cVerticallyMovingOrientation, inParams.mDeltaTime);
  307. // Reset ramp blocks
  308. mRampBlocksTimeLeft -= inParams.mDeltaTime;
  309. if (mRampBlocksTimeLeft < 0.0f)
  310. {
  311. for (size_t i = 0; i < mRampBlocks.size(); ++i)
  312. {
  313. mBodyInterface->SetPositionAndRotation(mRampBlocks[i], cRampBlocksStart + float(i) * cRampBlocksDelta, cRampOrientation, EActivation::Activate);
  314. mBodyInterface->SetLinearAndAngularVelocity(mRampBlocks[i], Vec3::sZero(), Vec3::sZero());
  315. }
  316. mRampBlocksTimeLeft = cRampBlocksTime;
  317. }
  318. }
  319. void CharacterBaseTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  320. {
  321. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  322. UIElement *scene_name = inUI->CreateMenu();
  323. for (uint i = 0; i < size(sScenes); ++i)
  324. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
  325. inUI->ShowMenu(scene_name);
  326. });
  327. }
  328. void CharacterBaseTest::GetInitialCamera(CameraState& ioState) const
  329. {
  330. // This will become the local space offset, look down the x axis and slightly down
  331. ioState.mPos = Vec3::sZero();
  332. ioState.mForward = Vec3(10.0f, -2.0f, 0).Normalized();
  333. }
  334. Mat44 CharacterBaseTest::GetCameraPivot(float inCameraHeading, float inCameraPitch) const
  335. {
  336. // Pivot is center of character + distance behind based on the heading and pitch of the camera
  337. Vec3 fwd = Vec3(Cos(inCameraPitch) * Cos(inCameraHeading), Sin(inCameraPitch), Cos(inCameraPitch) * Sin(inCameraHeading));
  338. return Mat44::sTranslation(GetCharacterPosition() + Vec3(0, cCharacterHeightStanding + cCharacterRadiusStanding, 0) - 5.0f * fwd);
  339. }
  340. void CharacterBaseTest::SaveState(StateRecorder &inStream) const
  341. {
  342. inStream.Write(mTime);
  343. inStream.Write(mRampBlocksTimeLeft);
  344. }
  345. void CharacterBaseTest::RestoreState(StateRecorder &inStream)
  346. {
  347. inStream.Read(mTime);
  348. inStream.Read(mRampBlocksTimeLeft);
  349. }
  350. void CharacterBaseTest::DrawCharacterState(const CharacterBase *inCharacter, Mat44Arg inCharacterTransform, Vec3Arg inCharacterVelocity)
  351. {
  352. // Draw current location
  353. // Drawing prior to update since the physics system state is also that prior to the simulation step (so that all detected collisions etc. make sense)
  354. mDebugRenderer->DrawCoordinateSystem(inCharacterTransform, 0.1f);
  355. // Determine color
  356. CharacterBase::EGroundState ground_state = inCharacter->GetGroundState();
  357. Color color;
  358. switch (ground_state)
  359. {
  360. case CharacterBase::EGroundState::OnGround:
  361. color = Color::sGreen;
  362. break;
  363. case CharacterBase::EGroundState::OnSteepGround:
  364. color = Color::sOrange;
  365. break;
  366. case CharacterBase::EGroundState::InAir:
  367. default:
  368. color = Color::sRed;
  369. break;
  370. }
  371. // Draw the state of the ground contact
  372. if (ground_state != CharacterBase::EGroundState::InAir)
  373. {
  374. Vec3 ground_position = inCharacter->GetGroundPosition();
  375. Vec3 ground_normal = inCharacter->GetGroundNormal();
  376. Vec3 ground_velocity = inCharacter->GetGroundVelocity();
  377. // Draw ground position
  378. mDebugRenderer->DrawMarker(ground_position, Color::sRed, 0.1f);
  379. mDebugRenderer->DrawArrow(ground_position, ground_position + 2.0f * ground_normal, Color::sGreen, 0.1f);
  380. // Draw ground velocity
  381. if (!ground_velocity.IsNearZero())
  382. mDebugRenderer->DrawArrow(ground_position, ground_position + ground_velocity, Color::sBlue, 0.1f);
  383. }
  384. // Draw provided character velocity
  385. if (!inCharacterVelocity.IsNearZero())
  386. mDebugRenderer->DrawArrow(inCharacterTransform.GetTranslation(), inCharacterTransform.GetTranslation() + inCharacterVelocity, Color::sYellow, 0.1f);
  387. // Draw text info
  388. const PhysicsMaterial *ground_material = inCharacter->GetGroundMaterial();
  389. Vec3 horizontal_velocity = inCharacterVelocity;
  390. horizontal_velocity.SetY(0);
  391. mDebugRenderer->DrawText3D(inCharacterTransform.GetTranslation(), StringFormat("Mat: %s\nHorizontal Vel: %.1f m/s\nVertical Vel: %.1f m/s", ground_material->GetDebugName(), (double)horizontal_velocity.Length(), (double)inCharacterVelocity.GetY()), color, 0.25f);
  392. }