CharacterVirtualTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/CharacterVirtualTest.h>
  6. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  7. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  8. #include <Layers.h>
  9. #include <Utils/Log.h>
  10. #include <Renderer/DebugRendererImp.h>
  11. #include <Application/DebugUI.h>
  12. //#define CHARACTER_TRACE_CONTACTS
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(CharacterVirtualTest)
  14. {
  15. JPH_ADD_BASE_CLASS(CharacterVirtualTest, CharacterBaseTest)
  16. }
  17. void CharacterVirtualTest::Initialize()
  18. {
  19. CharacterBaseTest::Initialize();
  20. // Create 'player' character
  21. Ref<CharacterVirtualSettings> settings = new CharacterVirtualSettings();
  22. settings->mMaxSlopeAngle = sMaxSlopeAngle;
  23. settings->mMaxStrength = sMaxStrength;
  24. settings->mShape = mStandingShape;
  25. settings->mBackFaceMode = sBackFaceMode;
  26. settings->mCharacterPadding = sCharacterPadding;
  27. settings->mPenetrationRecoverySpeed = sPenetrationRecoverySpeed;
  28. settings->mPredictiveContactDistance = sPredictiveContactDistance;
  29. settings->mSupportingVolume = Plane(Vec3::sAxisY(), -cCharacterRadiusStanding); // Accept contacts that touch the lower sphere of the capsule
  30. settings->mEnhancedInternalEdgeRemoval = sEnhancedInternalEdgeRemoval;
  31. settings->mInnerBodyShape = sCreateInnerBody? mInnerStandingShape : nullptr;
  32. settings->mInnerBodyLayer = Layers::MOVING;
  33. mCharacter = new CharacterVirtual(settings, RVec3::sZero(), Quat::sIdentity(), 0, mPhysicsSystem);
  34. mCharacter->SetCharacterVsCharacterCollision(&mCharacterVsCharacterCollision);
  35. mCharacterVsCharacterCollision.Add(mCharacter);
  36. // Install contact listener for all characters
  37. for (CharacterVirtual *character : mCharacterVsCharacterCollision.mCharacters)
  38. character->SetListener(this);
  39. }
  40. void CharacterVirtualTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  41. {
  42. CharacterBaseTest::PrePhysicsUpdate(inParams);
  43. // Draw character pre update (the sim is also drawn pre update)
  44. RMat44 com = mCharacter->GetCenterOfMassTransform();
  45. RMat44 world_transform = mCharacter->GetWorldTransform();
  46. #ifdef JPH_DEBUG_RENDERER
  47. mCharacter->GetShape()->Draw(mDebugRenderer, com, Vec3::sOne(), Color::sGreen, false, true);
  48. #endif // JPH_DEBUG_RENDERER
  49. // Draw shape including padding (only implemented for capsules right now)
  50. if (static_cast<const RotatedTranslatedShape *>(mCharacter->GetShape())->GetInnerShape()->GetSubType() == EShapeSubType::Capsule)
  51. {
  52. if (mCharacter->GetShape() == mStandingShape)
  53. mDebugRenderer->DrawCapsule(com, 0.5f * cCharacterHeightStanding, cCharacterRadiusStanding + mCharacter->GetCharacterPadding(), Color::sGrey, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  54. else
  55. mDebugRenderer->DrawCapsule(com, 0.5f * cCharacterHeightCrouching, cCharacterRadiusCrouching + mCharacter->GetCharacterPadding(), Color::sGrey, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  56. }
  57. // Remember old position
  58. RVec3 old_position = mCharacter->GetPosition();
  59. // Settings for our update function
  60. CharacterVirtual::ExtendedUpdateSettings update_settings;
  61. if (!sEnableStickToFloor)
  62. update_settings.mStickToFloorStepDown = Vec3::sZero();
  63. else
  64. update_settings.mStickToFloorStepDown = -mCharacter->GetUp() * update_settings.mStickToFloorStepDown.Length();
  65. if (!sEnableWalkStairs)
  66. update_settings.mWalkStairsStepUp = Vec3::sZero();
  67. else
  68. update_settings.mWalkStairsStepUp = mCharacter->GetUp() * update_settings.mWalkStairsStepUp.Length();
  69. // Update the character position
  70. mCharacter->ExtendedUpdate(inParams.mDeltaTime,
  71. -mCharacter->GetUp() * mPhysicsSystem->GetGravity().Length(),
  72. update_settings,
  73. mPhysicsSystem->GetDefaultBroadPhaseLayerFilter(Layers::MOVING),
  74. mPhysicsSystem->GetDefaultLayerFilter(Layers::MOVING),
  75. { },
  76. { },
  77. *mTempAllocator);
  78. #ifdef JPH_ENABLE_ASSERTS
  79. // Validate that our contact list is in sync with that of the character
  80. uint num_contacts = 0;
  81. for (const CharacterVirtual::Contact &c : mCharacter->GetActiveContacts())
  82. if (c.mHadCollision)
  83. {
  84. JPH_ASSERT(std::find(mActiveContacts.begin(), mActiveContacts.end(), c) != mActiveContacts.end());
  85. num_contacts++;
  86. }
  87. JPH_ASSERT(num_contacts == mActiveContacts.size());
  88. #endif
  89. // Calculate effective velocity
  90. RVec3 new_position = mCharacter->GetPosition();
  91. Vec3 velocity = Vec3(new_position - old_position) / inParams.mDeltaTime;
  92. // Draw state of character
  93. DrawCharacterState(mCharacter, world_transform, velocity);
  94. // Draw labels on ramp blocks
  95. for (size_t i = 0; i < mRampBlocks.size(); ++i)
  96. mDebugRenderer->DrawText3D(mBodyInterface->GetPosition(mRampBlocks[i]), StringFormat("PushesPlayer: %s\nPushable: %s", (i & 1) != 0? "True" : "False", (i & 2) != 0? "True" : "False"), Color::sWhite, 0.25f);
  97. }
  98. void CharacterVirtualTest::HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime)
  99. {
  100. bool player_controls_horizontal_velocity = sControlMovementDuringJump || mCharacter->IsSupported();
  101. if (player_controls_horizontal_velocity)
  102. {
  103. // Smooth the player input
  104. mDesiredVelocity = sEnableCharacterInertia? 0.25f * inMovementDirection * sCharacterSpeed + 0.75f * mDesiredVelocity : inMovementDirection * sCharacterSpeed;
  105. // True if the player intended to move
  106. mAllowSliding = !inMovementDirection.IsNearZero();
  107. }
  108. else
  109. {
  110. // While in air we allow sliding
  111. mAllowSliding = true;
  112. }
  113. // Update the character rotation and its up vector to match the up vector set by the user settings
  114. Quat character_up_rotation = Quat::sEulerAngles(Vec3(sUpRotationX, 0, sUpRotationZ));
  115. mCharacter->SetUp(character_up_rotation.RotateAxisY());
  116. mCharacter->SetRotation(character_up_rotation);
  117. // A cheaper way to update the character's ground velocity,
  118. // the platforms that the character is standing on may have changed velocity
  119. mCharacter->UpdateGroundVelocity();
  120. // Determine new basic velocity
  121. Vec3 current_vertical_velocity = mCharacter->GetLinearVelocity().Dot(mCharacter->GetUp()) * mCharacter->GetUp();
  122. Vec3 ground_velocity = mCharacter->GetGroundVelocity();
  123. Vec3 new_velocity;
  124. bool moving_towards_ground = (current_vertical_velocity.GetY() - ground_velocity.GetY()) < 0.1f;
  125. if (mCharacter->GetGroundState() == CharacterVirtual::EGroundState::OnGround // If on ground
  126. && (sEnableCharacterInertia?
  127. moving_towards_ground // Inertia enabled: And not moving away from ground
  128. : !mCharacter->IsSlopeTooSteep(mCharacter->GetGroundNormal()))) // Inertia disabled: And not on a slope that is too steep
  129. {
  130. // Assume velocity of ground when on ground
  131. new_velocity = ground_velocity;
  132. // Jump
  133. if (inJump && moving_towards_ground)
  134. new_velocity += sJumpSpeed * mCharacter->GetUp();
  135. }
  136. else
  137. new_velocity = current_vertical_velocity;
  138. // Gravity
  139. new_velocity += (character_up_rotation * mPhysicsSystem->GetGravity()) * inDeltaTime;
  140. if (player_controls_horizontal_velocity)
  141. {
  142. // Player input
  143. new_velocity += character_up_rotation * mDesiredVelocity;
  144. }
  145. else
  146. {
  147. // Preserve horizontal velocity
  148. Vec3 current_horizontal_velocity = mCharacter->GetLinearVelocity() - current_vertical_velocity;
  149. new_velocity += current_horizontal_velocity;
  150. }
  151. // Update character velocity
  152. mCharacter->SetLinearVelocity(new_velocity);
  153. // Stance switch
  154. if (inSwitchStance)
  155. {
  156. bool is_standing = mCharacter->GetShape() == mStandingShape;
  157. const Shape *shape = is_standing? mCrouchingShape : mStandingShape;
  158. if (mCharacter->SetShape(shape, 1.5f * mPhysicsSystem->GetPhysicsSettings().mPenetrationSlop, mPhysicsSystem->GetDefaultBroadPhaseLayerFilter(Layers::MOVING), mPhysicsSystem->GetDefaultLayerFilter(Layers::MOVING), { }, { }, *mTempAllocator))
  159. {
  160. const Shape *inner_shape = is_standing? mInnerCrouchingShape : mInnerStandingShape;
  161. mCharacter->SetInnerBodyShape(inner_shape);
  162. }
  163. }
  164. }
  165. void CharacterVirtualTest::AddCharacterMovementSettings(DebugUI* inUI, UIElement* inSubMenu)
  166. {
  167. inUI->CreateCheckBox(inSubMenu, "Enable Character Inertia", sEnableCharacterInertia, [](UICheckBox::EState inState) { sEnableCharacterInertia = inState == UICheckBox::STATE_CHECKED; });
  168. inUI->CreateCheckBox(inSubMenu, "Player Can Push Other Virtual Characters", sPlayerCanPushOtherCharacters, [](UICheckBox::EState inState) { sPlayerCanPushOtherCharacters = inState == UICheckBox::STATE_CHECKED; });
  169. inUI->CreateCheckBox(inSubMenu, "Other Virtual Characters Can Push Player", sOtherCharactersCanPushPlayer, [](UICheckBox::EState inState) { sOtherCharactersCanPushPlayer = inState == UICheckBox::STATE_CHECKED; });
  170. }
  171. void CharacterVirtualTest::AddConfigurationSettings(DebugUI *inUI, UIElement *inSubMenu)
  172. {
  173. inUI->CreateComboBox(inSubMenu, "Back Face Mode", { "Ignore", "Collide" }, (int)sBackFaceMode, [=](int inItem) { sBackFaceMode = (EBackFaceMode)inItem; });
  174. inUI->CreateSlider(inSubMenu, "Up Rotation X (degrees)", RadiansToDegrees(sUpRotationX), -90.0f, 90.0f, 1.0f, [](float inValue) { sUpRotationX = DegreesToRadians(inValue); });
  175. inUI->CreateSlider(inSubMenu, "Up Rotation Z (degrees)", RadiansToDegrees(sUpRotationZ), -90.0f, 90.0f, 1.0f, [](float inValue) { sUpRotationZ = DegreesToRadians(inValue); });
  176. inUI->CreateSlider(inSubMenu, "Max Slope Angle (degrees)", RadiansToDegrees(sMaxSlopeAngle), 0.0f, 90.0f, 1.0f, [](float inValue) { sMaxSlopeAngle = DegreesToRadians(inValue); });
  177. inUI->CreateSlider(inSubMenu, "Max Strength (N)", sMaxStrength, 0.0f, 500.0f, 1.0f, [](float inValue) { sMaxStrength = inValue; });
  178. inUI->CreateSlider(inSubMenu, "Character Padding", sCharacterPadding, 0.01f, 0.5f, 0.01f, [](float inValue) { sCharacterPadding = inValue; });
  179. inUI->CreateSlider(inSubMenu, "Penetration Recovery Speed", sPenetrationRecoverySpeed, 0.0f, 1.0f, 0.05f, [](float inValue) { sPenetrationRecoverySpeed = inValue; });
  180. inUI->CreateSlider(inSubMenu, "Predictive Contact Distance", sPredictiveContactDistance, 0.01f, 1.0f, 0.01f, [](float inValue) { sPredictiveContactDistance = inValue; });
  181. inUI->CreateCheckBox(inSubMenu, "Enable Walk Stairs", sEnableWalkStairs, [](UICheckBox::EState inState) { sEnableWalkStairs = inState == UICheckBox::STATE_CHECKED; });
  182. inUI->CreateCheckBox(inSubMenu, "Enable Stick To Floor", sEnableStickToFloor, [](UICheckBox::EState inState) { sEnableStickToFloor = inState == UICheckBox::STATE_CHECKED; });
  183. inUI->CreateCheckBox(inSubMenu, "Enhanced Internal Edge Removal", sEnhancedInternalEdgeRemoval, [](UICheckBox::EState inState) { sEnhancedInternalEdgeRemoval = inState == UICheckBox::STATE_CHECKED; });
  184. inUI->CreateCheckBox(inSubMenu, "Create Inner Body", sCreateInnerBody, [](UICheckBox::EState inState) { sCreateInnerBody = inState == UICheckBox::STATE_CHECKED; });
  185. }
  186. void CharacterVirtualTest::SaveState(StateRecorder &inStream) const
  187. {
  188. CharacterBaseTest::SaveState(inStream);
  189. mCharacter->SaveState(inStream);
  190. bool is_standing = mCharacter->GetShape() == mStandingShape;
  191. inStream.Write(is_standing);
  192. inStream.Write(mAllowSliding);
  193. inStream.Write(mDesiredVelocity);
  194. inStream.Write(mActiveContacts);
  195. }
  196. void CharacterVirtualTest::RestoreState(StateRecorder &inStream)
  197. {
  198. CharacterBaseTest::RestoreState(inStream);
  199. mCharacter->RestoreState(inStream);
  200. bool is_standing = mCharacter->GetShape() == mStandingShape; // Initialize variable for validation mode
  201. inStream.Read(is_standing);
  202. const Shape *shape = is_standing? mStandingShape : mCrouchingShape;
  203. mCharacter->SetShape(shape, FLT_MAX, { }, { }, { }, { }, *mTempAllocator);
  204. const Shape *inner_shape = is_standing? mInnerStandingShape : mInnerCrouchingShape;
  205. mCharacter->SetInnerBodyShape(inner_shape);
  206. inStream.Read(mAllowSliding);
  207. inStream.Read(mDesiredVelocity);
  208. inStream.Read(mActiveContacts);
  209. }
  210. void CharacterVirtualTest::OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity)
  211. {
  212. // Apply artificial velocity to the character when standing on the conveyor belt
  213. if (inBody2.GetID() == mConveyorBeltBody)
  214. ioLinearVelocity += Vec3(0, 0, 2);
  215. }
  216. void CharacterVirtualTest::OnContactCommon(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
  217. {
  218. // Draw a box around the character when it enters the sensor
  219. if (inBodyID2 == mSensorBody)
  220. {
  221. AABox box = inCharacter->GetShape()->GetWorldSpaceBounds(inCharacter->GetCenterOfMassTransform(), Vec3::sOne());
  222. mDebugRenderer->DrawBox(box, Color::sGreen, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  223. }
  224. // Dynamic boxes on the ramp go through all permutations
  225. Array<BodyID>::const_iterator i = find(mRampBlocks.begin(), mRampBlocks.end(), inBodyID2);
  226. if (i != mRampBlocks.end())
  227. {
  228. size_t index = i - mRampBlocks.begin();
  229. ioSettings.mCanPushCharacter = (index & 1) != 0;
  230. ioSettings.mCanReceiveImpulses = (index & 2) != 0;
  231. }
  232. // If we encounter an object that can push the player, enable sliding
  233. if (inCharacter == mCharacter
  234. && ioSettings.mCanPushCharacter
  235. && mPhysicsSystem->GetBodyInterface().GetMotionType(inBodyID2) != EMotionType::Static)
  236. mAllowSliding = true;
  237. }
  238. void CharacterVirtualTest::OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
  239. {
  240. OnContactCommon(inCharacter, inBodyID2, inSubShapeID2, inContactPosition, inContactNormal, ioSettings);
  241. if (inCharacter == mCharacter)
  242. {
  243. #ifdef CHARACTER_TRACE_CONTACTS
  244. Trace("Contact added with body %08x, sub shape %08x", inBodyID2.GetIndexAndSequenceNumber(), inSubShapeID2.GetValue());
  245. #endif
  246. CharacterVirtual::ContactKey c(inBodyID2, inSubShapeID2);
  247. if (std::find(mActiveContacts.begin(), mActiveContacts.end(), c) != mActiveContacts.end())
  248. FatalError("Got an add contact that should have been a persisted contact");
  249. mActiveContacts.push_back(c);
  250. }
  251. }
  252. void CharacterVirtualTest::OnContactPersisted(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
  253. {
  254. OnContactCommon(inCharacter, inBodyID2, inSubShapeID2, inContactPosition, inContactNormal, ioSettings);
  255. if (inCharacter == mCharacter)
  256. {
  257. #ifdef CHARACTER_TRACE_CONTACTS
  258. Trace("Contact persisted with body %08x, sub shape %08x", inBodyID2.GetIndexAndSequenceNumber(), inSubShapeID2.GetValue());
  259. #endif
  260. if (std::find(mActiveContacts.begin(), mActiveContacts.end(), CharacterVirtual::ContactKey(inBodyID2, inSubShapeID2)) == mActiveContacts.end())
  261. FatalError("Got a persisted contact that should have been an add contact");
  262. }
  263. }
  264. void CharacterVirtualTest::OnContactRemoved(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2)
  265. {
  266. if (inCharacter == mCharacter)
  267. {
  268. #ifdef CHARACTER_TRACE_CONTACTS
  269. Trace("Contact removed with body %08x, sub shape %08x", inBodyID2.GetIndexAndSequenceNumber(), inSubShapeID2.GetValue());
  270. #endif
  271. ContactSet::iterator it = std::find(mActiveContacts.begin(), mActiveContacts.end(), CharacterVirtual::ContactKey(inBodyID2, inSubShapeID2));
  272. if (it == mActiveContacts.end())
  273. FatalError("Got a remove contact that has not been added");
  274. mActiveContacts.erase(it);
  275. }
  276. }
  277. void CharacterVirtualTest::OnCharacterContactCommon(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
  278. {
  279. // Characters can only be pushed in their own update
  280. if (sPlayerCanPushOtherCharacters)
  281. ioSettings.mCanPushCharacter = sOtherCharactersCanPushPlayer || inOtherCharacter == mCharacter;
  282. else if (sOtherCharactersCanPushPlayer)
  283. ioSettings.mCanPushCharacter = inCharacter == mCharacter;
  284. else
  285. ioSettings.mCanPushCharacter = false;
  286. // If the player can be pushed by the other virtual character, we allow sliding
  287. if (inCharacter == mCharacter && ioSettings.mCanPushCharacter)
  288. mAllowSliding = true;
  289. }
  290. void CharacterVirtualTest::OnCharacterContactAdded(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
  291. {
  292. OnCharacterContactCommon(inCharacter, inOtherCharacter, inSubShapeID2, inContactPosition, inContactNormal, ioSettings);
  293. if (inCharacter == mCharacter)
  294. {
  295. #ifdef CHARACTER_TRACE_CONTACTS
  296. Trace("Contact added with character %08x, sub shape %08x", inOtherCharacter->GetID().GetValue(), inSubShapeID2.GetValue());
  297. #endif
  298. CharacterVirtual::ContactKey c(inOtherCharacter->GetID(), inSubShapeID2);
  299. if (std::find(mActiveContacts.begin(), mActiveContacts.end(), c) != mActiveContacts.end())
  300. FatalError("Got an add contact that should have been a persisted contact");
  301. mActiveContacts.push_back(c);
  302. }
  303. }
  304. void CharacterVirtualTest::OnCharacterContactPersisted(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
  305. {
  306. OnCharacterContactCommon(inCharacter, inOtherCharacter, inSubShapeID2, inContactPosition, inContactNormal, ioSettings);
  307. if (inCharacter == mCharacter)
  308. {
  309. #ifdef CHARACTER_TRACE_CONTACTS
  310. Trace("Contact persisted with character %08x, sub shape %08x", inOtherCharacter->GetID().GetValue(), inSubShapeID2.GetValue());
  311. #endif
  312. if (std::find(mActiveContacts.begin(), mActiveContacts.end(), CharacterVirtual::ContactKey(inOtherCharacter->GetID(), inSubShapeID2)) == mActiveContacts.end())
  313. FatalError("Got a persisted contact that should have been an add contact");
  314. }
  315. }
  316. void CharacterVirtualTest::OnCharacterContactRemoved(const CharacterVirtual *inCharacter, const CharacterID &inOtherCharacterID, const SubShapeID &inSubShapeID2)
  317. {
  318. if (inCharacter == mCharacter)
  319. {
  320. #ifdef CHARACTER_TRACE_CONTACTS
  321. Trace("Contact removed with character %08x, sub shape %08x", inOtherCharacterID.GetValue(), inSubShapeID2.GetValue());
  322. #endif
  323. ContactSet::iterator it = std::find(mActiveContacts.begin(), mActiveContacts.end(), CharacterVirtual::ContactKey(inOtherCharacterID, inSubShapeID2));
  324. if (it == mActiveContacts.end())
  325. FatalError("Got a remove contact that has not been added");
  326. mActiveContacts.erase(it);
  327. }
  328. }
  329. void CharacterVirtualTest::OnContactSolve(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial, Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity)
  330. {
  331. // Ignore callbacks for other characters than the player
  332. if (inCharacter != mCharacter)
  333. return;
  334. // Don't allow the player to slide down static not-too-steep surfaces when not actively moving and when not on a moving platform
  335. if (!mAllowSliding && inContactVelocity.IsNearZero() && !inCharacter->IsSlopeTooSteep(inContactNormal))
  336. ioNewCharacterVelocity = Vec3::sZero();
  337. }