CharacterVirtual.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Character/CharacterVirtual.h>
  6. #include <Jolt/Physics/Body/Body.h>
  7. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  8. #include <Jolt/Physics/PhysicsSystem.h>
  9. #include <Jolt/Physics/Collision/ShapeCast.h>
  10. #include <Jolt/Physics/Collision/CollideShape.h>
  11. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  12. #include <Jolt/Physics/Collision/InternalEdgeRemovingCollector.h>
  13. #include <Jolt/Core/QuickSort.h>
  14. #include <Jolt/Geometry/ConvexSupport.h>
  15. #include <Jolt/Geometry/GJKClosestPoint.h>
  16. #ifdef JPH_DEBUG_RENDERER
  17. #include <Jolt/Renderer/DebugRenderer.h>
  18. #endif // JPH_DEBUG_RENDERER
  19. JPH_NAMESPACE_BEGIN
  20. void CharacterVsCharacterCollisionSimple::Remove(const CharacterVirtual *inCharacter)
  21. {
  22. Array<CharacterVirtual *>::iterator i = std::find(mCharacters.begin(), mCharacters.end(), inCharacter);
  23. if (i != mCharacters.end())
  24. mCharacters.erase(i);
  25. }
  26. void CharacterVsCharacterCollisionSimple::CollideCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector) const
  27. {
  28. // Make shape 1 relative to inBaseOffset
  29. Mat44 transform1 = inCenterOfMassTransform.PostTranslated(-inBaseOffset).ToMat44();
  30. const Shape *shape = inCharacter->GetShape();
  31. CollideShapeSettings settings = inCollideShapeSettings;
  32. // Iterate over all characters
  33. for (const CharacterVirtual *c : mCharacters)
  34. if (c != inCharacter
  35. && !ioCollector.ShouldEarlyOut())
  36. {
  37. // Collector needs to know which character we're colliding with
  38. ioCollector.SetUserData(reinterpret_cast<uint64>(c));
  39. // Make shape 2 relative to inBaseOffset
  40. Mat44 transform2 = c->GetCenterOfMassTransform().PostTranslated(-inBaseOffset).ToMat44();
  41. // We need to add the padding of character 2 so that we will detect collision with its outer shell
  42. settings.mMaxSeparationDistance = inCollideShapeSettings.mMaxSeparationDistance + c->GetCharacterPadding();
  43. // Note that this collides against the character's shape without padding, this will be corrected for in CharacterVirtual::GetContactsAtPosition
  44. CollisionDispatch::sCollideShapeVsShape(shape, c->GetShape(), Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), transform1, transform2, SubShapeIDCreator(), SubShapeIDCreator(), settings, ioCollector);
  45. }
  46. // Reset the user data
  47. ioCollector.SetUserData(0);
  48. }
  49. void CharacterVsCharacterCollisionSimple::CastCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, Vec3Arg inDirection, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector) const
  50. {
  51. // Convert shape cast relative to inBaseOffset
  52. Mat44 transform1 = inCenterOfMassTransform.PostTranslated(-inBaseOffset).ToMat44();
  53. ShapeCast shape_cast(inCharacter->GetShape(), Vec3::sReplicate(1.0f), transform1, inDirection);
  54. // Iterate over all characters
  55. for (const CharacterVirtual *c : mCharacters)
  56. if (c != inCharacter
  57. && !ioCollector.ShouldEarlyOut())
  58. {
  59. // Collector needs to know which character we're colliding with
  60. ioCollector.SetUserData(reinterpret_cast<uint64>(c));
  61. // Make shape 2 relative to inBaseOffset
  62. Mat44 transform2 = c->GetCenterOfMassTransform().PostTranslated(-inBaseOffset).ToMat44();
  63. // Note that this collides against the character's shape without padding, this will be corrected for in CharacterVirtual::GetFirstContactForSweep
  64. CollisionDispatch::sCastShapeVsShapeWorldSpace(shape_cast, inShapeCastSettings, c->GetShape(), Vec3::sReplicate(1.0f), { }, transform2, SubShapeIDCreator(), SubShapeIDCreator(), ioCollector);
  65. }
  66. // Reset the user data
  67. ioCollector.SetUserData(0);
  68. }
  69. CharacterVirtual::CharacterVirtual(const CharacterVirtualSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, uint64 inUserData, PhysicsSystem *inSystem) :
  70. CharacterBase(inSettings, inSystem),
  71. mBackFaceMode(inSettings->mBackFaceMode),
  72. mPredictiveContactDistance(inSettings->mPredictiveContactDistance),
  73. mMaxCollisionIterations(inSettings->mMaxCollisionIterations),
  74. mMaxConstraintIterations(inSettings->mMaxConstraintIterations),
  75. mMinTimeRemaining(inSettings->mMinTimeRemaining),
  76. mCollisionTolerance(inSettings->mCollisionTolerance),
  77. mCharacterPadding(inSettings->mCharacterPadding),
  78. mMaxNumHits(inSettings->mMaxNumHits),
  79. mHitReductionCosMaxAngle(inSettings->mHitReductionCosMaxAngle),
  80. mPenetrationRecoverySpeed(inSettings->mPenetrationRecoverySpeed),
  81. mEnhancedInternalEdgeRemoval(inSettings->mEnhancedInternalEdgeRemoval),
  82. mShapeOffset(inSettings->mShapeOffset),
  83. mPosition(inPosition),
  84. mRotation(inRotation),
  85. mUserData(inUserData)
  86. {
  87. // Copy settings
  88. SetMaxStrength(inSettings->mMaxStrength);
  89. SetMass(inSettings->mMass);
  90. // Create an inner rigid body if requested
  91. if (inSettings->mInnerBodyShape != nullptr)
  92. {
  93. BodyCreationSettings settings(inSettings->mInnerBodyShape, GetInnerBodyPosition(), mRotation, EMotionType::Kinematic, inSettings->mInnerBodyLayer);
  94. settings.mAllowSleeping = false; // Disable sleeping so that we will receive sensor callbacks
  95. settings.mUserData = inUserData;
  96. mInnerBodyID = inSystem->GetBodyInterface().CreateAndAddBody(settings, EActivation::Activate);
  97. }
  98. }
  99. CharacterVirtual::~CharacterVirtual()
  100. {
  101. if (!mInnerBodyID.IsInvalid())
  102. {
  103. mSystem->GetBodyInterface().RemoveBody(mInnerBodyID);
  104. mSystem->GetBodyInterface().DestroyBody(mInnerBodyID);
  105. }
  106. }
  107. void CharacterVirtual::UpdateInnerBodyTransform()
  108. {
  109. if (!mInnerBodyID.IsInvalid())
  110. mSystem->GetBodyInterface().SetPositionAndRotation(mInnerBodyID, GetInnerBodyPosition(), mRotation, EActivation::DontActivate);
  111. }
  112. void CharacterVirtual::GetAdjustedBodyVelocity(const Body& inBody, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const
  113. {
  114. // Get real velocity of body
  115. if (!inBody.IsStatic())
  116. {
  117. const MotionProperties *mp = inBody.GetMotionPropertiesUnchecked();
  118. outLinearVelocity = mp->GetLinearVelocity();
  119. outAngularVelocity = mp->GetAngularVelocity();
  120. }
  121. else
  122. {
  123. outLinearVelocity = outAngularVelocity = Vec3::sZero();
  124. }
  125. // Allow application to override
  126. if (mListener != nullptr)
  127. mListener->OnAdjustBodyVelocity(this, inBody, outLinearVelocity, outAngularVelocity);
  128. }
  129. Vec3 CharacterVirtual::CalculateCharacterGroundVelocity(RVec3Arg inCenterOfMass, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, float inDeltaTime) const
  130. {
  131. // Get angular velocity
  132. float angular_velocity_len_sq = inAngularVelocity.LengthSq();
  133. if (angular_velocity_len_sq < 1.0e-12f)
  134. return inLinearVelocity;
  135. float angular_velocity_len = sqrt(angular_velocity_len_sq);
  136. // Calculate the rotation that the object will make in the time step
  137. Quat rotation = Quat::sRotation(inAngularVelocity / angular_velocity_len, angular_velocity_len * inDeltaTime);
  138. // Calculate where the new character position will be
  139. RVec3 new_position = inCenterOfMass + rotation * Vec3(mPosition - inCenterOfMass);
  140. // Calculate the velocity
  141. return inLinearVelocity + Vec3(new_position - mPosition) / inDeltaTime;
  142. }
  143. template <class taCollector>
  144. void CharacterVirtual::sFillContactProperties(const CharacterVirtual *inCharacter, Contact &outContact, const Body &inBody, Vec3Arg inUp, RVec3Arg inBaseOffset, const taCollector &inCollector, const CollideShapeResult &inResult)
  145. {
  146. // Get adjusted body velocity
  147. Vec3 linear_velocity, angular_velocity;
  148. inCharacter->GetAdjustedBodyVelocity(inBody, linear_velocity, angular_velocity);
  149. outContact.mPosition = inBaseOffset + inResult.mContactPointOn2;
  150. outContact.mLinearVelocity = linear_velocity + angular_velocity.Cross(Vec3(outContact.mPosition - inBody.GetCenterOfMassPosition())); // Calculate point velocity
  151. outContact.mContactNormal = -inResult.mPenetrationAxis.NormalizedOr(Vec3::sZero());
  152. outContact.mSurfaceNormal = inCollector.GetContext()->GetWorldSpaceSurfaceNormal(inResult.mSubShapeID2, outContact.mPosition);
  153. if (outContact.mContactNormal.Dot(outContact.mSurfaceNormal) < 0.0f)
  154. outContact.mSurfaceNormal = -outContact.mSurfaceNormal; // Flip surface normal if we're hitting a back face
  155. if (outContact.mContactNormal.Dot(inUp) > outContact.mSurfaceNormal.Dot(inUp))
  156. outContact.mSurfaceNormal = outContact.mContactNormal; // Replace surface normal with contact normal if the contact normal is pointing more upwards
  157. outContact.mDistance = -inResult.mPenetrationDepth;
  158. outContact.mBodyB = inResult.mBodyID2;
  159. outContact.mSubShapeIDB = inResult.mSubShapeID2;
  160. outContact.mMotionTypeB = inBody.GetMotionType();
  161. outContact.mIsSensorB = inBody.IsSensor();
  162. outContact.mUserData = inBody.GetUserData();
  163. outContact.mMaterial = inCollector.GetContext()->GetMaterial(inResult.mSubShapeID2);
  164. }
  165. void CharacterVirtual::sFillCharacterContactProperties(Contact &outContact, CharacterVirtual *inOtherCharacter, RVec3Arg inBaseOffset, const CollideShapeResult &inResult)
  166. {
  167. outContact.mPosition = inBaseOffset + inResult.mContactPointOn2;
  168. outContact.mLinearVelocity = inOtherCharacter->GetLinearVelocity();
  169. outContact.mSurfaceNormal = outContact.mContactNormal = -inResult.mPenetrationAxis.NormalizedOr(Vec3::sZero());
  170. outContact.mDistance = -inResult.mPenetrationDepth;
  171. outContact.mCharacterB = inOtherCharacter;
  172. outContact.mSubShapeIDB = inResult.mSubShapeID2;
  173. outContact.mMotionTypeB = EMotionType::Kinematic; // Other character is kinematic, we can't directly move it
  174. outContact.mIsSensorB = false;
  175. outContact.mUserData = inOtherCharacter->GetUserData();
  176. outContact.mMaterial = PhysicsMaterial::sDefault;
  177. }
  178. void CharacterVirtual::ContactCollector::AddHit(const CollideShapeResult &inResult)
  179. {
  180. // If we exceed our contact limit, try to clean up near-duplicate contacts
  181. if (mContacts.size() == mMaxHits)
  182. {
  183. // Flag that we hit this code path
  184. mMaxHitsExceeded = true;
  185. // Check if we can do reduction
  186. if (mHitReductionCosMaxAngle > -1.0f)
  187. {
  188. // Loop all contacts and find similar contacts
  189. for (int i = (int)mContacts.size() - 1; i >= 0; --i)
  190. {
  191. Contact &contact_i = mContacts[i];
  192. for (int j = i - 1; j >= 0; --j)
  193. {
  194. Contact &contact_j = mContacts[j];
  195. if (contact_i.IsSameBody(contact_j)
  196. && contact_i.mContactNormal.Dot(contact_j.mContactNormal) > mHitReductionCosMaxAngle) // Very similar contact normals
  197. {
  198. // Remove the contact with the biggest distance
  199. bool i_is_last = i == (int)mContacts.size() - 1;
  200. if (contact_i.mDistance > contact_j.mDistance)
  201. {
  202. // Remove i
  203. if (!i_is_last)
  204. contact_i = mContacts.back();
  205. mContacts.pop_back();
  206. // Break out of the loop, i is now an element that we already processed
  207. break;
  208. }
  209. else
  210. {
  211. // Remove j
  212. contact_j = mContacts.back();
  213. mContacts.pop_back();
  214. // If i was the last element, we just moved it into position j. Break out of the loop, we'll see it again later.
  215. if (i_is_last)
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. if (mContacts.size() == mMaxHits)
  223. {
  224. // There are still too many hits, give up!
  225. ForceEarlyOut();
  226. return;
  227. }
  228. }
  229. if (inResult.mBodyID2.IsInvalid())
  230. {
  231. // Assuming this is a hit against another character
  232. JPH_ASSERT(mOtherCharacter != nullptr);
  233. // Create contact with other character
  234. mContacts.emplace_back();
  235. Contact &contact = mContacts.back();
  236. sFillCharacterContactProperties(contact, mOtherCharacter, mBaseOffset, inResult);
  237. contact.mFraction = 0.0f;
  238. }
  239. else
  240. {
  241. // Create contact with other body
  242. BodyLockRead lock(mSystem->GetBodyLockInterface(), inResult.mBodyID2);
  243. if (lock.SucceededAndIsInBroadPhase())
  244. {
  245. mContacts.emplace_back();
  246. Contact &contact = mContacts.back();
  247. sFillContactProperties(mCharacter, contact, lock.GetBody(), mUp, mBaseOffset, *this, inResult);
  248. contact.mFraction = 0.0f;
  249. }
  250. }
  251. }
  252. void CharacterVirtual::ContactCastCollector::AddHit(const ShapeCastResult &inResult)
  253. {
  254. if (inResult.mFraction < mContact.mFraction // Since we're doing checks against the world and against characters, we may get a hit with a higher fraction than the previous hit
  255. && inResult.mFraction > 0.0f // Ignore collisions at fraction = 0
  256. && inResult.mPenetrationAxis.Dot(mDisplacement) > 0.0f) // Ignore penetrations that we're moving away from
  257. {
  258. // Test if this contact should be ignored
  259. for (const IgnoredContact &c : mIgnoredContacts)
  260. if (c.mBodyID == inResult.mBodyID2 && c.mSubShapeID == inResult.mSubShapeID2)
  261. return;
  262. Contact contact;
  263. if (inResult.mBodyID2.IsInvalid())
  264. {
  265. // Assuming this is a hit against another character
  266. JPH_ASSERT(mOtherCharacter != nullptr);
  267. // Create contact with other character
  268. sFillCharacterContactProperties(contact, mOtherCharacter, mBaseOffset, inResult);
  269. }
  270. else
  271. {
  272. // Lock body only while we fetch contact properties
  273. BodyLockRead lock(mSystem->GetBodyLockInterface(), inResult.mBodyID2);
  274. if (!lock.SucceededAndIsInBroadPhase())
  275. return;
  276. // Sweeps don't result in OnContactAdded callbacks so we can ignore sensors here
  277. const Body &body = lock.GetBody();
  278. if (body.IsSensor())
  279. return;
  280. // Convert the hit result into a contact
  281. sFillContactProperties(mCharacter, contact, body, mUp, mBaseOffset, *this, inResult);
  282. }
  283. contact.mFraction = inResult.mFraction;
  284. // Check if the contact that will make us penetrate more than the allowed tolerance
  285. if (contact.mDistance + contact.mContactNormal.Dot(mDisplacement) < -mCharacter->mCollisionTolerance
  286. && mCharacter->ValidateContact(contact))
  287. {
  288. mContact = contact;
  289. UpdateEarlyOutFraction(contact.mFraction);
  290. }
  291. }
  292. }
  293. void CharacterVirtual::CheckCollision(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  294. {
  295. // Query shape transform
  296. RMat44 transform = GetCenterOfMassTransform(inPosition, inRotation, inShape);
  297. // Settings for collide shape
  298. CollideShapeSettings settings;
  299. settings.mBackFaceMode = mBackFaceMode;
  300. settings.mActiveEdgeMovementDirection = inMovementDirection;
  301. settings.mMaxSeparationDistance = mCharacterPadding + inMaxSeparationDistance;
  302. // Body filter
  303. IgnoreSingleBodyFilterChained body_filter(mInnerBodyID, inBodyFilter);
  304. // Collide shape
  305. if (mEnhancedInternalEdgeRemoval)
  306. {
  307. // Version that does additional work to remove internal edges
  308. settings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
  309. settings.mCollectFacesMode = ECollectFacesMode::CollectFaces;
  310. // This is a copy of NarrowPhaseQuery::CollideShape with additional logic to wrap the collector in an InternalEdgeRemovingCollector and flushing that collector after every body
  311. class MyCollector : public CollideShapeBodyCollector
  312. {
  313. public:
  314. MyCollector(const Shape *inShape, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) :
  315. CollideShapeBodyCollector(ioCollector),
  316. mShape(inShape),
  317. mCenterOfMassTransform(inCenterOfMassTransform),
  318. mBaseOffset(inBaseOffset),
  319. mCollideShapeSettings(inCollideShapeSettings),
  320. mBodyLockInterface(inBodyLockInterface),
  321. mBodyFilter(inBodyFilter),
  322. mShapeFilter(inShapeFilter),
  323. mCollector(ioCollector)
  324. {
  325. }
  326. virtual void AddHit(const ResultType &inResult) override
  327. {
  328. // See NarrowPhaseQuery::CollideShape
  329. if (mBodyFilter.ShouldCollide(inResult))
  330. {
  331. BodyLockRead lock(mBodyLockInterface, inResult);
  332. if (lock.SucceededAndIsInBroadPhase())
  333. {
  334. const Body &body = lock.GetBody();
  335. if (mBodyFilter.ShouldCollideLocked(body))
  336. {
  337. TransformedShape ts = body.GetTransformedShape();
  338. mCollector.OnBody(body);
  339. lock.ReleaseLock();
  340. ts.CollideShape(mShape, Vec3::sReplicate(1.0f), mCenterOfMassTransform, mCollideShapeSettings, mBaseOffset, mCollector, mShapeFilter);
  341. // After each body, we need to flush the InternalEdgeRemovingCollector because it uses 'ts' as context and it will go out of scope at the end of this block
  342. mCollector.Flush();
  343. UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
  344. }
  345. }
  346. }
  347. }
  348. const Shape * mShape;
  349. RMat44 mCenterOfMassTransform;
  350. RVec3 mBaseOffset;
  351. const CollideShapeSettings & mCollideShapeSettings;
  352. const BodyLockInterface & mBodyLockInterface;
  353. const BodyFilter & mBodyFilter;
  354. const ShapeFilter & mShapeFilter;
  355. InternalEdgeRemovingCollector mCollector;
  356. };
  357. // Calculate bounds for shape and expand by max separation distance
  358. AABox bounds = inShape->GetWorldSpaceBounds(transform, Vec3::sReplicate(1.0f));
  359. bounds.ExpandBy(Vec3::sReplicate(settings.mMaxSeparationDistance));
  360. // Do broadphase test
  361. MyCollector collector(inShape, transform, settings, inBaseOffset, ioCollector, mSystem->GetBodyLockInterface(), body_filter, inShapeFilter);
  362. mSystem->GetBroadPhaseQuery().CollideAABox(bounds, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  363. }
  364. else
  365. {
  366. // Version that uses the cached active edges
  367. settings.mActiveEdgeMode = EActiveEdgeMode::CollideOnlyWithActive;
  368. mSystem->GetNarrowPhaseQuery().CollideShape(inShape, Vec3::sReplicate(1.0f), transform, settings, inBaseOffset, ioCollector, inBroadPhaseLayerFilter, inObjectLayerFilter, body_filter, inShapeFilter);
  369. }
  370. // Also collide with other characters
  371. if (mCharacterVsCharacterCollision != nullptr)
  372. {
  373. ioCollector.SetContext(nullptr); // We're no longer colliding with a transformed shape, reset
  374. mCharacterVsCharacterCollision->CollideCharacter(this, transform, settings, inBaseOffset, ioCollector);
  375. }
  376. }
  377. void CharacterVirtual::GetContactsAtPosition(RVec3Arg inPosition, Vec3Arg inMovementDirection, const Shape *inShape, TempContactList &outContacts, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  378. {
  379. // Remove previous results
  380. outContacts.clear();
  381. // Body filter
  382. IgnoreSingleBodyFilterChained body_filter(mInnerBodyID, inBodyFilter);
  383. // Collide shape
  384. ContactCollector collector(mSystem, this, mMaxNumHits, mHitReductionCosMaxAngle, mUp, mPosition, outContacts);
  385. CheckCollision(inPosition, mRotation, inMovementDirection, mPredictiveContactDistance, inShape, mPosition, collector, inBroadPhaseLayerFilter, inObjectLayerFilter, body_filter, inShapeFilter);
  386. // The broadphase bounding boxes will not be deterministic, which means that the order in which the contacts are received by the collector is not deterministic.
  387. // Therefore we need to sort the contacts to preserve determinism. Note that currently this will fail if we exceed mMaxNumHits hits.
  388. QuickSort(outContacts.begin(), outContacts.end(), ContactOrderingPredicate());
  389. // Flag if we exceeded the max number of hits
  390. mMaxHitsExceeded = collector.mMaxHitsExceeded;
  391. // Reduce distance to contact by padding to ensure we stay away from the object by a little margin
  392. // (this will make collision detection cheaper - especially for sweep tests as they won't hit the surface if we're properly sliding)
  393. for (Contact &c : outContacts)
  394. {
  395. c.mDistance -= mCharacterPadding;
  396. if (c.mCharacterB != nullptr)
  397. c.mDistance -= c.mCharacterB->mCharacterPadding;
  398. }
  399. }
  400. void CharacterVirtual::RemoveConflictingContacts(TempContactList &ioContacts, IgnoredContactList &outIgnoredContacts) const
  401. {
  402. // Only use this algorithm if we're penetrating further than this (due to numerical precision issues we can always penetrate a little bit and we don't want to discard contacts if they just have a tiny penetration)
  403. // We do need to account for padding (see GetContactsAtPosition) that is removed from the contact distances, to compensate we add it to the cMinRequiredPenetration
  404. const float cMinRequiredPenetration = 1.25f * mCharacterPadding;
  405. // Discard conflicting penetrating contacts
  406. for (size_t c1 = 0; c1 < ioContacts.size(); c1++)
  407. {
  408. Contact &contact1 = ioContacts[c1];
  409. if (contact1.mDistance <= -cMinRequiredPenetration) // Only for penetrations
  410. for (size_t c2 = c1 + 1; c2 < ioContacts.size(); c2++)
  411. {
  412. Contact &contact2 = ioContacts[c2];
  413. if (contact1.IsSameBody(contact2)
  414. && contact2.mDistance <= -cMinRequiredPenetration // Only for penetrations
  415. && contact1.mContactNormal.Dot(contact2.mContactNormal) < 0.0f) // Only opposing normals
  416. {
  417. // Discard contacts with the least amount of penetration
  418. if (contact1.mDistance < contact2.mDistance)
  419. {
  420. // Discard the 2nd contact
  421. outIgnoredContacts.emplace_back(contact2.mBodyB, contact2.mSubShapeIDB);
  422. ioContacts.erase(ioContacts.begin() + c2);
  423. c2--;
  424. }
  425. else
  426. {
  427. // Discard the first contact
  428. outIgnoredContacts.emplace_back(contact1.mBodyB, contact1.mSubShapeIDB);
  429. ioContacts.erase(ioContacts.begin() + c1);
  430. c1--;
  431. break;
  432. }
  433. }
  434. }
  435. }
  436. }
  437. bool CharacterVirtual::ValidateContact(const Contact &inContact) const
  438. {
  439. if (mListener == nullptr)
  440. return true;
  441. if (inContact.mCharacterB != nullptr)
  442. return mListener->OnCharacterContactValidate(this, inContact.mCharacterB, inContact.mSubShapeIDB);
  443. else
  444. return mListener->OnContactValidate(this, inContact.mBodyB, inContact.mSubShapeIDB);
  445. }
  446. void CharacterVirtual::ContactAdded(const Contact &inContact, CharacterContactSettings &ioSettings) const
  447. {
  448. if (mListener != nullptr)
  449. {
  450. if (inContact.mCharacterB != nullptr)
  451. mListener->OnCharacterContactAdded(this, inContact.mCharacterB, inContact.mSubShapeIDB, inContact.mPosition, -inContact.mContactNormal, ioSettings);
  452. else
  453. mListener->OnContactAdded(this, inContact.mBodyB, inContact.mSubShapeIDB, inContact.mPosition, -inContact.mContactNormal, ioSettings);
  454. }
  455. }
  456. template <class T>
  457. inline static bool sCorrectFractionForCharacterPadding(const Shape *inShape, Mat44Arg inStart, Vec3Arg inDisplacement, const T &inPolygon, float &ioFraction)
  458. {
  459. if (inShape->GetType() == EShapeType::Convex)
  460. {
  461. // Get the support function for the shape we're casting
  462. const ConvexShape *convex_shape = static_cast<const ConvexShape *>(inShape);
  463. ConvexShape::SupportBuffer buffer;
  464. const ConvexShape::Support *support = convex_shape->GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  465. // Cast the shape against the polygon
  466. GJKClosestPoint gjk;
  467. return gjk.CastShape(inStart, inDisplacement, cDefaultCollisionTolerance, *support, inPolygon, ioFraction);
  468. }
  469. else if (inShape->GetSubType() == EShapeSubType::RotatedTranslated)
  470. {
  471. const RotatedTranslatedShape *rt_shape = static_cast<const RotatedTranslatedShape *>(inShape);
  472. return sCorrectFractionForCharacterPadding(rt_shape->GetInnerShape(), inStart * Mat44::sRotation(rt_shape->GetRotation()), inDisplacement, inPolygon, ioFraction);
  473. }
  474. else
  475. {
  476. JPH_ASSERT(false, "Not supported yet!");
  477. return false;
  478. }
  479. }
  480. bool CharacterVirtual::GetFirstContactForSweep(RVec3Arg inPosition, Vec3Arg inDisplacement, Contact &outContact, const IgnoredContactList &inIgnoredContacts, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
  481. {
  482. // Too small distance -> skip checking
  483. float displacement_len_sq = inDisplacement.LengthSq();
  484. if (displacement_len_sq < 1.0e-8f)
  485. return false;
  486. // Calculate start transform
  487. RMat44 start = GetCenterOfMassTransform(inPosition, mRotation, mShape);
  488. // Settings for the cast
  489. ShapeCastSettings settings;
  490. settings.mBackFaceModeTriangles = mBackFaceMode;
  491. settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  492. settings.mActiveEdgeMode = EActiveEdgeMode::CollideOnlyWithActive;
  493. settings.mUseShrunkenShapeAndConvexRadius = true;
  494. settings.mReturnDeepestPoint = false;
  495. // Calculate how much extra fraction we need to add to the cast to account for the character padding
  496. float character_padding_fraction = mCharacterPadding / sqrt(displacement_len_sq);
  497. // Body filter
  498. IgnoreSingleBodyFilterChained body_filter(mInnerBodyID, inBodyFilter);
  499. // Cast shape
  500. Contact contact;
  501. contact.mFraction = 1.0f + character_padding_fraction;
  502. RVec3 base_offset = start.GetTranslation();
  503. ContactCastCollector collector(mSystem, this, inDisplacement, mUp, inIgnoredContacts, base_offset, contact);
  504. collector.ResetEarlyOutFraction(contact.mFraction);
  505. RShapeCast shape_cast(mShape, Vec3::sReplicate(1.0f), start, inDisplacement);
  506. mSystem->GetNarrowPhaseQuery().CastShape(shape_cast, settings, base_offset, collector, inBroadPhaseLayerFilter, inObjectLayerFilter, body_filter, inShapeFilter);
  507. // Also collide with other characters
  508. if (mCharacterVsCharacterCollision != nullptr)
  509. {
  510. collector.SetContext(nullptr); // We're no longer colliding with a transformed shape, reset
  511. mCharacterVsCharacterCollision->CastCharacter(this, start, inDisplacement, settings, base_offset, collector);
  512. }
  513. if (contact.mBodyB.IsInvalid() && contact.mCharacterB == nullptr)
  514. return false;
  515. // Store contact
  516. outContact = contact;
  517. TransformedShape ts;
  518. float character_padding = mCharacterPadding;
  519. if (outContact.mCharacterB != nullptr)
  520. {
  521. // Create a transformed shape for the character
  522. RMat44 com = outContact.mCharacterB->GetCenterOfMassTransform();
  523. ts = TransformedShape(com.GetTranslation(), com.GetQuaternion(), outContact.mCharacterB->GetShape(), BodyID(), SubShapeIDCreator());
  524. // We need to take the other character's padding into account as well
  525. character_padding += outContact.mCharacterB->mCharacterPadding;
  526. }
  527. else
  528. {
  529. // Create a transformed shape for the body
  530. ts = mSystem->GetBodyInterface().GetTransformedShape(outContact.mBodyB);
  531. }
  532. // Fetch the face we're colliding with
  533. Shape::SupportingFace face;
  534. ts.GetSupportingFace(outContact.mSubShapeIDB, -outContact.mContactNormal, base_offset, face);
  535. bool corrected = false;
  536. if (face.size() >= 2)
  537. {
  538. // Inflate the colliding face by the character padding
  539. PolygonConvexSupport polygon(face);
  540. AddConvexRadius add_cvx(polygon, character_padding);
  541. // Correct fraction to hit this inflated face instead of the inner shape
  542. corrected = sCorrectFractionForCharacterPadding(mShape, start.GetRotation(), inDisplacement, add_cvx, outContact.mFraction);
  543. }
  544. if (!corrected)
  545. {
  546. // When there's only a single contact point or when we were unable to correct the fraction,
  547. // we can just move the fraction back so that the character and its padding don't hit the contact point anymore
  548. outContact.mFraction = max(0.0f, outContact.mFraction - character_padding_fraction);
  549. }
  550. // Ensure that we never return a fraction that's bigger than 1 (which could happen due to float precision issues).
  551. outContact.mFraction = min(outContact.mFraction, 1.0f);
  552. return true;
  553. }
  554. void CharacterVirtual::DetermineConstraints(TempContactList &inContacts, float inDeltaTime, ConstraintList &outConstraints) const
  555. {
  556. for (Contact &c : inContacts)
  557. {
  558. Vec3 contact_velocity = c.mLinearVelocity;
  559. // Penetrating contact: Add a contact velocity that pushes the character out at the desired speed
  560. if (c.mDistance < 0.0f)
  561. contact_velocity -= c.mContactNormal * c.mDistance * mPenetrationRecoverySpeed / inDeltaTime;
  562. // Convert to a constraint
  563. outConstraints.emplace_back();
  564. Constraint &constraint = outConstraints.back();
  565. constraint.mContact = &c;
  566. constraint.mLinearVelocity = contact_velocity;
  567. constraint.mPlane = Plane(c.mContactNormal, c.mDistance);
  568. // Next check if the angle is too steep and if it is add an additional constraint that holds the character back
  569. if (IsSlopeTooSteep(c.mSurfaceNormal))
  570. {
  571. // Only take planes that point up.
  572. // Note that we use the contact normal to allow for better sliding as the surface normal may be in the opposite direction of movement.
  573. float dot = c.mContactNormal.Dot(mUp);
  574. if (dot > 1.0e-3f) // Add a little slack, if the normal is perfectly horizontal we already have our vertical plane.
  575. {
  576. // Mark the slope constraint as steep
  577. constraint.mIsSteepSlope = true;
  578. // Make horizontal normal
  579. Vec3 normal = (c.mContactNormal - dot * mUp).Normalized();
  580. // Create a secondary constraint that blocks horizontal movement
  581. outConstraints.emplace_back();
  582. Constraint &vertical_constraint = outConstraints.back();
  583. vertical_constraint.mContact = &c;
  584. vertical_constraint.mLinearVelocity = contact_velocity.Dot(normal) * normal; // Project the contact velocity on the new normal so that both planes push at an equal rate
  585. vertical_constraint.mPlane = Plane(normal, c.mDistance / normal.Dot(c.mContactNormal)); // Calculate the distance we have to travel horizontally to hit the contact plane
  586. }
  587. }
  588. }
  589. }
  590. bool CharacterVirtual::HandleContact(Vec3Arg inVelocity, Constraint &ioConstraint, float inDeltaTime) const
  591. {
  592. Contact &contact = *ioConstraint.mContact;
  593. // Validate the contact point
  594. if (!ValidateContact(contact))
  595. return false;
  596. // Send contact added event
  597. CharacterContactSettings settings;
  598. ContactAdded(contact, settings);
  599. contact.mCanPushCharacter = settings.mCanPushCharacter;
  600. // We don't have any further interaction with sensors beyond an OnContactAdded notification
  601. if (contact.mIsSensorB)
  602. return false;
  603. // If body B cannot receive an impulse, we're done
  604. if (!settings.mCanReceiveImpulses || contact.mMotionTypeB != EMotionType::Dynamic)
  605. return true;
  606. // Lock the body we're colliding with
  607. BodyLockWrite lock(mSystem->GetBodyLockInterface(), contact.mBodyB);
  608. if (!lock.SucceededAndIsInBroadPhase())
  609. return false; // Body has been removed, we should not collide with it anymore
  610. const Body &body = lock.GetBody();
  611. // Calculate the velocity that we want to apply at B so that it will start moving at the character's speed at the contact point
  612. constexpr float cDamping = 0.9f;
  613. constexpr float cPenetrationResolution = 0.4f;
  614. Vec3 relative_velocity = inVelocity - contact.mLinearVelocity;
  615. float projected_velocity = relative_velocity.Dot(contact.mContactNormal);
  616. float delta_velocity = -projected_velocity * cDamping - min(contact.mDistance, 0.0f) * cPenetrationResolution / inDeltaTime;
  617. // Don't apply impulses if we're separating
  618. if (delta_velocity < 0.0f)
  619. return true;
  620. // Determine mass properties of the body we're colliding with
  621. const MotionProperties *motion_properties = body.GetMotionProperties();
  622. RVec3 center_of_mass = body.GetCenterOfMassPosition();
  623. Mat44 inverse_inertia = body.GetInverseInertia();
  624. float inverse_mass = motion_properties->GetInverseMass();
  625. // Calculate the inverse of the mass of body B as seen at the contact point in the direction of the contact normal
  626. Vec3 jacobian = Vec3(contact.mPosition - center_of_mass).Cross(contact.mContactNormal);
  627. float inv_effective_mass = inverse_inertia.Multiply3x3(jacobian).Dot(jacobian) + inverse_mass;
  628. // Impulse P = M dv
  629. float impulse = delta_velocity / inv_effective_mass;
  630. // Clamp the impulse according to the character strength, character strength is a force in newtons, P = F dt
  631. float max_impulse = mMaxStrength * inDeltaTime;
  632. impulse = min(impulse, max_impulse);
  633. // Calculate the world space impulse to apply
  634. Vec3 world_impulse = -impulse * contact.mContactNormal;
  635. // Cancel impulse in down direction (we apply gravity later)
  636. float impulse_dot_up = world_impulse.Dot(mUp);
  637. if (impulse_dot_up < 0.0f)
  638. world_impulse -= impulse_dot_up * mUp;
  639. // Now apply the impulse (body is already locked so we use the no-lock interface)
  640. mSystem->GetBodyInterfaceNoLock().AddImpulse(contact.mBodyB, world_impulse, contact.mPosition);
  641. return true;
  642. }
  643. void CharacterVirtual::SolveConstraints(Vec3Arg inVelocity, float inDeltaTime, float inTimeRemaining, ConstraintList &ioConstraints, IgnoredContactList &ioIgnoredContacts, float &outTimeSimulated, Vec3 &outDisplacement, TempAllocator &inAllocator
  644. #ifdef JPH_DEBUG_RENDERER
  645. , bool inDrawConstraints
  646. #endif // JPH_DEBUG_RENDERER
  647. ) const
  648. {
  649. // If there are no constraints we can immediately move to our target
  650. if (ioConstraints.empty())
  651. {
  652. outDisplacement = inVelocity * inTimeRemaining;
  653. outTimeSimulated = inTimeRemaining;
  654. return;
  655. }
  656. // Create array that holds the constraints in order of time of impact (sort will happen later)
  657. Array<Constraint *, STLTempAllocator<Constraint *>> sorted_constraints(inAllocator);
  658. sorted_constraints.resize(ioConstraints.size());
  659. for (size_t index = 0; index < sorted_constraints.size(); index++)
  660. sorted_constraints[index] = &ioConstraints[index];
  661. // This is the velocity we use for the displacement, if we hit something it will be shortened
  662. Vec3 velocity = inVelocity;
  663. // Keep track of the last velocity that was applied to the character so that we can detect when the velocity reverses
  664. Vec3 last_velocity = inVelocity;
  665. // Start with no displacement
  666. outDisplacement = Vec3::sZero();
  667. outTimeSimulated = 0.0f;
  668. // These are the contacts that we hit previously without moving a significant distance
  669. Array<Constraint *, STLTempAllocator<Constraint *>> previous_contacts(inAllocator);
  670. previous_contacts.resize(mMaxConstraintIterations);
  671. int num_previous_contacts = 0;
  672. // Loop for a max amount of iterations
  673. for (uint iteration = 0; iteration < mMaxConstraintIterations; iteration++)
  674. {
  675. // Calculate time of impact for all constraints
  676. for (Constraint &c : ioConstraints)
  677. {
  678. // Project velocity on plane direction
  679. c.mProjectedVelocity = c.mPlane.GetNormal().Dot(c.mLinearVelocity - velocity);
  680. if (c.mProjectedVelocity < 1.0e-6f)
  681. {
  682. c.mTOI = FLT_MAX;
  683. }
  684. else
  685. {
  686. // Distance to plane
  687. float dist = c.mPlane.SignedDistance(outDisplacement);
  688. if (dist - c.mProjectedVelocity * inTimeRemaining > -1.0e-4f)
  689. {
  690. // Too little penetration, accept the movement
  691. c.mTOI = FLT_MAX;
  692. }
  693. else
  694. {
  695. // Calculate time of impact
  696. c.mTOI = max(0.0f, dist / c.mProjectedVelocity);
  697. }
  698. }
  699. }
  700. // Sort constraints on proximity
  701. QuickSort(sorted_constraints.begin(), sorted_constraints.end(), [](const Constraint *inLHS, const Constraint *inRHS) {
  702. // If both constraints hit at t = 0 then order the one that will push the character furthest first
  703. // Note that because we add velocity to penetrating contacts, this will also resolve contacts that penetrate the most
  704. if (inLHS->mTOI <= 0.0f && inRHS->mTOI <= 0.0f)
  705. return inLHS->mProjectedVelocity > inRHS->mProjectedVelocity;
  706. // Then sort on time of impact
  707. if (inLHS->mTOI != inRHS->mTOI)
  708. return inLHS->mTOI < inRHS->mTOI;
  709. // As a tie breaker sort static first so it has the most influence
  710. return inLHS->mContact->mMotionTypeB > inRHS->mContact->mMotionTypeB;
  711. });
  712. // Find the first valid constraint
  713. Constraint *constraint = nullptr;
  714. for (Constraint *c : sorted_constraints)
  715. {
  716. // Take the first contact and see if we can reach it
  717. if (c->mTOI >= inTimeRemaining)
  718. {
  719. // We can reach our goal!
  720. outDisplacement += velocity * inTimeRemaining;
  721. outTimeSimulated += inTimeRemaining;
  722. return;
  723. }
  724. // Test if this contact was discarded by the contact callback before
  725. if (c->mContact->mWasDiscarded)
  726. continue;
  727. // Check if we made contact with this before
  728. if (!c->mContact->mHadCollision)
  729. {
  730. // Handle the contact
  731. if (!HandleContact(velocity, *c, inDeltaTime))
  732. {
  733. // Constraint should be ignored, remove it from the list
  734. c->mContact->mWasDiscarded = true;
  735. // Mark it as ignored for GetFirstContactForSweep
  736. ioIgnoredContacts.emplace_back(c->mContact->mBodyB, c->mContact->mSubShapeIDB);
  737. continue;
  738. }
  739. c->mContact->mHadCollision = true;
  740. }
  741. // Cancel velocity of constraint if it cannot push the character
  742. if (!c->mContact->mCanPushCharacter)
  743. c->mLinearVelocity = Vec3::sZero();
  744. // We found the first constraint that we want to collide with
  745. constraint = c;
  746. break;
  747. }
  748. if (constraint == nullptr)
  749. {
  750. // All constraints were discarded, we can reach our goal!
  751. outDisplacement += velocity * inTimeRemaining;
  752. outTimeSimulated += inTimeRemaining;
  753. return;
  754. }
  755. // Move to the contact
  756. outDisplacement += velocity * constraint->mTOI;
  757. inTimeRemaining -= constraint->mTOI;
  758. outTimeSimulated += constraint->mTOI;
  759. // If there's not enough time left to be simulated, bail
  760. if (inTimeRemaining < mMinTimeRemaining)
  761. return;
  762. // If we've moved significantly, clear all previous contacts
  763. if (constraint->mTOI > 1.0e-4f)
  764. num_previous_contacts = 0;
  765. // Get the normal of the plane we're hitting
  766. Vec3 plane_normal = constraint->mPlane.GetNormal();
  767. // If we're hitting a steep slope we cancel the velocity towards the slope first so that we don't end up sliding up the slope
  768. // (we may hit the slope before the vertical wall constraint we added which will result in a small movement up causing jitter in the character movement)
  769. if (constraint->mIsSteepSlope)
  770. {
  771. // We're hitting a steep slope, create a vertical plane that blocks any further movement up the slope (note: not normalized)
  772. Vec3 vertical_plane_normal = plane_normal - plane_normal.Dot(mUp) * mUp;
  773. // Get the relative velocity between the character and the constraint
  774. Vec3 relative_velocity = velocity - constraint->mLinearVelocity;
  775. // Remove velocity towards the slope
  776. velocity = velocity - min(0.0f, relative_velocity.Dot(vertical_plane_normal)) * vertical_plane_normal / vertical_plane_normal.LengthSq();
  777. }
  778. // Get the relative velocity between the character and the constraint
  779. Vec3 relative_velocity = velocity - constraint->mLinearVelocity;
  780. // Calculate new velocity if we cancel the relative velocity in the normal direction
  781. Vec3 new_velocity = velocity - relative_velocity.Dot(plane_normal) * plane_normal;
  782. // Find the normal of the previous contact that we will violate the most if we move in this new direction
  783. float highest_penetration = 0.0f;
  784. Constraint *other_constraint = nullptr;
  785. for (Constraint **c = previous_contacts.data(); c < previous_contacts.data() + num_previous_contacts; ++c)
  786. if (*c != constraint)
  787. {
  788. // Calculate how much we will penetrate if we move in this direction
  789. Vec3 other_normal = (*c)->mPlane.GetNormal();
  790. float penetration = ((*c)->mLinearVelocity - new_velocity).Dot(other_normal);
  791. if (penetration > highest_penetration)
  792. {
  793. // We don't want parallel or anti-parallel normals as that will cause our cross product below to become zero. Slack is approx 10 degrees.
  794. float dot = other_normal.Dot(plane_normal);
  795. if (dot < 0.984f && dot > -0.984f)
  796. {
  797. highest_penetration = penetration;
  798. other_constraint = *c;
  799. }
  800. }
  801. }
  802. // Check if we found a 2nd constraint
  803. if (other_constraint != nullptr)
  804. {
  805. // Calculate the sliding direction and project the new velocity onto that sliding direction
  806. Vec3 other_normal = other_constraint->mPlane.GetNormal();
  807. Vec3 slide_dir = plane_normal.Cross(other_normal).Normalized();
  808. Vec3 velocity_in_slide_dir = new_velocity.Dot(slide_dir) * slide_dir;
  809. // Cancel the constraint velocity in the other constraint plane's direction so that we won't try to apply it again and keep ping ponging between planes
  810. constraint->mLinearVelocity -= min(0.0f, constraint->mLinearVelocity.Dot(other_normal)) * other_normal;
  811. // Cancel the other constraints velocity in this constraint plane's direction so that we won't try to apply it again and keep ping ponging between planes
  812. other_constraint->mLinearVelocity -= min(0.0f, other_constraint->mLinearVelocity.Dot(plane_normal)) * plane_normal;
  813. // Calculate the velocity of this constraint perpendicular to the slide direction
  814. Vec3 perpendicular_velocity = constraint->mLinearVelocity - constraint->mLinearVelocity.Dot(slide_dir) * slide_dir;
  815. // Calculate the velocity of the other constraint perpendicular to the slide direction
  816. Vec3 other_perpendicular_velocity = other_constraint->mLinearVelocity - other_constraint->mLinearVelocity.Dot(slide_dir) * slide_dir;
  817. // Add all components together
  818. new_velocity = velocity_in_slide_dir + perpendicular_velocity + other_perpendicular_velocity;
  819. }
  820. // Allow application to modify calculated velocity
  821. if (mListener != nullptr)
  822. {
  823. if (constraint->mContact->mCharacterB != nullptr)
  824. mListener->OnCharacterContactSolve(this, constraint->mContact->mCharacterB, constraint->mContact->mSubShapeIDB, constraint->mContact->mPosition, constraint->mContact->mContactNormal, constraint->mContact->mLinearVelocity, constraint->mContact->mMaterial, velocity, new_velocity);
  825. else
  826. mListener->OnContactSolve(this, constraint->mContact->mBodyB, constraint->mContact->mSubShapeIDB, constraint->mContact->mPosition, constraint->mContact->mContactNormal, constraint->mContact->mLinearVelocity, constraint->mContact->mMaterial, velocity, new_velocity);
  827. }
  828. #ifdef JPH_DEBUG_RENDERER
  829. if (inDrawConstraints)
  830. {
  831. // Calculate where to draw
  832. RVec3 offset = mPosition + Vec3(0, 0, 2.5f * (iteration + 1));
  833. // Draw constraint plane
  834. DebugRenderer::sInstance->DrawPlane(offset, constraint->mPlane.GetNormal(), Color::sCyan, 1.0f);
  835. // Draw 2nd constraint plane
  836. if (other_constraint != nullptr)
  837. DebugRenderer::sInstance->DrawPlane(offset, other_constraint->mPlane.GetNormal(), Color::sBlue, 1.0f);
  838. // Draw starting velocity
  839. DebugRenderer::sInstance->DrawArrow(offset, offset + velocity, Color::sGreen, 0.05f);
  840. // Draw resulting velocity
  841. DebugRenderer::sInstance->DrawArrow(offset, offset + new_velocity, Color::sRed, 0.05f);
  842. }
  843. #endif // JPH_DEBUG_RENDERER
  844. // Update the velocity
  845. velocity = new_velocity;
  846. // Add the contact to the list so that next iteration we can avoid violating it again
  847. previous_contacts[num_previous_contacts] = constraint;
  848. num_previous_contacts++;
  849. // Check early out
  850. if (constraint->mProjectedVelocity < 1.0e-8f // Constraint should not be pushing, otherwise there may be other constraints that are pushing us
  851. && velocity.LengthSq() < 1.0e-8f) // There's not enough velocity left
  852. return;
  853. // If the constraint has velocity we accept the new velocity, otherwise check that we didn't reverse velocity
  854. if (!constraint->mLinearVelocity.IsNearZero(1.0e-8f))
  855. last_velocity = constraint->mLinearVelocity;
  856. else if (velocity.Dot(last_velocity) < 0.0f)
  857. return;
  858. }
  859. }
  860. void CharacterVirtual::UpdateSupportingContact(bool inSkipContactVelocityCheck, TempAllocator &inAllocator)
  861. {
  862. // Flag contacts as having a collision if they're close enough but ignore contacts we're moving away from.
  863. // Note that if we did MoveShape before we want to preserve any contacts that it marked as colliding
  864. for (Contact &c : mActiveContacts)
  865. if (!c.mWasDiscarded
  866. && !c.mHadCollision
  867. && c.mDistance < mCollisionTolerance
  868. && (inSkipContactVelocityCheck || c.mSurfaceNormal.Dot(mLinearVelocity - c.mLinearVelocity) <= 1.0e-4f))
  869. {
  870. if (ValidateContact(c) && !c.mIsSensorB)
  871. c.mHadCollision = true;
  872. else
  873. c.mWasDiscarded = true;
  874. }
  875. // Calculate transform that takes us to character local space
  876. RMat44 inv_transform = RMat44::sInverseRotationTranslation(mRotation, mPosition);
  877. // Determine if we're supported or not
  878. int num_supported = 0;
  879. int num_sliding = 0;
  880. int num_avg_normal = 0;
  881. Vec3 avg_normal = Vec3::sZero();
  882. Vec3 avg_velocity = Vec3::sZero();
  883. const Contact *supporting_contact = nullptr;
  884. float max_cos_angle = -FLT_MAX;
  885. const Contact *deepest_contact = nullptr;
  886. float smallest_distance = FLT_MAX;
  887. for (const Contact &c : mActiveContacts)
  888. if (c.mHadCollision)
  889. {
  890. // Calculate the angle between the plane normal and the up direction
  891. float cos_angle = c.mSurfaceNormal.Dot(mUp);
  892. // Find the deepest contact
  893. if (c.mDistance < smallest_distance)
  894. {
  895. deepest_contact = &c;
  896. smallest_distance = c.mDistance;
  897. }
  898. // If this contact is in front of our plane, we cannot be supported by it
  899. if (mSupportingVolume.SignedDistance(Vec3(inv_transform * c.mPosition)) > 0.0f)
  900. continue;
  901. // Find the contact with the normal that is pointing most upwards and store it
  902. if (max_cos_angle < cos_angle)
  903. {
  904. supporting_contact = &c;
  905. max_cos_angle = cos_angle;
  906. }
  907. // Check if this is a sliding or supported contact
  908. bool is_supported = mCosMaxSlopeAngle > cNoMaxSlopeAngle || cos_angle >= mCosMaxSlopeAngle;
  909. if (is_supported)
  910. num_supported++;
  911. else
  912. num_sliding++;
  913. // If the angle between the two is less than 85 degrees we also use it to calculate the average normal
  914. if (cos_angle >= 0.08f)
  915. {
  916. avg_normal += c.mSurfaceNormal;
  917. num_avg_normal++;
  918. // For static or dynamic objects or for contacts that don't support us just take the contact velocity
  919. if (c.mMotionTypeB != EMotionType::Kinematic || !is_supported)
  920. avg_velocity += c.mLinearVelocity;
  921. else
  922. {
  923. // For keyframed objects that support us calculate the velocity at our position rather than at the contact position so that we properly follow the object
  924. BodyLockRead lock(mSystem->GetBodyLockInterface(), c.mBodyB);
  925. if (lock.SucceededAndIsInBroadPhase())
  926. {
  927. const Body &body = lock.GetBody();
  928. // Get adjusted body velocity
  929. Vec3 linear_velocity, angular_velocity;
  930. GetAdjustedBodyVelocity(body, linear_velocity, angular_velocity);
  931. // Calculate the ground velocity
  932. avg_velocity += CalculateCharacterGroundVelocity(body.GetCenterOfMassPosition(), linear_velocity, angular_velocity, mLastDeltaTime);
  933. }
  934. else
  935. {
  936. // Fall back to contact velocity
  937. avg_velocity += c.mLinearVelocity;
  938. }
  939. }
  940. }
  941. }
  942. // Take either the most supporting contact or the deepest contact
  943. const Contact *best_contact = supporting_contact != nullptr? supporting_contact : deepest_contact;
  944. // Calculate average normal and velocity
  945. if (num_avg_normal >= 1)
  946. {
  947. mGroundNormal = avg_normal.Normalized();
  948. mGroundVelocity = avg_velocity / float(num_avg_normal);
  949. }
  950. else if (best_contact != nullptr)
  951. {
  952. mGroundNormal = best_contact->mSurfaceNormal;
  953. mGroundVelocity = best_contact->mLinearVelocity;
  954. }
  955. else
  956. {
  957. mGroundNormal = Vec3::sZero();
  958. mGroundVelocity = Vec3::sZero();
  959. }
  960. // Copy contact properties
  961. if (best_contact != nullptr)
  962. {
  963. mGroundBodyID = best_contact->mBodyB;
  964. mGroundBodySubShapeID = best_contact->mSubShapeIDB;
  965. mGroundPosition = best_contact->mPosition;
  966. mGroundMaterial = best_contact->mMaterial;
  967. mGroundUserData = best_contact->mUserData;
  968. }
  969. else
  970. {
  971. mGroundBodyID = BodyID();
  972. mGroundBodySubShapeID = SubShapeID();
  973. mGroundPosition = RVec3::sZero();
  974. mGroundMaterial = PhysicsMaterial::sDefault;
  975. mGroundUserData = 0;
  976. }
  977. // Determine ground state
  978. if (num_supported > 0)
  979. {
  980. // We made contact with something that supports us
  981. mGroundState = EGroundState::OnGround;
  982. }
  983. else if (num_sliding > 0)
  984. {
  985. if ((mLinearVelocity - deepest_contact->mLinearVelocity).Dot(mUp) > 1.0e-4f)
  986. {
  987. // We cannot be on ground if we're moving upwards relative to the ground
  988. mGroundState = EGroundState::OnSteepGround;
  989. }
  990. else
  991. {
  992. // If we're sliding down, we may actually be standing on multiple sliding contacts in such a way that we can't slide off, in this case we're also supported
  993. // Convert the contacts into constraints
  994. TempContactList contacts(mActiveContacts.begin(), mActiveContacts.end(), inAllocator);
  995. ConstraintList constraints(inAllocator);
  996. constraints.reserve(contacts.size() * 2);
  997. DetermineConstraints(contacts, mLastDeltaTime, constraints);
  998. // Solve the displacement using these constraints, this is used to check if we didn't move at all because we are supported
  999. Vec3 displacement;
  1000. float time_simulated;
  1001. IgnoredContactList ignored_contacts(inAllocator);
  1002. ignored_contacts.reserve(contacts.size());
  1003. SolveConstraints(-mUp, 1.0f, 1.0f, constraints, ignored_contacts, time_simulated, displacement, inAllocator);
  1004. // If we're blocked then we're supported, otherwise we're sliding
  1005. float min_required_displacement_sq = Square(0.6f * mLastDeltaTime);
  1006. if (time_simulated < 0.001f || displacement.LengthSq() < min_required_displacement_sq)
  1007. mGroundState = EGroundState::OnGround;
  1008. else
  1009. mGroundState = EGroundState::OnSteepGround;
  1010. }
  1011. }
  1012. else
  1013. {
  1014. // Not supported by anything
  1015. mGroundState = best_contact != nullptr? EGroundState::NotSupported : EGroundState::InAir;
  1016. }
  1017. }
  1018. void CharacterVirtual::StoreActiveContacts(const TempContactList &inContacts, TempAllocator &inAllocator)
  1019. {
  1020. mActiveContacts.assign(inContacts.begin(), inContacts.end());
  1021. UpdateSupportingContact(true, inAllocator);
  1022. }
  1023. void CharacterVirtual::MoveShape(RVec3 &ioPosition, Vec3Arg inVelocity, float inDeltaTime, ContactList *outActiveContacts, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator
  1024. #ifdef JPH_DEBUG_RENDERER
  1025. , bool inDrawConstraints
  1026. #endif // JPH_DEBUG_RENDERER
  1027. ) const
  1028. {
  1029. JPH_DET_LOG("CharacterVirtual::MoveShape: pos: " << ioPosition << " vel: " << inVelocity << " dt: " << inDeltaTime);
  1030. Vec3 movement_direction = inVelocity.NormalizedOr(Vec3::sZero());
  1031. float time_remaining = inDeltaTime;
  1032. for (uint iteration = 0; iteration < mMaxCollisionIterations && time_remaining >= mMinTimeRemaining; iteration++)
  1033. {
  1034. JPH_DET_LOG("iter: " << iteration << " time: " << time_remaining);
  1035. // Determine contacts in the neighborhood
  1036. TempContactList contacts(inAllocator);
  1037. contacts.reserve(mMaxNumHits);
  1038. GetContactsAtPosition(ioPosition, movement_direction, mShape, contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter);
  1039. #ifdef JPH_ENABLE_DETERMINISM_LOG
  1040. for (const Contact &c : contacts)
  1041. JPH_DET_LOG("contact: " << c.mPosition << " vel: " << c.mLinearVelocity << " cnormal: " << c.mContactNormal << " snormal: " << c.mSurfaceNormal << " dist: " << c.mDistance << " fraction: " << c.mFraction << " body: " << c.mBodyB << " subshape: " << c.mSubShapeIDB);
  1042. #endif // JPH_ENABLE_DETERMINISM_LOG
  1043. // Remove contacts with the same body that have conflicting normals
  1044. IgnoredContactList ignored_contacts(inAllocator);
  1045. ignored_contacts.reserve(contacts.size());
  1046. RemoveConflictingContacts(contacts, ignored_contacts);
  1047. // Convert contacts into constraints
  1048. ConstraintList constraints(inAllocator);
  1049. constraints.reserve(contacts.size() * 2);
  1050. DetermineConstraints(contacts, inDeltaTime, constraints);
  1051. #ifdef JPH_DEBUG_RENDERER
  1052. bool draw_constraints = inDrawConstraints && iteration == 0;
  1053. if (draw_constraints)
  1054. {
  1055. for (const Constraint &c : constraints)
  1056. {
  1057. // Draw contact point
  1058. DebugRenderer::sInstance->DrawMarker(c.mContact->mPosition, Color::sYellow, 0.05f);
  1059. Vec3 dist_to_plane = -c.mPlane.GetConstant() * c.mPlane.GetNormal();
  1060. // Draw arrow towards surface that we're hitting
  1061. DebugRenderer::sInstance->DrawArrow(c.mContact->mPosition, c.mContact->mPosition - dist_to_plane, Color::sYellow, 0.05f);
  1062. // Draw plane around the player position indicating the space that we can move
  1063. DebugRenderer::sInstance->DrawPlane(mPosition + dist_to_plane, c.mPlane.GetNormal(), Color::sCyan, 1.0f);
  1064. DebugRenderer::sInstance->DrawArrow(mPosition + dist_to_plane, mPosition + dist_to_plane + c.mContact->mSurfaceNormal, Color::sRed, 0.05f);
  1065. }
  1066. }
  1067. #endif // JPH_DEBUG_RENDERER
  1068. // Solve the displacement using these constraints
  1069. Vec3 displacement;
  1070. float time_simulated;
  1071. SolveConstraints(inVelocity, inDeltaTime, time_remaining, constraints, ignored_contacts, time_simulated, displacement, inAllocator
  1072. #ifdef JPH_DEBUG_RENDERER
  1073. , draw_constraints
  1074. #endif // JPH_DEBUG_RENDERER
  1075. );
  1076. // Store the contacts now that the colliding ones have been marked
  1077. if (outActiveContacts != nullptr)
  1078. outActiveContacts->assign(contacts.begin(), contacts.end());
  1079. // Do a sweep to test if the path is really unobstructed
  1080. Contact cast_contact;
  1081. if (GetFirstContactForSweep(ioPosition, displacement, cast_contact, ignored_contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter))
  1082. {
  1083. displacement *= cast_contact.mFraction;
  1084. time_simulated *= cast_contact.mFraction;
  1085. }
  1086. // Update the position
  1087. ioPosition += displacement;
  1088. time_remaining -= time_simulated;
  1089. // If the displacement during this iteration was too small we assume we cannot further progress this update
  1090. if (displacement.LengthSq() < 1.0e-8f)
  1091. break;
  1092. }
  1093. }
  1094. void CharacterVirtual::SetUserData(uint64 inUserData)
  1095. {
  1096. mUserData = inUserData;
  1097. if (!mInnerBodyID.IsInvalid())
  1098. mSystem->GetBodyInterface().SetUserData(mInnerBodyID, inUserData);
  1099. }
  1100. Vec3 CharacterVirtual::CancelVelocityTowardsSteepSlopes(Vec3Arg inDesiredVelocity) const
  1101. {
  1102. // If we're not pushing against a steep slope, return the desired velocity
  1103. // Note: This is important as WalkStairs overrides the ground state to OnGround when its first check fails but the second succeeds
  1104. if (mGroundState == CharacterVirtual::EGroundState::OnGround
  1105. || mGroundState == CharacterVirtual::EGroundState::InAir)
  1106. return inDesiredVelocity;
  1107. Vec3 desired_velocity = inDesiredVelocity;
  1108. for (const Contact &c : mActiveContacts)
  1109. if (c.mHadCollision
  1110. && IsSlopeTooSteep(c.mSurfaceNormal))
  1111. {
  1112. // Note that we use the contact normal to allow for better sliding as the surface normal may be in the opposite direction of movement.
  1113. Vec3 normal = c.mContactNormal;
  1114. // Remove normal vertical component
  1115. normal -= normal.Dot(mUp) * mUp;
  1116. // Cancel horizontal movement in opposite direction
  1117. float dot = normal.Dot(desired_velocity);
  1118. if (dot < 0.0f)
  1119. desired_velocity -= (dot * normal) / normal.LengthSq();
  1120. }
  1121. return desired_velocity;
  1122. }
  1123. void CharacterVirtual::Update(float inDeltaTime, Vec3Arg inGravity, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator)
  1124. {
  1125. // If there's no delta time, we don't need to do anything
  1126. if (inDeltaTime <= 0.0f)
  1127. return;
  1128. // Remember delta time for checking if we're supported by the ground
  1129. mLastDeltaTime = inDeltaTime;
  1130. // Slide the shape through the world
  1131. MoveShape(mPosition, mLinearVelocity, inDeltaTime, &mActiveContacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator
  1132. #ifdef JPH_DEBUG_RENDERER
  1133. , sDrawConstraints
  1134. #endif // JPH_DEBUG_RENDERER
  1135. );
  1136. // Determine the object that we're standing on
  1137. UpdateSupportingContact(false, inAllocator);
  1138. // Ensure that the rigid body ends up at the new position
  1139. UpdateInnerBodyTransform();
  1140. // If we're on the ground
  1141. if (!mGroundBodyID.IsInvalid() && mMass > 0.0f)
  1142. {
  1143. // Add the impulse to the ground due to gravity: P = F dt = M g dt
  1144. float normal_dot_gravity = mGroundNormal.Dot(inGravity);
  1145. if (normal_dot_gravity < 0.0f)
  1146. {
  1147. Vec3 world_impulse = -(mMass * normal_dot_gravity / inGravity.Length() * inDeltaTime) * inGravity;
  1148. mSystem->GetBodyInterface().AddImpulse(mGroundBodyID, world_impulse, mGroundPosition);
  1149. }
  1150. }
  1151. }
  1152. void CharacterVirtual::RefreshContacts(const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator)
  1153. {
  1154. // Determine the contacts
  1155. TempContactList contacts(inAllocator);
  1156. contacts.reserve(mMaxNumHits);
  1157. GetContactsAtPosition(mPosition, mLinearVelocity.NormalizedOr(Vec3::sZero()), mShape, contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter);
  1158. StoreActiveContacts(contacts, inAllocator);
  1159. }
  1160. void CharacterVirtual::UpdateGroundVelocity()
  1161. {
  1162. BodyLockRead lock(mSystem->GetBodyLockInterface(), mGroundBodyID);
  1163. if (lock.SucceededAndIsInBroadPhase())
  1164. {
  1165. const Body &body = lock.GetBody();
  1166. // Get adjusted body velocity
  1167. Vec3 linear_velocity, angular_velocity;
  1168. GetAdjustedBodyVelocity(body, linear_velocity, angular_velocity);
  1169. // Calculate the ground velocity
  1170. mGroundVelocity = CalculateCharacterGroundVelocity(body.GetCenterOfMassPosition(), linear_velocity, angular_velocity, mLastDeltaTime);
  1171. }
  1172. }
  1173. void CharacterVirtual::MoveToContact(RVec3Arg inPosition, const Contact &inContact, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator)
  1174. {
  1175. // Set the new position
  1176. SetPosition(inPosition);
  1177. // Trigger contact added callback
  1178. CharacterContactSettings dummy;
  1179. ContactAdded(inContact, dummy);
  1180. // Determine the contacts
  1181. TempContactList contacts(inAllocator);
  1182. contacts.reserve(mMaxNumHits + 1); // +1 because we can add one extra below
  1183. GetContactsAtPosition(mPosition, mLinearVelocity.NormalizedOr(Vec3::sZero()), mShape, contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter);
  1184. // Ensure that we mark inContact as colliding
  1185. bool found_contact = false;
  1186. for (Contact &c : contacts)
  1187. if (c.mBodyB == inContact.mBodyB
  1188. && c.mSubShapeIDB == inContact.mSubShapeIDB)
  1189. {
  1190. c.mHadCollision = true;
  1191. found_contact = true;
  1192. }
  1193. if (!found_contact)
  1194. {
  1195. contacts.push_back(inContact);
  1196. Contact &copy = contacts.back();
  1197. copy.mHadCollision = true;
  1198. }
  1199. StoreActiveContacts(contacts, inAllocator);
  1200. JPH_ASSERT(mGroundState != EGroundState::InAir);
  1201. // Ensure that the rigid body ends up at the new position
  1202. UpdateInnerBodyTransform();
  1203. }
  1204. bool CharacterVirtual::SetShape(const Shape *inShape, float inMaxPenetrationDepth, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator)
  1205. {
  1206. if (mShape == nullptr || mSystem == nullptr)
  1207. {
  1208. // It hasn't been initialized yet
  1209. mShape = inShape;
  1210. return true;
  1211. }
  1212. if (inShape != mShape && inShape != nullptr)
  1213. {
  1214. if (inMaxPenetrationDepth < FLT_MAX)
  1215. {
  1216. // Check collision around the new shape
  1217. TempContactList contacts(inAllocator);
  1218. contacts.reserve(mMaxNumHits);
  1219. GetContactsAtPosition(mPosition, mLinearVelocity.NormalizedOr(Vec3::sZero()), inShape, contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter);
  1220. // Test if this results in penetration, if so cancel the transition
  1221. for (const Contact &c : contacts)
  1222. if (c.mDistance < -inMaxPenetrationDepth
  1223. && !c.mIsSensorB)
  1224. return false;
  1225. StoreActiveContacts(contacts, inAllocator);
  1226. }
  1227. // Set new shape
  1228. mShape = inShape;
  1229. }
  1230. return mShape == inShape;
  1231. }
  1232. void CharacterVirtual::SetInnerBodyShape(const Shape *inShape)
  1233. {
  1234. mSystem->GetBodyInterface().SetShape(mInnerBodyID, inShape, false, EActivation::DontActivate);
  1235. }
  1236. bool CharacterVirtual::CanWalkStairs(Vec3Arg inLinearVelocity) const
  1237. {
  1238. // We can only walk stairs if we're supported
  1239. if (!IsSupported())
  1240. return false;
  1241. // Check if there's enough horizontal velocity to trigger a stair walk
  1242. Vec3 horizontal_velocity = inLinearVelocity - inLinearVelocity.Dot(mUp) * mUp;
  1243. if (horizontal_velocity.IsNearZero(1.0e-6f))
  1244. return false;
  1245. // Check contacts for steep slopes
  1246. for (const Contact &c : mActiveContacts)
  1247. if (c.mHadCollision
  1248. && c.mSurfaceNormal.Dot(horizontal_velocity - c.mLinearVelocity) < 0.0f // Pushing into the contact
  1249. && IsSlopeTooSteep(c.mSurfaceNormal)) // Slope too steep
  1250. return true;
  1251. return false;
  1252. }
  1253. bool CharacterVirtual::WalkStairs(float inDeltaTime, Vec3Arg inStepUp, Vec3Arg inStepForward, Vec3Arg inStepForwardTest, Vec3Arg inStepDownExtra, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator)
  1254. {
  1255. // Move up
  1256. Vec3 up = inStepUp;
  1257. Contact contact;
  1258. IgnoredContactList dummy_ignored_contacts(inAllocator);
  1259. if (GetFirstContactForSweep(mPosition, up, contact, dummy_ignored_contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter))
  1260. {
  1261. if (contact.mFraction < 1.0e-6f)
  1262. return false; // No movement, cancel
  1263. // Limit up movement to the first contact point
  1264. up *= contact.mFraction;
  1265. }
  1266. RVec3 up_position = mPosition + up;
  1267. #ifdef JPH_DEBUG_RENDERER
  1268. // Draw sweep up
  1269. if (sDrawWalkStairs)
  1270. DebugRenderer::sInstance->DrawArrow(mPosition, up_position, Color::sWhite, 0.01f);
  1271. #endif // JPH_DEBUG_RENDERER
  1272. // Collect normals of steep slopes that we would like to walk stairs on.
  1273. // We need to do this before calling MoveShape because it will update mActiveContacts.
  1274. Vec3 character_velocity = inStepForward / inDeltaTime;
  1275. Vec3 horizontal_velocity = character_velocity - character_velocity.Dot(mUp) * mUp;
  1276. Array<Vec3, STLTempAllocator<Vec3>> steep_slope_normals(inAllocator);
  1277. steep_slope_normals.reserve(mActiveContacts.size());
  1278. for (const Contact &c : mActiveContacts)
  1279. if (c.mHadCollision
  1280. && c.mSurfaceNormal.Dot(horizontal_velocity - c.mLinearVelocity) < 0.0f // Pushing into the contact
  1281. && IsSlopeTooSteep(c.mSurfaceNormal)) // Slope too steep
  1282. steep_slope_normals.push_back(c.mSurfaceNormal);
  1283. if (steep_slope_normals.empty())
  1284. return false; // No steep slopes, cancel
  1285. // Horizontal movement
  1286. RVec3 new_position = up_position;
  1287. MoveShape(new_position, character_velocity, inDeltaTime, nullptr, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator);
  1288. Vec3 horizontal_movement = Vec3(new_position - up_position);
  1289. float horizontal_movement_sq = horizontal_movement.LengthSq();
  1290. if (horizontal_movement_sq < 1.0e-8f)
  1291. return false; // No movement, cancel
  1292. // Check if we made any progress towards any of the steep slopes, if not we just slid along the slope
  1293. // so we need to cancel the stair walk or else we will move faster than we should as we've done
  1294. // normal movement first and then stair walk.
  1295. bool made_progress = false;
  1296. float max_dot = -0.05f * inStepForward.Length();
  1297. for (const Vec3 &normal : steep_slope_normals)
  1298. if (normal.Dot(horizontal_movement) < max_dot)
  1299. {
  1300. // We moved more than 5% of the forward step against a steep slope, accept this as progress
  1301. made_progress = true;
  1302. break;
  1303. }
  1304. if (!made_progress)
  1305. return false;
  1306. #ifdef JPH_DEBUG_RENDERER
  1307. // Draw horizontal sweep
  1308. if (sDrawWalkStairs)
  1309. DebugRenderer::sInstance->DrawArrow(up_position, new_position, Color::sWhite, 0.01f);
  1310. #endif // JPH_DEBUG_RENDERER
  1311. // Move down towards the floor.
  1312. // Note that we travel the same amount down as we traveled up with the specified extra
  1313. Vec3 down = -up + inStepDownExtra;
  1314. if (!GetFirstContactForSweep(new_position, down, contact, dummy_ignored_contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter))
  1315. return false; // No floor found, we're in mid air, cancel stair walk
  1316. #ifdef JPH_DEBUG_RENDERER
  1317. // Draw sweep down
  1318. if (sDrawWalkStairs)
  1319. {
  1320. RVec3 debug_pos = new_position + contact.mFraction * down;
  1321. DebugRenderer::sInstance->DrawArrow(new_position, debug_pos, Color::sWhite, 0.01f);
  1322. DebugRenderer::sInstance->DrawArrow(contact.mPosition, contact.mPosition + contact.mSurfaceNormal, Color::sWhite, 0.01f);
  1323. mShape->Draw(DebugRenderer::sInstance, GetCenterOfMassTransform(debug_pos, mRotation, mShape), Vec3::sReplicate(1.0f), Color::sWhite, false, true);
  1324. }
  1325. #endif // JPH_DEBUG_RENDERER
  1326. // Test for floor that will support the character
  1327. if (IsSlopeTooSteep(contact.mSurfaceNormal))
  1328. {
  1329. // If no test position was provided, we cancel the stair walk
  1330. if (inStepForwardTest.IsNearZero())
  1331. return false;
  1332. // Delta time may be very small, so it may be that we hit the edge of a step and the normal is too horizontal.
  1333. // In order to judge if the floor is flat further along the sweep, we test again for a floor at inStepForwardTest
  1334. // and check if the normal is valid there.
  1335. RVec3 test_position = up_position;
  1336. MoveShape(test_position, inStepForwardTest / inDeltaTime, inDeltaTime, nullptr, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator);
  1337. float test_horizontal_movement_sq = Vec3(test_position - up_position).LengthSq();
  1338. if (test_horizontal_movement_sq <= horizontal_movement_sq + 1.0e-8f)
  1339. return false; // We didn't move any further than in the previous test
  1340. #ifdef JPH_DEBUG_RENDERER
  1341. // Draw 2nd sweep horizontal
  1342. if (sDrawWalkStairs)
  1343. DebugRenderer::sInstance->DrawArrow(up_position, test_position, Color::sCyan, 0.01f);
  1344. #endif // JPH_DEBUG_RENDERER
  1345. // Then sweep down
  1346. Contact test_contact;
  1347. if (!GetFirstContactForSweep(test_position, down, test_contact, dummy_ignored_contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter))
  1348. return false;
  1349. #ifdef JPH_DEBUG_RENDERER
  1350. // Draw 2nd sweep down
  1351. if (sDrawWalkStairs)
  1352. {
  1353. RVec3 debug_pos = test_position + test_contact.mFraction * down;
  1354. DebugRenderer::sInstance->DrawArrow(test_position, debug_pos, Color::sCyan, 0.01f);
  1355. DebugRenderer::sInstance->DrawArrow(test_contact.mPosition, test_contact.mPosition + test_contact.mSurfaceNormal, Color::sCyan, 0.01f);
  1356. mShape->Draw(DebugRenderer::sInstance, GetCenterOfMassTransform(debug_pos, mRotation, mShape), Vec3::sReplicate(1.0f), Color::sCyan, false, true);
  1357. }
  1358. #endif // JPH_DEBUG_RENDERER
  1359. if (IsSlopeTooSteep(test_contact.mSurfaceNormal))
  1360. return false;
  1361. }
  1362. // Calculate new down position
  1363. down *= contact.mFraction;
  1364. new_position += down;
  1365. // Move the character to the new location
  1366. MoveToContact(new_position, contact, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator);
  1367. // Override ground state to 'on ground', it is possible that the contact normal is too steep, but in this case the inStepForwardTest has found a contact normal that is not too steep
  1368. mGroundState = EGroundState::OnGround;
  1369. return true;
  1370. }
  1371. bool CharacterVirtual::StickToFloor(Vec3Arg inStepDown, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator)
  1372. {
  1373. // Try to find the floor
  1374. Contact contact;
  1375. IgnoredContactList dummy_ignored_contacts(inAllocator);
  1376. if (!GetFirstContactForSweep(mPosition, inStepDown, contact, dummy_ignored_contacts, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter))
  1377. return false; // If no floor found, don't update our position
  1378. // Calculate new position
  1379. RVec3 new_position = mPosition + contact.mFraction * inStepDown;
  1380. #ifdef JPH_DEBUG_RENDERER
  1381. // Draw sweep down
  1382. if (sDrawStickToFloor)
  1383. {
  1384. DebugRenderer::sInstance->DrawArrow(mPosition, new_position, Color::sOrange, 0.01f);
  1385. mShape->Draw(DebugRenderer::sInstance, GetCenterOfMassTransform(new_position, mRotation, mShape), Vec3::sReplicate(1.0f), Color::sOrange, false, true);
  1386. }
  1387. #endif // JPH_DEBUG_RENDERER
  1388. // Move the character to the new location
  1389. MoveToContact(new_position, contact, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator);
  1390. return true;
  1391. }
  1392. void CharacterVirtual::ExtendedUpdate(float inDeltaTime, Vec3Arg inGravity, const ExtendedUpdateSettings &inSettings, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator)
  1393. {
  1394. // Update the velocity
  1395. Vec3 desired_velocity = mLinearVelocity;
  1396. mLinearVelocity = CancelVelocityTowardsSteepSlopes(desired_velocity);
  1397. // Remember old position
  1398. RVec3 old_position = mPosition;
  1399. // Track if on ground before the update
  1400. bool ground_to_air = IsSupported();
  1401. // Update the character position (instant, do not have to wait for physics update)
  1402. Update(inDeltaTime, inGravity, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator);
  1403. // ... and that we got into air after
  1404. if (IsSupported())
  1405. ground_to_air = false;
  1406. // If stick to floor enabled and we're going from supported to not supported
  1407. if (ground_to_air && !inSettings.mStickToFloorStepDown.IsNearZero())
  1408. {
  1409. // If we're not moving up, stick to the floor
  1410. float velocity = Vec3(mPosition - old_position).Dot(mUp) / inDeltaTime;
  1411. if (velocity <= 1.0e-6f)
  1412. StickToFloor(inSettings.mStickToFloorStepDown, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator);
  1413. }
  1414. // If walk stairs enabled
  1415. if (!inSettings.mWalkStairsStepUp.IsNearZero())
  1416. {
  1417. // Calculate how much we wanted to move horizontally
  1418. Vec3 desired_horizontal_step = desired_velocity * inDeltaTime;
  1419. desired_horizontal_step -= desired_horizontal_step.Dot(mUp) * mUp;
  1420. float desired_horizontal_step_len = desired_horizontal_step.Length();
  1421. if (desired_horizontal_step_len > 0.0f)
  1422. {
  1423. // Calculate how much we moved horizontally
  1424. Vec3 achieved_horizontal_step = Vec3(mPosition - old_position);
  1425. achieved_horizontal_step -= achieved_horizontal_step.Dot(mUp) * mUp;
  1426. // Only count movement in the direction of the desired movement
  1427. // (otherwise we find it ok if we're sliding downhill while we're trying to climb uphill)
  1428. Vec3 step_forward_normalized = desired_horizontal_step / desired_horizontal_step_len;
  1429. achieved_horizontal_step = max(0.0f, achieved_horizontal_step.Dot(step_forward_normalized)) * step_forward_normalized;
  1430. float achieved_horizontal_step_len = achieved_horizontal_step.Length();
  1431. // If we didn't move as far as we wanted and we're against a slope that's too steep
  1432. if (achieved_horizontal_step_len + 1.0e-4f < desired_horizontal_step_len
  1433. && CanWalkStairs(desired_velocity))
  1434. {
  1435. // Calculate how much we should step forward
  1436. // Note that we clamp the step forward to a minimum distance. This is done because at very high frame rates the delta time
  1437. // may be very small, causing a very small step forward. If the step becomes small enough, we may not move far enough
  1438. // horizontally to actually end up at the top of the step.
  1439. Vec3 step_forward = step_forward_normalized * max(inSettings.mWalkStairsMinStepForward, desired_horizontal_step_len - achieved_horizontal_step_len);
  1440. // Calculate how far to scan ahead for a floor. This is only used in case the floor normal at step_forward is too steep.
  1441. // In that case an additional check will be performed at this distance to check if that normal is not too steep.
  1442. // Start with the ground normal in the horizontal plane and normalizing it
  1443. Vec3 step_forward_test = -mGroundNormal;
  1444. step_forward_test -= step_forward_test.Dot(mUp) * mUp;
  1445. step_forward_test = step_forward_test.NormalizedOr(step_forward_normalized);
  1446. // If this normalized vector and the character forward vector is bigger than a preset angle, we use the character forward vector instead of the ground normal
  1447. // to do our forward test
  1448. if (step_forward_test.Dot(step_forward_normalized) < inSettings.mWalkStairsCosAngleForwardContact)
  1449. step_forward_test = step_forward_normalized;
  1450. // Calculate the correct magnitude for the test vector
  1451. step_forward_test *= inSettings.mWalkStairsStepForwardTest;
  1452. WalkStairs(inDeltaTime, inSettings.mWalkStairsStepUp, step_forward, step_forward_test, inSettings.mWalkStairsStepDownExtra, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter, inAllocator);
  1453. }
  1454. }
  1455. }
  1456. }
  1457. void CharacterVirtual::Contact::SaveState(StateRecorder &inStream) const
  1458. {
  1459. inStream.Write(mPosition);
  1460. inStream.Write(mLinearVelocity);
  1461. inStream.Write(mContactNormal);
  1462. inStream.Write(mSurfaceNormal);
  1463. inStream.Write(mDistance);
  1464. inStream.Write(mFraction);
  1465. inStream.Write(mBodyB);
  1466. inStream.Write(mSubShapeIDB);
  1467. inStream.Write(mMotionTypeB);
  1468. inStream.Write(mHadCollision);
  1469. inStream.Write(mWasDiscarded);
  1470. inStream.Write(mCanPushCharacter);
  1471. // Cannot store user data (may be a pointer) and material
  1472. }
  1473. void CharacterVirtual::Contact::RestoreState(StateRecorder &inStream)
  1474. {
  1475. inStream.Read(mPosition);
  1476. inStream.Read(mLinearVelocity);
  1477. inStream.Read(mContactNormal);
  1478. inStream.Read(mSurfaceNormal);
  1479. inStream.Read(mDistance);
  1480. inStream.Read(mFraction);
  1481. inStream.Read(mBodyB);
  1482. inStream.Read(mSubShapeIDB);
  1483. inStream.Read(mMotionTypeB);
  1484. inStream.Read(mHadCollision);
  1485. inStream.Read(mWasDiscarded);
  1486. inStream.Read(mCanPushCharacter);
  1487. mUserData = 0; // Cannot restore user data
  1488. mMaterial = PhysicsMaterial::sDefault; // Cannot restore material
  1489. }
  1490. void CharacterVirtual::SaveState(StateRecorder &inStream) const
  1491. {
  1492. CharacterBase::SaveState(inStream);
  1493. inStream.Write(mPosition);
  1494. inStream.Write(mRotation);
  1495. inStream.Write(mLinearVelocity);
  1496. inStream.Write(mLastDeltaTime);
  1497. inStream.Write(mMaxHitsExceeded);
  1498. // Store contacts that had collision, we're using it at the beginning of the step in CancelVelocityTowardsSteepSlopes
  1499. uint32 num_contacts = 0;
  1500. for (const Contact &c : mActiveContacts)
  1501. if (c.mHadCollision)
  1502. ++num_contacts;
  1503. inStream.Write(num_contacts);
  1504. for (const Contact &c : mActiveContacts)
  1505. if (c.mHadCollision)
  1506. c.SaveState(inStream);
  1507. }
  1508. void CharacterVirtual::RestoreState(StateRecorder &inStream)
  1509. {
  1510. CharacterBase::RestoreState(inStream);
  1511. inStream.Read(mPosition);
  1512. inStream.Read(mRotation);
  1513. inStream.Read(mLinearVelocity);
  1514. inStream.Read(mLastDeltaTime);
  1515. inStream.Read(mMaxHitsExceeded);
  1516. // When validating remove contacts that don't have collision since we didn't save them
  1517. if (inStream.IsValidating())
  1518. for (int i = (int)mActiveContacts.size() - 1; i >= 0; --i)
  1519. if (!mActiveContacts[i].mHadCollision)
  1520. mActiveContacts.erase(mActiveContacts.begin() + i);
  1521. uint32 num_contacts = (uint32)mActiveContacts.size();
  1522. inStream.Read(num_contacts);
  1523. mActiveContacts.resize(num_contacts);
  1524. for (Contact &c : mActiveContacts)
  1525. c.RestoreState(inStream);
  1526. }
  1527. JPH_NAMESPACE_END