CharacterVirtualTests.cpp 23 KB

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