CharacterBaseTest.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Character/CharacterBaseTest.h>
  6. #include <Jolt/Physics/PhysicsScene.h>
  7. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  8. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  9. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  10. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  11. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  12. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  13. #include <Jolt/Physics/Constraints/HingeConstraint.h>
  14. #include <Jolt/Core/StringTools.h>
  15. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  16. #include <Application/DebugUI.h>
  17. #include <Layers.h>
  18. #include <Utils/Log.h>
  19. #include <Renderer/DebugRendererImp.h>
  20. JPH_IMPLEMENT_RTTI_ABSTRACT(CharacterBaseTest)
  21. {
  22. JPH_ADD_BASE_CLASS(CharacterBaseTest, Test)
  23. }
  24. const char *CharacterBaseTest::sScenes[] =
  25. {
  26. "PerlinMesh",
  27. "PerlinHeightField",
  28. "ObstacleCourse",
  29. "InitiallyIntersecting",
  30. #ifdef JPH_OBJECT_STREAM
  31. "Terrain1",
  32. "Terrain2",
  33. #endif // JPH_OBJECT_STREAM
  34. };
  35. const char *CharacterBaseTest::sSceneName = "ObstacleCourse";
  36. // Scene constants
  37. static const RVec3 cRotatingPosition(-5, 0.15f, 15);
  38. static const Quat cRotatingOrientation = Quat::sIdentity();
  39. static const RVec3 cRotatingWallPosition(5, 1.0f, 25.0f);
  40. static const Quat cRotatingWallOrientation = Quat::sIdentity();
  41. static const RVec3 cRotatingAndTranslatingPosition(-10, 0.15f, 27.5f);
  42. static const Quat cRotatingAndTranslatingOrientation = Quat::sIdentity();
  43. static const RVec3 cSmoothVerticallyMovingPosition(0, 2.0f, 15);
  44. static const Quat cSmoothVerticallyMovingOrientation = Quat::sIdentity();
  45. static const RVec3 cReversingVerticallyMovingPosition(0, 0.15f, 25);
  46. static const Quat cReversingVerticallyMovingOrientation = Quat::sIdentity();
  47. static const RVec3 cHorizontallyMovingPosition(5, 1, 15);
  48. static const Quat cHorizontallyMovingOrientation = Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI);
  49. static const RVec3 cConveyorBeltPosition(-10, 0.15f, 15);
  50. static const RVec3 cRampPosition(15, 2.2f, 15);
  51. static const Quat cRampOrientation = Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI);
  52. static const RVec3 cRampBlocksStart = cRampPosition + Vec3(-3.0f, 3.0f, 1.5f);
  53. static const Vec3 cRampBlocksDelta = Vec3(2.0f, 0, 0);
  54. static const float cRampBlocksTime = 5.0f;
  55. static const RVec3 cSmallBumpsPosition(-5.0f, 0, 2.5f);
  56. static const float cSmallBumpHeight = 0.05f;
  57. static const float cSmallBumpWidth = 0.01f;
  58. static const float cSmallBumpDelta = 0.5f;
  59. static const RVec3 cLargeBumpsPosition(-10.0f, 0, 2.5f);
  60. static const float cLargeBumpHeight = 0.3f;
  61. static const float cLargeBumpWidth = 0.1f;
  62. static const float cLargeBumpDelta = 2.0f;
  63. static const RVec3 cStairsPosition(-15.0f, 0, 2.5f);
  64. static const float cStairsStepHeight = 0.3f;
  65. static const RVec3 cMeshStairsPosition(-20.0f, 0, 2.5f);
  66. static const RVec3 cNoStairsPosition(-15.0f, 0, 10.0f);
  67. static const float cNoStairsStepHeight = 0.3f;
  68. static const float cNoStairsStepDelta = 0.05f;
  69. static const RVec3 cMeshNoStairsPosition(-20.0f, 0, 10.0f);
  70. static const RVec3 cMeshWallPosition(-25.0f, 0, -27.0f);
  71. static const float cMeshWallHeight = 3.0f;
  72. static const float cMeshWallWidth = 2.0f;
  73. static const float cMeshWallStepStart = 0.5f;
  74. static const float cMeshWallStepEnd = 4.0f;
  75. static const int cMeshWallSegments = 25;
  76. static const RVec3 cHalfCylinderPosition(5.0f, 0, 8.0f);
  77. static const RVec3 cMeshBoxPosition(30.0f, 1.5f, 5.0f);
  78. static const RVec3 cSensorPosition(30, 0.9f, -5);
  79. static const RVec3 cCharacterPosition(-3.5f, 0, 3.0f);
  80. static const RVec3 cCharacterVirtualPosition(-5.0f, 0, 3.0f);
  81. static const RVec3 cCharacterVirtualWithInnerBodyPosition(-6.5f, 0, 3.0f);
  82. static const Vec3 cCharacterVelocity(0, 0, 2);
  83. CharacterBaseTest::~CharacterBaseTest()
  84. {
  85. if (mAnimatedCharacter != nullptr)
  86. mAnimatedCharacter->RemoveFromPhysicsSystem();
  87. }
  88. void CharacterBaseTest::Initialize()
  89. {
  90. // Create capsule shapes for all stances
  91. switch (sShapeType)
  92. {
  93. case EType::Capsule:
  94. mStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cCharacterHeightStanding, cCharacterRadiusStanding)).Create().Get();
  95. mCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cCharacterHeightCrouching, cCharacterRadiusCrouching)).Create().Get();
  96. mInnerStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cInnerShapeFraction * cCharacterHeightStanding, cInnerShapeFraction * cCharacterRadiusStanding)).Create().Get();
  97. mInnerCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new CapsuleShape(0.5f * cInnerShapeFraction * cCharacterHeightCrouching, cInnerShapeFraction * cCharacterRadiusCrouching)).Create().Get();
  98. break;
  99. case EType::Cylinder:
  100. mStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new CylinderShape(0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, cCharacterRadiusStanding)).Create().Get();
  101. mCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new CylinderShape(0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, cCharacterRadiusCrouching)).Create().Get();
  102. mInnerStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new CylinderShape(cInnerShapeFraction * (0.5f * cCharacterHeightStanding + cCharacterRadiusStanding), cInnerShapeFraction * cCharacterRadiusStanding)).Create().Get();
  103. mInnerCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new CylinderShape(cInnerShapeFraction * (0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching), cInnerShapeFraction * cCharacterRadiusCrouching)).Create().Get();
  104. break;
  105. case EType::Box:
  106. mStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new BoxShape(Vec3(cCharacterRadiusStanding, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, cCharacterRadiusStanding))).Create().Get();
  107. mCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new BoxShape(Vec3(cCharacterRadiusCrouching, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, cCharacterRadiusCrouching))).Create().Get();
  108. mInnerStandingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, 0), Quat::sIdentity(), new BoxShape(cInnerShapeFraction * Vec3(cCharacterRadiusStanding, 0.5f * cCharacterHeightStanding + cCharacterRadiusStanding, cCharacterRadiusStanding))).Create().Get();
  109. mInnerCrouchingShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, 0), Quat::sIdentity(), new BoxShape(cInnerShapeFraction * Vec3(cCharacterRadiusCrouching, 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching, cCharacterRadiusCrouching))).Create().Get();
  110. break;
  111. }
  112. if (strcmp(sSceneName, "PerlinMesh") == 0)
  113. {
  114. // Default terrain
  115. CreateMeshTerrain();
  116. }
  117. else if (strcmp(sSceneName, "PerlinHeightField") == 0)
  118. {
  119. // Default terrain
  120. CreateHeightFieldTerrain();
  121. }
  122. else if (strcmp(sSceneName, "InitiallyIntersecting") == 0)
  123. {
  124. CreateFloor();
  125. // Create a grid of boxes that are initially intersecting with the character
  126. RefConst<Shape> box = new BoxShape(Vec3(0.1f, 0.1f, 0.1f));
  127. BodyCreationSettings settings(box, RVec3(0, 0.5f, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  128. for (int x = 0; x < 4; ++x)
  129. for (int y = 0; y <= 10; ++y)
  130. for (int z = 0; z <= 10; ++z)
  131. {
  132. settings.mPosition = RVec3(-0.5f + 0.1f * x, 0.1f + 0.1f * y, -0.5f + 0.1f * z);
  133. mBodyInterface->CreateAndAddBody(settings, EActivation::DontActivate);
  134. }
  135. }
  136. else if (strcmp(sSceneName, "ObstacleCourse") == 0)
  137. {
  138. // Default terrain
  139. CreateFloor(350.0f);
  140. {
  141. // Create ramps with different inclinations
  142. Ref<Shape> ramp = RotatedTranslatedShapeSettings(Vec3(0, 0, -2.5f), Quat::sIdentity(), new BoxShape(Vec3(1.0f, 0.05f, 2.5f))).Create().Get();
  143. for (int angle = 0; angle < 18; ++angle)
  144. mBodyInterface->CreateAndAddBody(BodyCreationSettings(ramp, RVec3(-15.0f + angle * 2.0f, 0, -10.0f), Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(10.0f * angle)), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  145. }
  146. {
  147. // Create ramps with different inclinations intersecting with a steep slope
  148. Ref<Shape> ramp = RotatedTranslatedShapeSettings(Vec3(0, 0, -2.5f), Quat::sIdentity(), new BoxShape(Vec3(1.0f, 0.05f, 2.5f))).Create().Get();
  149. Ref<Shape> ramp2 = RotatedTranslatedShapeSettings(Vec3(0, 2.0f, 0), Quat::sIdentity(), new BoxShape(Vec3(0.05f, 2.0f, 1.0f))).Create().Get();
  150. for (int angle = 0; angle < 9; ++angle)
  151. {
  152. mBodyInterface->CreateAndAddBody(BodyCreationSettings(ramp, RVec3(-15.0f + angle * 2.0f, 0, -20.0f - angle * 0.1f), Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(10.0f * angle)), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  153. mBodyInterface->CreateAndAddBody(BodyCreationSettings(ramp2, RVec3(-15.0f + angle * 2.0f, 0, -21.0f), Quat::sRotation(Vec3::sAxisZ(), DegreesToRadians(20.0f)), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  154. }
  155. }
  156. {
  157. // Create wall consisting of vertical pillars
  158. // Note: Convex radius 0 because otherwise it will be a bumpy wall
  159. Ref<Shape> wall = new BoxShape(Vec3(0.1f, 2.5f, 0.1f), 0.0f);
  160. for (int z = 0; z < 30; ++z)
  161. mBodyInterface->CreateAndAddBody(BodyCreationSettings(wall, RVec3(0.0f, 2.5f, 2.0f + 0.2f * z), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  162. }
  163. {
  164. // Kinematic blocks to test interacting with moving objects
  165. Ref<Shape> kinematic = new BoxShape(Vec3(1, 0.15f, 3.0f));
  166. mRotatingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cRotatingPosition, cRotatingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  167. mRotatingWallBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(3.0f, 1, 0.15f)), cRotatingWallPosition, cRotatingWallOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  168. mRotatingAndTranslatingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cRotatingAndTranslatingPosition, cRotatingAndTranslatingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  169. mSmoothVerticallyMovingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cSmoothVerticallyMovingPosition, cSmoothVerticallyMovingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  170. mReversingVerticallyMovingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cReversingVerticallyMovingPosition, cReversingVerticallyMovingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  171. mHorizontallyMovingBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(kinematic, cHorizontallyMovingPosition, cHorizontallyMovingOrientation, EMotionType::Kinematic, Layers::MOVING), EActivation::Activate);
  172. }
  173. {
  174. // Conveyor belt (only works with virtual character)
  175. mConveyorBeltBody = mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(1, 0.15f, 3.0f)), cConveyorBeltPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::Activate);
  176. }
  177. {
  178. // A rolling sphere towards the player
  179. BodyCreationSettings bcs(new SphereShape(0.2f), RVec3(0.0f, 0.2f, -1.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  180. bcs.mLinearVelocity = Vec3(0, 0, 2.0f);
  181. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  182. bcs.mMassPropertiesOverride.mMass = 10.0f;
  183. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  184. }
  185. {
  186. // Dynamic blocks to test player pushing blocks
  187. Ref<Shape> block = new BoxShape(Vec3::sReplicate(0.5f));
  188. for (int y = 0; y < 3; ++y)
  189. {
  190. BodyCreationSettings bcs(block, RVec3(5.0f, 0.5f + float(y), 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  191. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  192. bcs.mMassPropertiesOverride.mMass = 10.0f;
  193. mBodyInterface->CreateAndAddBody(bcs, EActivation::DontActivate);
  194. }
  195. }
  196. {
  197. // Dynamic block on a static step (to test pushing block on stairs)
  198. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(0.5f, 0.15f, 0.5f)), RVec3(10.0f, 0.15f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  199. BodyCreationSettings bcs(new BoxShape(Vec3::sReplicate(0.5f)), RVec3(10.0f, 0.8f, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  200. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  201. bcs.mMassPropertiesOverride.mMass = 10.0f;
  202. mBodyInterface->CreateAndAddBody(bcs, EActivation::DontActivate);
  203. }
  204. {
  205. // Dynamic spheres to test player pushing stuff you can step on
  206. float h = 0.0f;
  207. for (int y = 0; y < 3; ++y)
  208. {
  209. float r = 0.4f - 0.1f * y;
  210. h += r;
  211. BodyCreationSettings bcs(new SphereShape(r), RVec3(15.0f, h, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  212. h += r;
  213. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  214. bcs.mMassPropertiesOverride.mMass = 10.0f;
  215. mBodyInterface->CreateAndAddBody(bcs, EActivation::DontActivate);
  216. }
  217. }
  218. {
  219. // A seesaw to test character gravity
  220. BodyID b1 = mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(1.0f, 0.2f, 0.05f)), RVec3(20.0f, 0.2f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  221. BodyCreationSettings bcs(new BoxShape(Vec3(1.0f, 0.05f, 5.0f)), RVec3(20.0f, 0.45f, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  222. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  223. bcs.mMassPropertiesOverride.mMass = 10.0f;
  224. BodyID b2 = mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  225. // Connect the parts with a hinge
  226. HingeConstraintSettings hinge;
  227. hinge.mPoint1 = hinge.mPoint2 = RVec3(20.0f, 0.4f, 0.0f);
  228. hinge.mHingeAxis1 = hinge.mHingeAxis2 = Vec3::sAxisX();
  229. mPhysicsSystem->AddConstraint(mBodyInterface->CreateConstraint(&hinge, b1, b2));
  230. }
  231. {
  232. // A board above the character to crouch and jump up against
  233. float h = 0.5f * cCharacterHeightCrouching + cCharacterRadiusCrouching + 0.1f;
  234. for (int x = 0; x < 2; ++x)
  235. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(1.0f, h, 0.05f)), RVec3(25.0f, h, x == 0? -0.95f : 0.95f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  236. BodyCreationSettings bcs(new BoxShape(Vec3(1.0f, 0.05f, 1.0f)), RVec3(25.0f, 2.0f * h + 0.05f, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  237. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  238. bcs.mMassPropertiesOverride.mMass = 10.0f;
  239. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  240. }
  241. {
  242. // A floating static block
  243. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(0.5f)), RVec3(30.0f, 1.5f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  244. }
  245. {
  246. // Create ramp
  247. BodyCreationSettings ramp(new BoxShape(Vec3(4.0f, 0.1f, 3.0f)), cRampPosition, cRampOrientation, EMotionType::Static, Layers::NON_MOVING);
  248. mBodyInterface->CreateAndAddBody(ramp, EActivation::DontActivate);
  249. // Create blocks on ramp
  250. Ref<Shape> block = new BoxShape(Vec3::sReplicate(0.5f));
  251. BodyCreationSettings bcs(block, cRampBlocksStart, cRampOrientation, EMotionType::Dynamic, Layers::MOVING);
  252. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  253. bcs.mMassPropertiesOverride.mMass = 10.0f;
  254. for (int i = 0; i < 4; ++i)
  255. {
  256. mRampBlocks.emplace_back(mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate));
  257. bcs.mPosition += cRampBlocksDelta;
  258. }
  259. }
  260. // Create three funnels with walls that are too steep to climb
  261. Ref<Shape> funnel = new BoxShape(Vec3(0.1f, 1.0f, 1.0f));
  262. for (int i = 0; i < 2; ++i)
  263. {
  264. Quat rotation = Quat::sRotation(Vec3::sAxisY(), JPH_PI * i);
  265. mBodyInterface->CreateAndAddBody(BodyCreationSettings(funnel, RVec3(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);
  266. }
  267. for (int i = 0; i < 3; ++i)
  268. {
  269. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 2.0f / 3.0f * JPH_PI * i);
  270. mBodyInterface->CreateAndAddBody(BodyCreationSettings(funnel, RVec3(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);
  271. }
  272. for (int i = 0; i < 4; ++i)
  273. {
  274. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI * i);
  275. mBodyInterface->CreateAndAddBody(BodyCreationSettings(funnel, RVec3(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);
  276. }
  277. // Create small bumps
  278. {
  279. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cSmallBumpHeight, 0.5f * cSmallBumpWidth), 0.0f), RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  280. for (int i = 0; i < 10; ++i)
  281. {
  282. step.mPosition = cSmallBumpsPosition + Vec3(0, 0.5f * cSmallBumpHeight, cSmallBumpDelta * i);
  283. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  284. }
  285. }
  286. // Create large bumps
  287. {
  288. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cLargeBumpHeight, 0.5f * cLargeBumpWidth)), RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  289. for (int i = 0; i < 5; ++i)
  290. {
  291. step.mPosition = cLargeBumpsPosition + Vec3(0, 0.5f * cLargeBumpHeight, cLargeBumpDelta * i);
  292. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  293. }
  294. }
  295. // Create stairs
  296. {
  297. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cStairsStepHeight, 0.5f * cStairsStepHeight)), RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  298. for (int i = 0; i < 10; ++i)
  299. {
  300. step.mPosition = cStairsPosition + Vec3(0, cStairsStepHeight * (0.5f + i), cStairsStepHeight * i);
  301. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  302. }
  303. }
  304. // A wall beside the stairs
  305. 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);
  306. // Create stairs from triangles
  307. {
  308. TriangleList triangles;
  309. float rear_z = 10 * cStairsStepHeight;
  310. for (int i = 0; i < 10; ++i)
  311. {
  312. // Start of step
  313. Vec3 base(0, cStairsStepHeight * i, cStairsStepHeight * i);
  314. // Left side
  315. Vec3 b1 = base + Vec3(2.0f, 0, 0);
  316. Vec3 s1 = b1 + Vec3(0, cStairsStepHeight, 0);
  317. Vec3 p1 = s1 + Vec3(0, 0, cStairsStepHeight);
  318. // Right side
  319. Vec3 width(-4.0f, 0, 0);
  320. Vec3 b2 = b1 + width;
  321. Vec3 s2 = s1 + width;
  322. Vec3 p2 = p1 + width;
  323. triangles.push_back(Triangle(s1, b1, s2));
  324. triangles.push_back(Triangle(b1, b2, s2));
  325. triangles.push_back(Triangle(s1, p2, p1));
  326. triangles.push_back(Triangle(s1, s2, p2));
  327. // Side of stairs
  328. Vec3 rb2 = b2; rb2.SetZ(rear_z);
  329. Vec3 rs2 = s2; rs2.SetZ(rear_z);
  330. triangles.push_back(Triangle(s2, b2, rs2));
  331. triangles.push_back(Triangle(rs2, b2, rb2));
  332. }
  333. MeshShapeSettings mesh(triangles);
  334. mesh.SetEmbedded();
  335. BodyCreationSettings mesh_stairs(&mesh, cMeshStairsPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  336. mBodyInterface->CreateAndAddBody(mesh_stairs, EActivation::DontActivate);
  337. }
  338. // A wall to the side and behind the stairs
  339. 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);
  340. // Create stairs with too little space between the steps
  341. {
  342. BodyCreationSettings step(new BoxShape(Vec3(2.0f, 0.5f * cNoStairsStepHeight, 0.5f * cNoStairsStepHeight)), RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  343. for (int i = 0; i < 10; ++i)
  344. {
  345. step.mPosition = cNoStairsPosition + Vec3(0, cNoStairsStepHeight * (0.5f + i), cNoStairsStepDelta * i);
  346. mBodyInterface->CreateAndAddBody(step, EActivation::DontActivate);
  347. }
  348. }
  349. // Create stairs with too little space between the steps consisting of triangles
  350. {
  351. TriangleList triangles;
  352. for (int i = 0; i < 10; ++i)
  353. {
  354. // Start of step
  355. Vec3 base(0, cStairsStepHeight * i, cNoStairsStepDelta * i);
  356. // Left side
  357. Vec3 b1 = base - Vec3(2.0f, 0, 0);
  358. Vec3 s1 = b1 + Vec3(0, cStairsStepHeight, 0);
  359. Vec3 p1 = s1 + Vec3(0, 0, cNoStairsStepDelta);
  360. // Right side
  361. Vec3 width(4.0f, 0, 0);
  362. Vec3 b2 = b1 + width;
  363. Vec3 s2 = s1 + width;
  364. Vec3 p2 = p1 + width;
  365. triangles.push_back(Triangle(s1, s2, b1));
  366. triangles.push_back(Triangle(b1, s2, b2));
  367. triangles.push_back(Triangle(s1, p1, p2));
  368. triangles.push_back(Triangle(s1, p2, s2));
  369. }
  370. MeshShapeSettings mesh(triangles);
  371. mesh.SetEmbedded();
  372. BodyCreationSettings mesh_stairs(&mesh, cMeshNoStairsPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  373. mBodyInterface->CreateAndAddBody(mesh_stairs, EActivation::DontActivate);
  374. }
  375. // Create mesh with walls at varying angles
  376. {
  377. TriangleList triangles;
  378. Vec3 p1(0.5f * cMeshWallWidth, 0, 0);
  379. Vec3 h(0, cMeshWallHeight, 0);
  380. for (int i = 0; i < cMeshWallSegments; ++i)
  381. {
  382. float delta = cMeshWallStepStart + i * (cMeshWallStepEnd - cMeshWallStepStart) / (cMeshWallSegments - 1);
  383. Vec3 p2 = Vec3((i & 1)? 0.5f * cMeshWallWidth : -0.5f * cMeshWallWidth, 0, p1.GetZ() + delta);
  384. triangles.push_back(Triangle(p1, p1 + h, p2 + h));
  385. triangles.push_back(Triangle(p1, p2 + h, p2));
  386. p1 = p2;
  387. }
  388. MeshShapeSettings mesh(triangles);
  389. mesh.SetEmbedded();
  390. BodyCreationSettings wall(&mesh, cMeshWallPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  391. mBodyInterface->CreateAndAddBody(wall, EActivation::DontActivate);
  392. }
  393. // Create a half cylinder with caps for testing contact point limit
  394. {
  395. VertexList vertices;
  396. IndexedTriangleList triangles;
  397. // The half cylinder
  398. const int cPosSegments = 2;
  399. const int cAngleSegments = 512;
  400. const float cCylinderLength = 2.0f;
  401. for (int pos = 0; pos < cPosSegments; ++pos)
  402. for (int angle = 0; angle < cAngleSegments; ++angle)
  403. {
  404. uint32 start = (uint32)vertices.size();
  405. float radius = cCharacterRadiusStanding + 0.05f;
  406. float angle_rad = (-0.5f + float(angle) / cAngleSegments) * JPH_PI;
  407. float s = Sin(angle_rad);
  408. float c = Cos(angle_rad);
  409. float x = cCylinderLength * (-0.5f + float(pos) / (cPosSegments - 1));
  410. float y = angle == 0 || angle == cAngleSegments - 1? 0.5f : (1.0f - c) * radius;
  411. float z = s * radius;
  412. vertices.push_back(Float3(x, y, z));
  413. if (pos > 0 && angle > 0)
  414. {
  415. triangles.push_back(IndexedTriangle(start, start - 1, start - cAngleSegments));
  416. triangles.push_back(IndexedTriangle(start - 1, start - cAngleSegments - 1, start - cAngleSegments));
  417. }
  418. }
  419. // Add end caps
  420. uint32 end = cAngleSegments * (cPosSegments - 1);
  421. for (int angle = 0; angle < cAngleSegments - 1; ++angle)
  422. {
  423. triangles.push_back(IndexedTriangle(0, angle + 1, angle));
  424. triangles.push_back(IndexedTriangle(end, end + angle, end + angle + 1));
  425. }
  426. MeshShapeSettings mesh(std::move(vertices), std::move(triangles));
  427. mesh.SetEmbedded();
  428. BodyCreationSettings mesh_cylinder(&mesh, cHalfCylinderPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  429. mBodyInterface->CreateAndAddBody(mesh_cylinder, EActivation::DontActivate);
  430. }
  431. // Create a box made out of polygons (character should not get stuck behind back facing side)
  432. {
  433. VertexList vertices = {
  434. Float3(-1, 1, -1),
  435. Float3( 1, 1, -1),
  436. Float3( 1, 1, 1),
  437. Float3(-1, 1, 1),
  438. Float3(-1, -1, -1),
  439. Float3( 1, -1, -1),
  440. Float3( 1, -1, 1),
  441. Float3(-1, -1, 1)
  442. };
  443. IndexedTriangleList triangles = {
  444. IndexedTriangle(0, 3, 2),
  445. IndexedTriangle(0, 2, 1),
  446. IndexedTriangle(4, 5, 6),
  447. IndexedTriangle(4, 6, 7),
  448. IndexedTriangle(0, 4, 3),
  449. IndexedTriangle(3, 4, 7),
  450. IndexedTriangle(2, 6, 5),
  451. IndexedTriangle(2, 5, 1),
  452. IndexedTriangle(3, 7, 6),
  453. IndexedTriangle(3, 6, 2),
  454. IndexedTriangle(0, 1, 5),
  455. IndexedTriangle(0, 5, 4)
  456. };
  457. MeshShapeSettings mesh(std::move(vertices), std::move(triangles));
  458. mesh.SetEmbedded();
  459. BodyCreationSettings box(&mesh, cMeshBoxPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  460. mBodyInterface->CreateAndAddBody(box, EActivation::DontActivate);
  461. }
  462. // Create a sensor
  463. {
  464. BodyCreationSettings sensor(new BoxShape(Vec3::sReplicate(1.0f)), cSensorPosition, Quat::sIdentity(), EMotionType::Kinematic, Layers::SENSOR);
  465. sensor.mIsSensor = true;
  466. mSensorBody = mBodyInterface->CreateAndAddBody(sensor, EActivation::Activate);
  467. }
  468. // Create Character
  469. {
  470. CharacterSettings settings;
  471. settings.mLayer = Layers::MOVING;
  472. settings.mShape = mStandingShape;
  473. settings.mSupportingVolume = Plane(Vec3::sAxisY(), -cCharacterRadiusStanding); // Accept contacts that touch the lower sphere of the capsule
  474. mAnimatedCharacter = new Character(&settings, cCharacterPosition, Quat::sIdentity(), 0, mPhysicsSystem);
  475. mAnimatedCharacter->AddToPhysicsSystem();
  476. }
  477. // Create CharacterVirtual
  478. {
  479. CharacterVirtualSettings settings;
  480. settings.mShape = mStandingShape;
  481. settings.mSupportingVolume = Plane(Vec3::sAxisY(), -cCharacterRadiusStanding); // Accept contacts that touch the lower sphere of the capsule
  482. mAnimatedCharacterVirtual = new CharacterVirtual(&settings, cCharacterVirtualPosition, Quat::sIdentity(), 0, mPhysicsSystem);
  483. mAnimatedCharacterVirtual->SetCharacterVsCharacterCollision(&mCharacterVsCharacterCollision);
  484. mCharacterVsCharacterCollision.Add(mAnimatedCharacterVirtual);
  485. }
  486. // Create CharacterVirtual with inner rigid body
  487. {
  488. CharacterVirtualSettings settings;
  489. settings.mShape = mStandingShape;
  490. settings.mInnerBodyShape = mInnerStandingShape;
  491. settings.mSupportingVolume = Plane(Vec3::sAxisY(), -cCharacterRadiusStanding); // Accept contacts that touch the lower sphere of the capsule
  492. mAnimatedCharacterVirtualWithInnerBody = new CharacterVirtual(&settings, cCharacterVirtualWithInnerBodyPosition, Quat::sIdentity(), 0, mPhysicsSystem);
  493. mAnimatedCharacterVirtualWithInnerBody->SetCharacterVsCharacterCollision(&mCharacterVsCharacterCollision);
  494. mCharacterVsCharacterCollision.Add(mAnimatedCharacterVirtualWithInnerBody);
  495. }
  496. }
  497. #ifdef JPH_OBJECT_STREAM
  498. else
  499. {
  500. // Load scene
  501. Ref<PhysicsScene> scene;
  502. if (!ObjectStreamIn::sReadObject((String("Assets/") + sSceneName + ".bof").c_str(), scene))
  503. FatalError("Failed to load scene");
  504. scene->FixInvalidScales();
  505. for (BodyCreationSettings &settings : scene->GetBodies())
  506. {
  507. settings.mObjectLayer = Layers::NON_MOVING;
  508. settings.mFriction = 0.5f;
  509. }
  510. scene->CreateBodies(mPhysicsSystem);
  511. }
  512. #endif // JPH_OBJECT_STREAM
  513. }
  514. void CharacterBaseTest::ProcessInput(const ProcessInputParams &inParams)
  515. {
  516. // Determine controller input
  517. mControlInput = Vec3::sZero();
  518. if (inParams.mKeyboard->IsKeyPressed(EKey::Left)) mControlInput.SetZ(-1);
  519. if (inParams.mKeyboard->IsKeyPressed(EKey::Right)) mControlInput.SetZ(1);
  520. if (inParams.mKeyboard->IsKeyPressed(EKey::Up)) mControlInput.SetX(1);
  521. if (inParams.mKeyboard->IsKeyPressed(EKey::Down)) mControlInput.SetX(-1);
  522. if (mControlInput != Vec3::sZero())
  523. mControlInput = mControlInput.Normalized();
  524. // Rotate controls to align with the camera
  525. Vec3 cam_fwd = inParams.mCameraState.mForward;
  526. cam_fwd.SetY(0.0f);
  527. cam_fwd = cam_fwd.NormalizedOr(Vec3::sAxisX());
  528. Quat rotation = Quat::sFromTo(Vec3::sAxisX(), cam_fwd);
  529. mControlInput = rotation * mControlInput;
  530. // Check actions
  531. mJump = inParams.mKeyboard->IsKeyPressedAndTriggered(EKey::RControl, mWasJump);
  532. mSwitchStance = inParams.mKeyboard->IsKeyPressedAndTriggered(EKey::RShift, mWasSwitchStance);
  533. }
  534. void CharacterBaseTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  535. {
  536. // Update scene time
  537. mTime += inParams.mDeltaTime;
  538. // Update camera pivot
  539. mCameraPivot = GetCharacterPosition();
  540. // Animate bodies
  541. if (!mRotatingBody.IsInvalid())
  542. mBodyInterface->MoveKinematic(mRotatingBody, cRotatingPosition, Quat::sRotation(Vec3::sAxisY(), JPH_PI * Sin(mTime)), inParams.mDeltaTime);
  543. if (!mRotatingWallBody.IsInvalid())
  544. mBodyInterface->MoveKinematic(mRotatingWallBody, cRotatingWallPosition, Quat::sRotation(Vec3::sAxisY(), JPH_PI * Sin(mTime)), inParams.mDeltaTime);
  545. if (!mRotatingAndTranslatingBody.IsInvalid())
  546. mBodyInterface->MoveKinematic(mRotatingAndTranslatingBody, cRotatingAndTranslatingPosition + 5.0f * Vec3(Sin(JPH_PI * mTime), 0, Cos(JPH_PI * mTime)), Quat::sRotation(Vec3::sAxisY(), JPH_PI * Sin(mTime)), inParams.mDeltaTime);
  547. if (!mHorizontallyMovingBody.IsInvalid())
  548. mBodyInterface->MoveKinematic(mHorizontallyMovingBody, cHorizontallyMovingPosition + Vec3(3.0f * Sin(mTime), 0, 0), cHorizontallyMovingOrientation, inParams.mDeltaTime);
  549. if (!mSmoothVerticallyMovingBody.IsInvalid())
  550. mBodyInterface->MoveKinematic(mSmoothVerticallyMovingBody, cSmoothVerticallyMovingPosition + Vec3(0, 1.75f * Sin(mTime), 0), cSmoothVerticallyMovingOrientation, inParams.mDeltaTime);
  551. if (!mReversingVerticallyMovingBody.IsInvalid())
  552. {
  553. RVec3 pos = mBodyInterface->GetPosition(mReversingVerticallyMovingBody);
  554. if (pos.GetY() < cReversingVerticallyMovingPosition.GetY())
  555. mReversingVerticallyMovingVelocity = 1.0f;
  556. else if (pos.GetY() > cReversingVerticallyMovingPosition.GetY() + 5.0f)
  557. mReversingVerticallyMovingVelocity = -1.0f;
  558. mBodyInterface->MoveKinematic(mReversingVerticallyMovingBody, pos + Vec3(0, mReversingVerticallyMovingVelocity * 3.0f * inParams.mDeltaTime, 0), cReversingVerticallyMovingOrientation, inParams.mDeltaTime);
  559. }
  560. // Animate character
  561. if (mAnimatedCharacter != nullptr)
  562. mAnimatedCharacter->SetLinearVelocity(Sin(mTime) * cCharacterVelocity);
  563. // Animate character virtual
  564. for (CharacterVirtual *character : { mAnimatedCharacterVirtual, mAnimatedCharacterVirtualWithInnerBody })
  565. if (character != nullptr)
  566. {
  567. #ifdef JPH_DEBUG_RENDERER
  568. character->GetShape()->Draw(mDebugRenderer, character->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f), Color::sOrange, false, true);
  569. #else
  570. mDebugRenderer->DrawCapsule(character->GetCenterOfMassTransform(), 0.5f * cCharacterHeightStanding, cCharacterRadiusStanding + character->GetCharacterPadding(), Color::sOrange, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  571. #endif // JPH_DEBUG_RENDERER
  572. // Update velocity and apply gravity
  573. Vec3 velocity;
  574. if (character->GetGroundState() == CharacterVirtual::EGroundState::OnGround)
  575. velocity = Vec3::sZero();
  576. else
  577. velocity = character->GetLinearVelocity() * mAnimatedCharacter->GetUp() + mPhysicsSystem->GetGravity() * inParams.mDeltaTime;
  578. velocity += Sin(mTime) * cCharacterVelocity;
  579. character->SetLinearVelocity(velocity);
  580. // Move character
  581. CharacterVirtual::ExtendedUpdateSettings update_settings;
  582. character->ExtendedUpdate(inParams.mDeltaTime,
  583. mPhysicsSystem->GetGravity(),
  584. update_settings,
  585. mPhysicsSystem->GetDefaultBroadPhaseLayerFilter(Layers::MOVING),
  586. mPhysicsSystem->GetDefaultLayerFilter(Layers::MOVING),
  587. { },
  588. { },
  589. *mTempAllocator);
  590. }
  591. // Reset ramp blocks
  592. mRampBlocksTimeLeft -= inParams.mDeltaTime;
  593. if (mRampBlocksTimeLeft < 0.0f)
  594. {
  595. for (size_t i = 0; i < mRampBlocks.size(); ++i)
  596. {
  597. mBodyInterface->SetPositionAndRotation(mRampBlocks[i], cRampBlocksStart + float(i) * cRampBlocksDelta, cRampOrientation, EActivation::Activate);
  598. mBodyInterface->SetLinearAndAngularVelocity(mRampBlocks[i], Vec3::sZero(), Vec3::sZero());
  599. }
  600. mRampBlocksTimeLeft = cRampBlocksTime;
  601. }
  602. // Call handle input after new velocities have been set to avoid frame delay
  603. HandleInput(mControlInput, mJump, mSwitchStance, inParams.mDeltaTime);
  604. }
  605. void CharacterBaseTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  606. {
  607. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  608. UIElement *scene_name = inUI->CreateMenu();
  609. for (uint i = 0; i < size(sScenes); ++i)
  610. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
  611. inUI->ShowMenu(scene_name);
  612. });
  613. inUI->CreateTextButton(inSubMenu, "Character Movement Settings", [this, inUI]() {
  614. UIElement *movement_settings = inUI->CreateMenu();
  615. inUI->CreateCheckBox(movement_settings, "Control Movement During Jump", sControlMovementDuringJump, [](UICheckBox::EState inState) { sControlMovementDuringJump = inState == UICheckBox::STATE_CHECKED; });
  616. inUI->CreateSlider(movement_settings, "Character Speed", sCharacterSpeed, 0.1f, 10.0f, 0.1f, [](float inValue) { sCharacterSpeed = inValue; });
  617. inUI->CreateSlider(movement_settings, "Character Jump Speed", sJumpSpeed, 0.1f, 10.0f, 0.1f, [](float inValue) { sJumpSpeed = inValue; });
  618. AddCharacterMovementSettings(inUI, movement_settings);
  619. inUI->ShowMenu(movement_settings);
  620. });
  621. inUI->CreateTextButton(inSubMenu, "Configuration Settings", [this, inUI]() {
  622. UIElement *configuration_settings = inUI->CreateMenu();
  623. inUI->CreateComboBox(configuration_settings, "Shape Type", { "Capsule", "Cylinder", "Box" }, (int)sShapeType, [](int inItem) { sShapeType = (EType)inItem; });
  624. AddConfigurationSettings(inUI, configuration_settings);
  625. inUI->CreateTextButton(configuration_settings, "Accept Changes", [this]() { RestartTest(); });
  626. inUI->ShowMenu(configuration_settings);
  627. });
  628. }
  629. void CharacterBaseTest::GetInitialCamera(CameraState& ioState) const
  630. {
  631. // This will become the local space offset, look down the x axis and slightly down
  632. ioState.mPos = RVec3::sZero();
  633. ioState.mForward = Vec3(10.0f, -2.0f, 0).Normalized();
  634. }
  635. RMat44 CharacterBaseTest::GetCameraPivot(float inCameraHeading, float inCameraPitch) const
  636. {
  637. // Pivot is center of character + distance behind based on the heading and pitch of the camera
  638. Vec3 fwd = Vec3(Cos(inCameraPitch) * Cos(inCameraHeading), Sin(inCameraPitch), Cos(inCameraPitch) * Sin(inCameraHeading));
  639. return RMat44::sTranslation(mCameraPivot + Vec3(0, cCharacterHeightStanding + cCharacterRadiusStanding, 0) - 5.0f * fwd);
  640. }
  641. void CharacterBaseTest::SaveState(StateRecorder &inStream) const
  642. {
  643. inStream.Write(mTime);
  644. inStream.Write(mRampBlocksTimeLeft);
  645. inStream.Write(mReversingVerticallyMovingVelocity);
  646. if (mAnimatedCharacterVirtual != nullptr)
  647. mAnimatedCharacterVirtual->SaveState(inStream);
  648. if (mAnimatedCharacterVirtualWithInnerBody != nullptr)
  649. mAnimatedCharacterVirtualWithInnerBody->SaveState(inStream);
  650. }
  651. void CharacterBaseTest::RestoreState(StateRecorder &inStream)
  652. {
  653. inStream.Read(mTime);
  654. inStream.Read(mRampBlocksTimeLeft);
  655. inStream.Read(mReversingVerticallyMovingVelocity);
  656. if (mAnimatedCharacterVirtual != nullptr)
  657. mAnimatedCharacterVirtual->RestoreState(inStream);
  658. if (mAnimatedCharacterVirtualWithInnerBody != nullptr)
  659. mAnimatedCharacterVirtualWithInnerBody->RestoreState(inStream);
  660. }
  661. void CharacterBaseTest::SaveInputState(StateRecorder &inStream) const
  662. {
  663. inStream.Write(mControlInput);
  664. inStream.Write(mJump);
  665. inStream.Write(mSwitchStance);
  666. }
  667. void CharacterBaseTest::RestoreInputState(StateRecorder &inStream)
  668. {
  669. inStream.Read(mControlInput);
  670. inStream.Read(mJump);
  671. inStream.Read(mSwitchStance);
  672. }
  673. void CharacterBaseTest::DrawCharacterState(const CharacterBase *inCharacter, RMat44Arg inCharacterTransform, Vec3Arg inCharacterVelocity)
  674. {
  675. // Draw current location
  676. // 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)
  677. mDebugRenderer->DrawCoordinateSystem(inCharacterTransform, 0.1f);
  678. // Draw the state of the ground contact
  679. CharacterBase::EGroundState ground_state = inCharacter->GetGroundState();
  680. if (ground_state != CharacterBase::EGroundState::InAir)
  681. {
  682. RVec3 ground_position = inCharacter->GetGroundPosition();
  683. Vec3 ground_normal = inCharacter->GetGroundNormal();
  684. Vec3 ground_velocity = inCharacter->GetGroundVelocity();
  685. // Draw ground position
  686. mDebugRenderer->DrawMarker(ground_position, Color::sRed, 0.1f);
  687. mDebugRenderer->DrawArrow(ground_position, ground_position + 2.0f * ground_normal, Color::sGreen, 0.1f);
  688. // Draw ground velocity
  689. if (!ground_velocity.IsNearZero())
  690. mDebugRenderer->DrawArrow(ground_position, ground_position + ground_velocity, Color::sBlue, 0.1f);
  691. }
  692. // Draw provided character velocity
  693. if (!inCharacterVelocity.IsNearZero())
  694. mDebugRenderer->DrawArrow(inCharacterTransform.GetTranslation(), inCharacterTransform.GetTranslation() + inCharacterVelocity, Color::sYellow, 0.1f);
  695. // Draw text info
  696. const PhysicsMaterial *ground_material = inCharacter->GetGroundMaterial();
  697. Vec3 horizontal_velocity = inCharacterVelocity;
  698. horizontal_velocity.SetY(0);
  699. mDebugRenderer->DrawText3D(inCharacterTransform.GetTranslation(), StringFormat("State: %s\nMat: %s\nHorizontal Vel: %.1f m/s\nVertical Vel: %.1f m/s", CharacterBase::sToString(ground_state), ground_material->GetDebugName(), (double)horizontal_velocity.Length(), (double)inCharacterVelocity.GetY()), Color::sWhite, 0.25f);
  700. }