CharacterBaseTest.cpp 24 KB

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