CharacterVirtualTests.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include "PhysicsTestContext.h"
  5. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  6. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  7. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  8. #include <Jolt/Physics/Character/CharacterVirtual.h>
  9. #include "Layers.h"
  10. TEST_SUITE("CharacterVirtualTests")
  11. {
  12. class Character : public CharacterContactListener
  13. {
  14. public:
  15. // Construct
  16. Character(PhysicsTestContext &ioContext) : mContext(ioContext) { }
  17. // Create the character
  18. void Create()
  19. {
  20. // Create capsule
  21. Ref<Shape> capsule = new CapsuleShape(0.5f * mHeightStanding, mRadiusStanding);
  22. mCharacterSettings.mShape = RotatedTranslatedShapeSettings(Vec3(0, 0.5f * mHeightStanding + mRadiusStanding, 0), Quat::sIdentity(), capsule).Create().Get();
  23. // Configure supporting volume
  24. mCharacterSettings.mSupportingVolume = Plane(Vec3::sAxisY(), -mHeightStanding); // Accept contacts that touch the lower sphere of the capsule
  25. // Create character
  26. mCharacter = new CharacterVirtual(&mCharacterSettings, mInitialPosition, Quat::sIdentity(), mContext.GetSystem());
  27. mCharacter->SetListener(this);
  28. }
  29. // Step the character and the world
  30. void Step()
  31. {
  32. // Step the world
  33. mContext.SimulateSingleStep();
  34. // Determine new basic velocity
  35. Vec3 current_vertical_velocity = Vec3(0, mCharacter->GetLinearVelocity().GetY(), 0);
  36. Vec3 ground_velocity = mCharacter->GetGroundVelocity();
  37. Vec3 new_velocity;
  38. if (mCharacter->GetGroundState() == CharacterVirtual::EGroundState::OnGround // If on ground
  39. && (current_vertical_velocity.GetY() - ground_velocity.GetY()) < 0.1f) // And not moving away from ground
  40. {
  41. // Assume velocity of ground when on ground
  42. new_velocity = ground_velocity;
  43. // Jump
  44. new_velocity += Vec3(0, mJumpSpeed, 0);
  45. mJumpSpeed = 0.0f;
  46. }
  47. else
  48. new_velocity = current_vertical_velocity;
  49. // Gravity
  50. PhysicsSystem *system = mContext.GetSystem();
  51. float delta_time = mContext.GetDeltaTime();
  52. new_velocity += system->GetGravity() * delta_time;
  53. // Player input
  54. new_velocity += mHorizontalSpeed;
  55. // Update character velocity
  56. mCharacter->SetLinearVelocity(new_velocity);
  57. RVec3 start_pos = mCharacter->GetPosition();
  58. // Update the character position
  59. TempAllocatorMalloc allocator;
  60. mCharacter->ExtendedUpdate(delta_time,
  61. system->GetGravity(),
  62. mUpdateSettings,
  63. system->GetDefaultBroadPhaseLayerFilter(Layers::MOVING),
  64. system->GetDefaultLayerFilter(Layers::MOVING),
  65. { },
  66. { },
  67. allocator);
  68. // Calculate effective velocity in this step
  69. mEffectiveVelocity = Vec3(mCharacter->GetPosition() - start_pos) / delta_time;
  70. }
  71. // Simulate a longer period of time
  72. void Simulate(float inTime)
  73. {
  74. int num_steps = (int)round(inTime / mContext.GetDeltaTime());
  75. for (int step = 0; step < num_steps; ++step)
  76. Step();
  77. }
  78. // Configuration
  79. RVec3 mInitialPosition = RVec3::sZero();
  80. float mHeightStanding = 1.35f;
  81. float mRadiusStanding = 0.3f;
  82. CharacterVirtualSettings mCharacterSettings;
  83. CharacterVirtual::ExtendedUpdateSettings mUpdateSettings;
  84. // Character movement settings (update to control the movement of the character)
  85. Vec3 mHorizontalSpeed = Vec3::sZero();
  86. float mJumpSpeed = 0.0f; // Character will jump when not 0, will auto reset
  87. // The character
  88. Ref<CharacterVirtual> mCharacter;
  89. // Calculated effective velocity after a step
  90. Vec3 mEffectiveVelocity = Vec3::sZero();
  91. private:
  92. // CharacterContactListener callback
  93. virtual void OnContactSolve(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial, Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity) override
  94. {
  95. // Don't allow sliding if the character doesn't want to move
  96. if (mHorizontalSpeed.IsNearZero() && inContactVelocity.IsNearZero() && !inCharacter->IsSlopeTooSteep(inContactNormal))
  97. ioNewCharacterVelocity = Vec3::sZero();
  98. }
  99. PhysicsTestContext & mContext;
  100. };
  101. TEST_CASE("TestFallingAndJumping")
  102. {
  103. // Create floor
  104. PhysicsTestContext c;
  105. c.CreateFloor();
  106. // Create character
  107. Character character(c);
  108. character.mInitialPosition = RVec3(0, 2, 0);
  109. character.Create();
  110. // After 1 step we should still be in air
  111. character.Step();
  112. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::InAir);
  113. // After some time we should be on the floor
  114. character.Simulate(1.0f);
  115. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  116. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), RVec3::sZero());
  117. CHECK_APPROX_EQUAL(character.mEffectiveVelocity, Vec3::sZero());
  118. // Jump
  119. character.mJumpSpeed = 1.0f;
  120. character.Step();
  121. Vec3 velocity(0, 1.0f + c.GetDeltaTime() * c.GetSystem()->GetGravity().GetY(), 0);
  122. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), RVec3(velocity * c.GetDeltaTime()));
  123. CHECK_APPROX_EQUAL(character.mEffectiveVelocity, velocity);
  124. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::InAir);
  125. // After some time we should be on the floor again
  126. character.Simulate(1.0f);
  127. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  128. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), RVec3::sZero());
  129. CHECK_APPROX_EQUAL(character.mEffectiveVelocity, Vec3::sZero());
  130. }
  131. TEST_CASE("TestMovingOnSlope")
  132. {
  133. constexpr float cFloorHalfHeight = 1.0f;
  134. constexpr float cMovementTime = 1.5f;
  135. // Iterate various slope angles
  136. for (float slope_angle = DegreesToRadians(5.0f); slope_angle < DegreesToRadians(85.0f); slope_angle += DegreesToRadians(10.0f))
  137. {
  138. // Create sloped floor
  139. PhysicsTestContext c;
  140. Quat slope_rotation = Quat::sRotation(Vec3::sAxisZ(), slope_angle);
  141. c.CreateBox(RVec3::sZero(), slope_rotation, EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(100.0f, cFloorHalfHeight, 100.0f));
  142. // Create character so that it is touching the slope
  143. Character character(c);
  144. float radius_and_padding = character.mRadiusStanding + character.mCharacterSettings.mCharacterPadding;
  145. character.mInitialPosition = RVec3(0, (radius_and_padding + cFloorHalfHeight) / Cos(slope_angle) - radius_and_padding, 0);
  146. character.Create();
  147. // Determine if the slope is too steep for the character
  148. bool too_steep = slope_angle > character.mCharacterSettings.mMaxSlopeAngle;
  149. CharacterBase::EGroundState expected_ground_state = (too_steep? CharacterBase::EGroundState::OnSteepGround : CharacterBase::EGroundState::OnGround);
  150. Vec3 gravity = c.GetSystem()->GetGravity();
  151. float time_step = c.GetDeltaTime();
  152. Vec3 slope_normal = slope_rotation.RotateAxisY();
  153. // Calculate expected position after 1 time step
  154. RVec3 position_after_1_step = character.mInitialPosition;
  155. if (too_steep)
  156. {
  157. // Apply 1 frame of gravity and cancel movement in the slope normal direction
  158. Vec3 velocity = gravity * time_step;
  159. velocity -= velocity.Dot(slope_normal) * slope_normal;
  160. position_after_1_step += velocity * time_step;
  161. }
  162. // After 1 step we should be on the slope
  163. character.Step();
  164. CHECK(character.mCharacter->GetGroundState() == expected_ground_state);
  165. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), position_after_1_step);
  166. // Cancel any velocity to make the calculation below easier (otherwise we have to take gravity for 1 time step into account)
  167. character.mCharacter->SetLinearVelocity(Vec3::sZero());
  168. RVec3 start_pos = character.mCharacter->GetPosition();
  169. // Start moving in X direction
  170. character.mHorizontalSpeed = Vec3(2.0f, 0, 0);
  171. character.Simulate(cMovementTime);
  172. CHECK(character.mCharacter->GetGroundState() == expected_ground_state);
  173. // Calculate resulting translation
  174. Vec3 translation = Vec3(character.mCharacter->GetPosition() - start_pos);
  175. // Calculate expected translation
  176. Vec3 expected_translation;
  177. if (too_steep)
  178. {
  179. // If too steep, we're just falling. Integrate using an Euler integrator.
  180. Vec3 velocity = Vec3::sZero();
  181. expected_translation = Vec3::sZero();
  182. int num_steps = (int)round(cMovementTime / time_step);
  183. for (int i = 0; i < num_steps; ++i)
  184. {
  185. velocity += gravity * time_step;
  186. expected_translation += velocity * time_step;
  187. }
  188. }
  189. else
  190. {
  191. // Every frame we apply 1 delta time * gravity which gets reset on the next update, add this to the horizontal speed
  192. expected_translation = (character.mHorizontalSpeed + gravity * time_step) * cMovementTime;
  193. }
  194. // Cancel movement in slope direction
  195. expected_translation -= expected_translation.Dot(slope_normal) * slope_normal;
  196. // Check that we travelled the right amount
  197. CHECK_APPROX_EQUAL(translation, expected_translation, 1.0e-4f);
  198. }
  199. }
  200. TEST_CASE("TestStickToFloor")
  201. {
  202. constexpr float cFloorHalfHeight = 1.0f;
  203. constexpr float cSlopeAngle = DegreesToRadians(45.0f);
  204. constexpr float cMovementTime = 1.5f;
  205. for (int mode = 0; mode < 2; ++mode)
  206. {
  207. // If this run is with 'stick to floor' enabled
  208. bool stick_to_floor = mode == 0;
  209. // Create sloped floor
  210. PhysicsTestContext c;
  211. Quat slope_rotation = Quat::sRotation(Vec3::sAxisZ(), cSlopeAngle);
  212. c.CreateBox(RVec3::sZero(), slope_rotation, EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(100.0f, cFloorHalfHeight, 100.0f));
  213. // Create character so that it is touching the slope
  214. Character character(c);
  215. float radius_and_padding = character.mRadiusStanding + character.mCharacterSettings.mCharacterPadding;
  216. character.mInitialPosition = RVec3(0, (radius_and_padding + cFloorHalfHeight) / Cos(cSlopeAngle) - radius_and_padding, 0);
  217. character.mUpdateSettings.mStickToFloorStepDown = stick_to_floor? Vec3(0, -0.5f, 0) : Vec3::sZero();
  218. character.Create();
  219. // After 1 step we should be on the slope
  220. character.Step();
  221. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  222. // Cancel any velocity to make the calculation below easier (otherwise we have to take gravity for 1 time step into account)
  223. character.mCharacter->SetLinearVelocity(Vec3::sZero());
  224. RVec3 start_pos = character.mCharacter->GetPosition();
  225. // Start moving down the slope at a speed high enough so that gravity will not keep us on the floor
  226. character.mHorizontalSpeed = Vec3(-10.0f, 0, 0);
  227. character.Simulate(cMovementTime);
  228. CHECK(character.mCharacter->GetGroundState() == (stick_to_floor? CharacterBase::EGroundState::OnGround : CharacterBase::EGroundState::InAir));
  229. // Calculate resulting translation
  230. Vec3 translation = Vec3(character.mCharacter->GetPosition() - start_pos);
  231. // Calculate expected translation
  232. Vec3 expected_translation;
  233. if (stick_to_floor)
  234. {
  235. // We should stick to the floor, so the vertical translation follows the slope perfectly
  236. expected_translation = character.mHorizontalSpeed * cMovementTime;
  237. expected_translation.SetY(expected_translation.GetX() * Tan(cSlopeAngle));
  238. }
  239. else
  240. {
  241. Vec3 gravity = c.GetSystem()->GetGravity();
  242. float time_step = c.GetDeltaTime();
  243. // If too steep, we're just falling. Integrate using an Euler integrator.
  244. Vec3 velocity = character.mHorizontalSpeed;
  245. expected_translation = Vec3::sZero();
  246. int num_steps = (int)round(cMovementTime / time_step);
  247. for (int i = 0; i < num_steps; ++i)
  248. {
  249. velocity += gravity * time_step;
  250. expected_translation += velocity * time_step;
  251. }
  252. }
  253. // Check that we travelled the right amount
  254. CHECK_APPROX_EQUAL(translation, expected_translation, 1.0e-4f);
  255. }
  256. }
  257. TEST_CASE("TestWalkStairs")
  258. {
  259. const float cStepHeight = 0.3f;
  260. const int cNumSteps = 10;
  261. // Create stairs from triangles
  262. TriangleList triangles;
  263. for (int i = 0; i < cNumSteps; ++i)
  264. {
  265. // Start of step
  266. Vec3 base(0, cStepHeight * i, cStepHeight * i);
  267. // Left side
  268. Vec3 b1 = base + Vec3(2.0f, 0, 0);
  269. Vec3 s1 = b1 + Vec3(0, cStepHeight, 0);
  270. Vec3 p1 = s1 + Vec3(0, 0, cStepHeight);
  271. // Right side
  272. Vec3 width(-4.0f, 0, 0);
  273. Vec3 b2 = b1 + width;
  274. Vec3 s2 = s1 + width;
  275. Vec3 p2 = p1 + width;
  276. triangles.push_back(Triangle(s1, b1, s2));
  277. triangles.push_back(Triangle(b1, b2, s2));
  278. triangles.push_back(Triangle(s1, p2, p1));
  279. triangles.push_back(Triangle(s1, s2, p2));
  280. }
  281. MeshShapeSettings mesh(triangles);
  282. mesh.SetEmbedded();
  283. BodyCreationSettings mesh_stairs(&mesh, RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  284. // Stair stepping is very delta time sensitive, so test various update frequencies
  285. float frequencies[] = { 60.0f, 120.0f, 240.0f, 360.0f };
  286. for (float frequency : frequencies)
  287. {
  288. float time_step = 1.0f / frequency;
  289. PhysicsTestContext c(time_step);
  290. c.CreateFloor();
  291. c.GetBodyInterface().CreateAndAddBody(mesh_stairs, EActivation::DontActivate);
  292. // Create character so that it is touching the slope
  293. Character character(c);
  294. character.mInitialPosition = RVec3(0, 0, -2.0f); // Start in front of the stairs
  295. character.mUpdateSettings.mWalkStairsStepUp = Vec3::sZero(); // No stair walking
  296. character.Create();
  297. // Start moving towards the stairs
  298. character.mHorizontalSpeed = Vec3(0, 0, 4.0f);
  299. character.Simulate(1.0f);
  300. // We should have gotten stuck at the start of the stairs (can't move up)
  301. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  302. float radius_and_padding = character.mRadiusStanding + character.mCharacterSettings.mCharacterPadding;
  303. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), RVec3(0, 0, -radius_and_padding), 1.0e-2f);
  304. // Enable stair walking
  305. character.mUpdateSettings.mWalkStairsStepUp = Vec3(0, 0.4f, 0);
  306. // Calculate time it should take to move up the stairs at constant speed
  307. float movement_time = (cNumSteps * cStepHeight + radius_and_padding) / character.mHorizontalSpeed.GetZ();
  308. int max_steps = int(1.5f * round(movement_time / time_step)); // In practise there is a bit of slowdown while stair stepping, so add a bit of slack
  309. // Step until we reach the top of the stairs
  310. RVec3 last_position = character.mCharacter->GetPosition();
  311. bool reached_goal = false;
  312. for (int i = 0; i < max_steps; ++i)
  313. {
  314. character.Step();
  315. // We should always be on the floor during stair stepping
  316. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  317. // Check position progression
  318. RVec3 position = character.mCharacter->GetPosition();
  319. CHECK_APPROX_EQUAL(position.GetX(), 0); // No movement in X
  320. CHECK(position.GetZ() > last_position.GetZ()); // Always moving forward
  321. CHECK(position.GetZ() < cNumSteps * cStepHeight); // No movement beyond stairs
  322. if (position.GetY() > cNumSteps * cStepHeight - 1.0e-3f)
  323. {
  324. reached_goal = true;
  325. break;
  326. }
  327. last_position = position;
  328. }
  329. CHECK(reached_goal);
  330. }
  331. }
  332. TEST_CASE("TestRotatingPlatform")
  333. {
  334. constexpr float cFloorHalfHeight = 1.0f;
  335. constexpr float cFloorHalfWidth = 10.0f;
  336. constexpr float cCharacterPosition = 0.9f * cFloorHalfWidth;
  337. constexpr float cAngularVelocity = 2.0f * JPH_PI;
  338. PhysicsTestContext c;
  339. // Create box
  340. Body &box = c.CreateBox(RVec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3(cFloorHalfWidth, cFloorHalfHeight, cFloorHalfWidth));
  341. box.SetAllowSleeping(false);
  342. // Create character so that it is touching the box at the
  343. Character character(c);
  344. character.mInitialPosition = RVec3(cCharacterPosition, cFloorHalfHeight, 0);
  345. character.Create();
  346. // Step to ensure the character is on the box
  347. character.Step();
  348. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  349. // Set the box to rotate a full circle per second
  350. box.SetAngularVelocity(Vec3(0, cAngularVelocity, 0));
  351. // Rotate and check that character stays on the box
  352. for (int t = 0; t < 60; ++t)
  353. {
  354. character.Step();
  355. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  356. // Note that the character moves according to the ground velocity and the ground velocity is updated at the end of the step
  357. // so the character is always 1 time step behind the platform. This is why we use t and not t + 1 to calculate the expected position.
  358. RVec3 expected_position = RMat44::sRotation(Quat::sRotation(Vec3::sAxisY(), float(t) * c.GetDeltaTime() * cAngularVelocity)) * character.mInitialPosition;
  359. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), expected_position, 1.0e-4f);
  360. }
  361. }
  362. TEST_CASE("TestMovingPlatformUp")
  363. {
  364. constexpr float cFloorHalfHeight = 1.0f;
  365. constexpr float cFloorHalfWidth = 10.0f;
  366. constexpr float cLinearVelocity = 0.5f;
  367. PhysicsTestContext c;
  368. // Create box
  369. Body &box = c.CreateBox(RVec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3(cFloorHalfWidth, cFloorHalfHeight, cFloorHalfWidth));
  370. box.SetAllowSleeping(false);
  371. // Create character so that it is touching the box at the
  372. Character character(c);
  373. character.mInitialPosition = RVec3(0, cFloorHalfHeight, 0);
  374. character.Create();
  375. // Step to ensure the character is on the box
  376. character.Step();
  377. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  378. // Set the box to move up
  379. box.SetLinearVelocity(Vec3(0, cLinearVelocity, 0));
  380. // Check that character stays on the box
  381. for (int t = 0; t < 60; ++t)
  382. {
  383. character.Step();
  384. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  385. RVec3 expected_position = box.GetPosition() + character.mInitialPosition;
  386. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), expected_position, 1.0e-2f);
  387. }
  388. // Stop box
  389. box.SetLinearVelocity(Vec3::sZero());
  390. character.Simulate(0.5f);
  391. // Set the box to move down
  392. box.SetLinearVelocity(Vec3(0, -cLinearVelocity, 0));
  393. // Check that character stays on the box
  394. for (int t = 0; t < 60; ++t)
  395. {
  396. character.Step();
  397. CHECK(character.mCharacter->GetGroundState() == CharacterBase::EGroundState::OnGround);
  398. RVec3 expected_position = box.GetPosition() + character.mInitialPosition;
  399. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), expected_position, 1.0e-2f);
  400. }
  401. }
  402. TEST_CASE("TestContactPointLimit")
  403. {
  404. PhysicsTestContext ctx;
  405. Body &floor = ctx.CreateFloor();
  406. // Create character at the origin
  407. Character character(ctx);
  408. character.mInitialPosition = RVec3(0, 1, 0);
  409. character.mUpdateSettings.mStickToFloorStepDown = Vec3::sZero();
  410. character.mUpdateSettings.mWalkStairsStepUp = Vec3::sZero();
  411. character.Create();
  412. // Radius including pading
  413. const float character_radius = character.mRadiusStanding + character.mCharacterSettings.mCharacterPadding;
  414. // Create a half cylinder with caps for testing contact point limit
  415. VertexList vertices;
  416. IndexedTriangleList triangles;
  417. // The half cylinder
  418. const int cPosSegments = 2;
  419. const int cAngleSegments = 768;
  420. const float cCylinderLength = 2.0f;
  421. for (int pos = 0; pos < cPosSegments; ++pos)
  422. for (int angle = 0; angle < cAngleSegments; ++angle)
  423. {
  424. uint32 start = (uint32)vertices.size();
  425. float radius = character_radius + 0.01f;
  426. float angle_rad = (-0.5f + float(angle) / cAngleSegments) * JPH_PI;
  427. float s = Sin(angle_rad);
  428. float c = Cos(angle_rad);
  429. float x = cCylinderLength * (-0.5f + float(pos) / (cPosSegments - 1));
  430. float y = angle == 0 || angle == cAngleSegments - 1? 0.5f : (1.0f - c) * radius;
  431. float z = s * radius;
  432. vertices.push_back(Float3(x, y, z));
  433. if (pos > 0 && angle > 0)
  434. {
  435. triangles.push_back(IndexedTriangle(start, start - 1, start - cAngleSegments));
  436. triangles.push_back(IndexedTriangle(start - 1, start - cAngleSegments - 1, start - cAngleSegments));
  437. }
  438. }
  439. // Add end caps
  440. uint32 end = cAngleSegments * (cPosSegments - 1);
  441. for (int angle = 0; angle < cAngleSegments - 1; ++angle)
  442. {
  443. triangles.push_back(IndexedTriangle(0, angle + 1, angle));
  444. triangles.push_back(IndexedTriangle(end, end + angle, end + angle + 1));
  445. }
  446. // Create test body
  447. MeshShapeSettings mesh(vertices, triangles);
  448. mesh.SetEmbedded();
  449. BodyCreationSettings mesh_cylinder(&mesh, character.mInitialPosition, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  450. BodyID cylinder_id = ctx.GetBodyInterface().CreateAndAddBody(mesh_cylinder, EActivation::DontActivate);
  451. // End positions that can be reached by character
  452. RVec3 pos_end(0.5_r * cCylinderLength - character_radius, 1, 0);
  453. RVec3 neg_end(-0.5_r * cCylinderLength + character_radius, 1, 0);
  454. // Move towards positive cap and test if we hit the end
  455. character.mHorizontalSpeed = Vec3(cCylinderLength, 0, 0);
  456. for (int t = 0; t < 60; ++t)
  457. {
  458. character.Step();
  459. CHECK(character.mCharacter->GetMaxHitsExceeded());
  460. CHECK(character.mCharacter->GetActiveContacts().size() < character.mCharacter->GetMaxNumHits());
  461. CHECK(character.mCharacter->GetGroundBodyID() == cylinder_id);
  462. CHECK(character.mCharacter->GetGroundNormal().Dot(Vec3::sAxisY()) > 0.999f);
  463. }
  464. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), pos_end, 1.0e-4f);
  465. // Move towards negative cap and test if we hit the end
  466. character.mHorizontalSpeed = Vec3(-cCylinderLength, 0, 0);
  467. for (int t = 0; t < 60; ++t)
  468. {
  469. character.Step();
  470. CHECK(character.mCharacter->GetMaxHitsExceeded());
  471. CHECK(character.mCharacter->GetActiveContacts().size() < character.mCharacter->GetMaxNumHits());
  472. CHECK(character.mCharacter->GetGroundBodyID() == cylinder_id);
  473. CHECK(character.mCharacter->GetGroundNormal().Dot(Vec3::sAxisY()) > 0.999f);
  474. }
  475. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), neg_end, 1.0e-4f);
  476. // Turn off contact point reduction
  477. character.mCharacter->SetHitReductionCosMaxAngle(-1.0f);
  478. // Move towards positive cap and test that we did not reach the end
  479. character.mHorizontalSpeed = Vec3(cCylinderLength, 0, 0);
  480. for (int t = 0; t < 60; ++t)
  481. {
  482. character.Step();
  483. CHECK(character.mCharacter->GetMaxHitsExceeded());
  484. CHECK(character.mCharacter->GetActiveContacts().size() == character.mCharacter->GetMaxNumHits());
  485. }
  486. RVec3 cur_pos = character.mCharacter->GetPosition();
  487. CHECK((pos_end - cur_pos).Length() > 0.01_r);
  488. // Move towards negative cap and test that we got stuck
  489. character.mHorizontalSpeed = Vec3(-cCylinderLength, 0, 0);
  490. for (int t = 0; t < 60; ++t)
  491. {
  492. character.Step();
  493. CHECK(character.mCharacter->GetMaxHitsExceeded());
  494. CHECK(character.mCharacter->GetActiveContacts().size() == character.mCharacter->GetMaxNumHits());
  495. }
  496. CHECK(cur_pos.IsClose(character.mCharacter->GetPosition(), 1.0e-6f));
  497. // Now teleport the character next to the half cylinder
  498. character.mCharacter->SetPosition(RVec3(0, 0, 1));
  499. // Move in positive X and check that we did not exceed max hits and that we were able to move unimpeded
  500. character.mHorizontalSpeed = Vec3(cCylinderLength, 0, 0);
  501. for (int t = 0; t < 60; ++t)
  502. {
  503. character.Step();
  504. CHECK(!character.mCharacter->GetMaxHitsExceeded());
  505. CHECK(character.mCharacter->GetActiveContacts().size() == 1); // We should only hit the floor
  506. CHECK(character.mCharacter->GetGroundBodyID() == floor.GetID());
  507. CHECK(character.mCharacter->GetGroundNormal().Dot(Vec3::sAxisY()) > 0.999f);
  508. }
  509. CHECK_APPROX_EQUAL(character.mCharacter->GetPosition(), RVec3(cCylinderLength, 0, 1), 1.0e-4f);
  510. }
  511. }