CharacterVirtual.cpp 70 KB

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