CharacterBaseTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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(300.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. // Create ramp
  164. BodyCreationSettings ramp(new BoxShape(Vec3(4.0f, 0.1f, 3.0f)), cRampPosition, cRampOrientation, EMotionType::Static, Layers::NON_MOVING);
  165. mBodyInterface->CreateAndAddBody(ramp, EActivation::DontActivate);
  166. // Create blocks on ramp
  167. Ref<Shape> block = new BoxShape(Vec3::sReplicate(0.5f));
  168. BodyCreationSettings bcs(block, cRampBlocksStart, cRampOrientation, EMotionType::Dynamic, Layers::MOVING);
  169. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  170. bcs.mMassPropertiesOverride.mMass = 10.0f;
  171. for (int i = 0; i < 4; ++i)
  172. {
  173. mRampBlocks.emplace_back(mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate));
  174. bcs.mPosition += cRampBlocksDelta;
  175. }
  176. }
  177. // Create three funnels with walls that are too steep to climb
  178. Ref<Shape> funnel = new BoxShape(Vec3(0.1f, 1.0f, 1.0f));
  179. for (int i = 0; i < 2; ++i)
  180. {
  181. Quat rotation = Quat::sRotation(Vec3::sAxisY(), JPH_PI * i);
  182. 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);
  183. }
  184. for (int i = 0; i < 3; ++i)
  185. {
  186. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 2.0f / 3.0f * JPH_PI * i);
  187. 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);
  188. }
  189. for (int i = 0; i < 4; ++i)
  190. {
  191. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI * i);
  192. 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);
  193. }
  194. // Create small bumps
  195. {
  196. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cSmallBumpHeight, 0.5f * cSmallBumpWidth), 0.0f), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  197. for (int i = 0; i < 10; ++i)
  198. {
  199. step.mPosition = cSmallBumpsPosition + Vec3(0, 0.5f * cSmallBumpHeight, cSmallBumpDelta * i);
  200. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  201. }
  202. }
  203. // Create large bumps
  204. {
  205. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cLargeBumpHeight, 0.5f * cLargeBumpWidth)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  206. for (int i = 0; i < 5; ++i)
  207. {
  208. step.mPosition = cLargeBumpsPosition + Vec3(0, 0.5f * cLargeBumpHeight, cLargeBumpDelta * i);
  209. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  210. }
  211. }
  212. // Create stairs
  213. {
  214. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cStairsStepHeight, 0.5f * cStairsStepHeight)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  215. for (int i = 0; i < 10; ++i)
  216. {
  217. step.mPosition = cStairsPosition + Vec3(0, cStairsStepHeight * (0.5f + i), cStairsStepHeight * i);
  218. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  219. }
  220. }
  221. // Create stairs with too little space between the steps
  222. {
  223. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cNoStairsStepHeight, 0.5f * cNoStairsStepHeight)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  224. for (int i = 0; i < 10; ++i)
  225. {
  226. step.mPosition = cNoStairsPosition + Vec3(0, cNoStairsStepHeight * (0.5f + i), cNoStairsStepDelta * i);
  227. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  228. }
  229. }
  230. // Create mesh with walls at varying angles
  231. {
  232. TriangleList triangles;
  233. Vec3 p1(0.5f * cMeshWallWidth, 0, 0);
  234. Vec3 h(0, cMeshWallHeight, 0);
  235. for (int i = 0; i < cMeshWallSegments; ++i)
  236. {
  237. float delta = cMeshWallStepStart + i * (cMeshWallStepEnd - cMeshWallStepStart) / (cMeshWallSegments - 1);
  238. Vec3 p2 = Vec3((i & 1)? 0.5f * cMeshWallWidth : -0.5f * cMeshWallWidth, 0, p1.GetZ() + delta);
  239. triangles.push_back(Triangle(p1, p1 + h, p2 + h));
  240. triangles.push_back(Triangle(p1, p2 + h, p2));
  241. p1 = p2;
  242. }
  243. MeshShapeSettings mesh(triangles);
  244. mesh.SetEmbedded();
  245. BodyCreationSettings wall(&mesh, cMeshWallPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  246. mBodyInterface->CreateAndAddBody(wall, EActivation::DontActivate);
  247. }
  248. }
  249. else
  250. {
  251. // Load scene
  252. Ref<PhysicsScene> scene;
  253. if (!ObjectStreamIn::sReadObject((String("Assets/") + sSceneName + ".bof").c_str(), scene))
  254. FatalError("Failed to load scene");
  255. scene->FixInvalidScales();
  256. for (BodyCreationSettings &settings : scene->GetBodies())
  257. {
  258. settings.mObjectLayer = Layers::NON_MOVING;
  259. settings.mFriction = 0.5f;
  260. }
  261. scene->CreateBodies(mPhysicsSystem);
  262. }
  263. // Create capsule shapes for all stances
  264. mStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cCharacterHeightStanding, cCharacterRadiusStanding)).Create().Get();
  265. mCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cCharacterHeightCrouching, cCharacterRadiusCrouching)).Create().Get();
  266. }
  267. void CharacterBaseTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  268. {
  269. // Update scene time
  270. mTime += inParams.mDeltaTime;
  271. // Determine controller input
  272. Vec3 control_input = Vec3::sZero();
  273. if (inParams.mKeyboard->IsKeyPressed(DIK_LEFT)) control_input.SetZ(-1);
  274. if (inParams.mKeyboard->IsKeyPressed(DIK_RIGHT)) control_input.SetZ(1);
  275. if (inParams.mKeyboard->IsKeyPressed(DIK_UP)) control_input.SetX(1);
  276. if (inParams.mKeyboard->IsKeyPressed(DIK_DOWN)) control_input.SetX(-1);
  277. if (control_input != Vec3::sZero())
  278. control_input = control_input.Normalized();
  279. // Rotate controls to align with the camera
  280. Vec3 cam_fwd = inParams.mCameraState.mForward;
  281. cam_fwd.SetY(0.0f);
  282. cam_fwd = cam_fwd.NormalizedOr(Vec3::sAxisX());
  283. Quat rotation = Quat::sFromTo(Vec3::sAxisX(), cam_fwd);
  284. control_input = rotation * control_input;
  285. // Check actions
  286. bool jump = false;
  287. bool switch_stance = false;
  288. for (int key = inParams.mKeyboard->GetFirstKey(); key != 0; key = inParams.mKeyboard->GetNextKey())
  289. {
  290. if (key == DIK_RSHIFT)
  291. switch_stance = true;
  292. else if (key == DIK_RCONTROL)
  293. jump = true;
  294. }
  295. HandleInput(control_input, jump, switch_stance, inParams.mDeltaTime);
  296. // Animate bodies
  297. if (!mRotatingBody.IsInvalid())
  298. mBodyInterface->MoveKinematic(mRotatingBody, cRotatingPosition, Quat::sRotation(Vec3::sAxisY(), JPH_PI * Sin(mTime)), inParams.mDeltaTime);
  299. if (!mHorizontallyMovingBody.IsInvalid())
  300. mBodyInterface->MoveKinematic(mHorizontallyMovingBody, cHorizontallyMovingPosition + Vec3(3.0f * Sin(mTime), 0, 0), cHorizontallyMovingOrientation, inParams.mDeltaTime);
  301. if (!mVerticallyMovingBody.IsInvalid())
  302. mBodyInterface->MoveKinematic(mVerticallyMovingBody, cVerticallyMovingPosition + Vec3(0, 1.75f * Sin(mTime), 0), cVerticallyMovingOrientation, inParams.mDeltaTime);
  303. // Reset ramp blocks
  304. mRampBlocksTimeLeft -= inParams.mDeltaTime;
  305. if (mRampBlocksTimeLeft < 0.0f)
  306. {
  307. for (size_t i = 0; i < mRampBlocks.size(); ++i)
  308. {
  309. mBodyInterface->SetPositionAndRotation(mRampBlocks[i], cRampBlocksStart + float(i) * cRampBlocksDelta, cRampOrientation, EActivation::Activate);
  310. mBodyInterface->SetLinearAndAngularVelocity(mRampBlocks[i], Vec3::sZero(), Vec3::sZero());
  311. }
  312. mRampBlocksTimeLeft = cRampBlocksTime;
  313. }
  314. }
  315. void CharacterBaseTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  316. {
  317. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  318. UIElement *scene_name = inUI->CreateMenu();
  319. for (uint i = 0; i < size(sScenes); ++i)
  320. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
  321. inUI->ShowMenu(scene_name);
  322. });
  323. }
  324. void CharacterBaseTest::GetInitialCamera(CameraState& ioState) const
  325. {
  326. // This will become the local space offset, look down the x axis and slightly down
  327. ioState.mPos = Vec3::sZero();
  328. ioState.mForward = Vec3(10.0f, -2.0f, 0).Normalized();
  329. }
  330. Mat44 CharacterBaseTest::GetCameraPivot(float inCameraHeading, float inCameraPitch) const
  331. {
  332. // Pivot is center of character + distance behind based on the heading and pitch of the camera
  333. Vec3 fwd = Vec3(Cos(inCameraPitch) * Cos(inCameraHeading), Sin(inCameraPitch), Cos(inCameraPitch) * Sin(inCameraHeading));
  334. return Mat44::sTranslation(GetCharacterPosition() + Vec3(0, cCharacterHeightStanding + cCharacterRadiusStanding, 0) - 5.0f * fwd);
  335. }
  336. void CharacterBaseTest::SaveState(StateRecorder &inStream) const
  337. {
  338. inStream.Write(mTime);
  339. inStream.Write(mRampBlocksTimeLeft);
  340. }
  341. void CharacterBaseTest::RestoreState(StateRecorder &inStream)
  342. {
  343. inStream.Read(mTime);
  344. inStream.Read(mRampBlocksTimeLeft);
  345. }
  346. void CharacterBaseTest::DrawCharacterState(const CharacterBase *inCharacter, Mat44Arg inCharacterTransform, Vec3Arg inCharacterVelocity)
  347. {
  348. // Draw current location
  349. // 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)
  350. mDebugRenderer->DrawCoordinateSystem(inCharacterTransform, 0.1f);
  351. // Determine color
  352. CharacterBase::EGroundState ground_state = inCharacter->GetGroundState();
  353. Color color;
  354. switch (ground_state)
  355. {
  356. case CharacterBase::EGroundState::OnGround:
  357. color = Color::sGreen;
  358. break;
  359. case CharacterBase::EGroundState::OnSteepGround:
  360. color = Color::sOrange;
  361. break;
  362. case CharacterBase::EGroundState::InAir:
  363. default:
  364. color = Color::sRed;
  365. break;
  366. }
  367. // Draw the state of the ground contact
  368. if (ground_state != CharacterBase::EGroundState::InAir)
  369. {
  370. Vec3 ground_position = inCharacter->GetGroundPosition();
  371. Vec3 ground_normal = inCharacter->GetGroundNormal();
  372. Vec3 ground_velocity = inCharacter->GetGroundVelocity();
  373. // Draw ground position
  374. mDebugRenderer->DrawMarker(ground_position, Color::sRed, 0.1f);
  375. mDebugRenderer->DrawArrow(ground_position, ground_position + 2.0f * ground_normal, Color::sGreen, 0.1f);
  376. // Draw ground velocity
  377. if (!ground_velocity.IsNearZero())
  378. mDebugRenderer->DrawArrow(ground_position, ground_position + ground_velocity, Color::sBlue, 0.1f);
  379. }
  380. // Draw provided character velocity
  381. if (!inCharacterVelocity.IsNearZero())
  382. mDebugRenderer->DrawArrow(inCharacterTransform.GetTranslation(), inCharacterTransform.GetTranslation() + inCharacterVelocity, Color::sYellow, 0.1f);
  383. // Draw text info
  384. const PhysicsMaterial *ground_material = inCharacter->GetGroundMaterial();
  385. Vec3 horizontal_velocity = inCharacterVelocity;
  386. horizontal_velocity.SetY(0);
  387. 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);
  388. }