CharacterBaseTest.cpp 38 KB

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