Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Body/Body.inl

raw
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 JPH_NAMESPACE_BEGIN
8
9 RMat44 Body::GetWorldTransform() const
10 {
11 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess(), BodyAccess::EAccess::Read));
12
13 return RMat44::sRotationTranslation(mRotation, mPosition).PreTranslated(-mShape->GetCenterOfMass());
14 }
15
16 RMat44 Body::GetCenterOfMassTransform() const
17 {
18 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess(), BodyAccess::EAccess::Read));
19
20 return RMat44::sRotationTranslation(mRotation, mPosition);
21 }
22
23 RMat44 Body::GetInverseCenterOfMassTransform() const
24 {
25 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess(), BodyAccess::EAccess::Read));
26
27 return RMat44::sInverseRotationTranslation(mRotation, mPosition);
28 }
29
30 inline bool Body::sFindCollidingPairsCanCollide(const Body &inBody1, const Body &inBody2)
31 {
32 // First body should never be a soft body
33 JPH_ASSERT(!inBody1.IsSoftBody());
34
35 // One of these conditions must be true
36 // - We always allow detecting collisions between kinematic and non-dynamic bodies
37 // - One of the bodies must be dynamic to collide
38 // - A kinematic object can collide with a sensor
39 if (!inBody1.GetCollideKinematicVsNonDynamic()
40 && !inBody2.GetCollideKinematicVsNonDynamic()
41 && (!inBody1.IsDynamic() && !inBody2.IsDynamic())
42 && !(inBody1.IsKinematic() && inBody2.IsSensor())
43 && !(inBody2.IsKinematic() && inBody1.IsSensor()))
44 return false;
45
46 // Check that body 1 is active
47 uint32 body1_index_in_active_bodies = inBody1.GetIndexInActiveBodiesInternal();
48 JPH_ASSERT(!inBody1.IsStatic() && body1_index_in_active_bodies != Body::cInactiveIndex, "This function assumes that Body 1 is active");
49
50 // If the pair A, B collides we need to ensure that the pair B, A does not collide or else we will handle the collision twice.
51 // If A is the same body as B we don't want to collide (1)
52 // If A is dynamic / kinematic and B is static we should collide (2)
53 // If A is dynamic / kinematic and B is dynamic / kinematic we should only collide if
54 // - A is active and B is not active (3)
55 // - A is active and B will become active during this simulation step (4)
56 // - A is active and B is active, we require a condition that makes A, B collide and B, A not (5)
57 //
58 // In order to implement this we use the index in the active body list and make use of the fact that
59 // a body not in the active list has Body.Index = 0xffffffff which is the highest possible value for an uint32.
60 //
61 // Because we know that A is active we know that A.Index != 0xffffffff:
62 // (1) Because A.Index != 0xffffffff, if A.Index = B.Index then A = B, so to collide A.Index != B.Index
63 // (2) A.Index != 0xffffffff, B.Index = 0xffffffff (because it's static and cannot be in the active list), so to collide A.Index != B.Index
64 // (3) A.Index != 0xffffffff, B.Index = 0xffffffff (because it's not yet active), so to collide A.Index != B.Index
65 // (4) A.Index != 0xffffffff, B.Index = 0xffffffff currently. But it can activate during the Broad/NarrowPhase step at which point it
66 // will be added to the end of the active list which will make B.Index > A.Index (this holds only true when we don't deactivate
67 // bodies during the Broad/NarrowPhase step), so to collide A.Index < B.Index.
68 // (5) As tie breaker we can use the same condition A.Index < B.Index to collide, this means that if A, B collides then B, A won't
69 static_assert(Body::cInactiveIndex == 0xffffffff, "The algorithm below uses this value");
70 if (!inBody2.IsSoftBody() && body1_index_in_active_bodies >= inBody2.GetIndexInActiveBodiesInternal())
71 return false;
72 JPH_ASSERT(inBody1.GetID() != inBody2.GetID(), "Read the comment above, A and B are the same body which should not be possible!");
73
74 // Check collision group filter
75 if (!inBody1.GetCollisionGroup().CanCollide(inBody2.GetCollisionGroup()))
76 return false;
77
78 return true;
79 }
80
81 void Body::AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
82 {
83 JPH_ASSERT(IsRigidBody());
84 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess(), BodyAccess::EAccess::ReadWrite));
85
86 // This used to use the equation: d/dt R(t) = 1/2 * w(t) * R(t) so that R(t + dt) = R(t) + 1/2 * w(t) * R(t) * dt
87 // See: Appendix B of An Introduction to Physically Based Modeling: Rigid Body Simulation II-Nonpenetration Constraints
88 // URL: https://www.cs.cmu.edu/~baraff/sigcourse/notesd2.pdf
89 // But this is a first order approximation and does not work well for kinematic ragdolls that are driven to a new
90 // pose if the poses differ enough. So now we split w(t) * dt into an axis and angle part and create a quaternion with it.
91 // Note that the resulting quaternion is normalized since otherwise numerical drift will eventually make the rotation non-normalized.
92 float len = inAngularVelocityTimesDeltaTime.Length();
93 if (len > 1.0e-6f)
94 {
95 mRotation = (Quat::sRotation(inAngularVelocityTimesDeltaTime / len, len) * mRotation).Normalized();
96 JPH_ASSERT(!mRotation.IsNaN());
97 }
98 }
99
100 void Body::SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
101 {
102 JPH_ASSERT(IsRigidBody());
103 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess(), BodyAccess::EAccess::ReadWrite));
104
105 // See comment at Body::AddRotationStep
106 float len = inAngularVelocityTimesDeltaTime.Length();
107 if (len > 1.0e-6f)
108 {
109 mRotation = (Quat::sRotation(inAngularVelocityTimesDeltaTime / len, -len) * mRotation).Normalized();
110 JPH_ASSERT(!mRotation.IsNaN());
111 }
112 }
113
114 Vec3 Body::GetWorldSpaceSurfaceNormal(const SubShapeID &inSubShapeID, RVec3Arg inPosition) const
115 {
116 RMat44 inv_com = GetInverseCenterOfMassTransform();
117 return inv_com.Multiply3x3Transposed(mShape->GetSurfaceNormal(inSubShapeID, Vec3(inv_com * inPosition))).Normalized();
118 }
119
120 Mat44 Body::GetInverseInertia() const
121 {
122 JPH_ASSERT(IsDynamic());
123
124 return GetMotionProperties()->GetInverseInertiaForRotation(Mat44::sRotation(mRotation));
125 }
126
127 void Body::AddForce(Vec3Arg inForce, RVec3Arg inPosition)
128 {
129 AddForce(inForce);
130 AddTorque(Vec3(inPosition - mPosition).Cross(inForce));
131 }
132
133 void Body::AddImpulse(Vec3Arg inImpulse)
134 {
135 JPH_ASSERT(IsDynamic());
136
137 SetLinearVelocityClamped(mMotionProperties->GetLinearVelocity() + inImpulse * mMotionProperties->GetInverseMass());
138 }
139
140 void Body::AddImpulse(Vec3Arg inImpulse, RVec3Arg inPosition)
141 {
142 JPH_ASSERT(IsDynamic());
143
144 SetLinearVelocityClamped(mMotionProperties->GetLinearVelocity() + inImpulse * mMotionProperties->GetInverseMass());
145
146 SetAngularVelocityClamped(mMotionProperties->GetAngularVelocity() + mMotionProperties->MultiplyWorldSpaceInverseInertiaByVector(mRotation, Vec3(inPosition - mPosition).Cross(inImpulse)));
147 }
148
149 void Body::AddAngularImpulse(Vec3Arg inAngularImpulse)
150 {
151 JPH_ASSERT(IsDynamic());
152
153 SetAngularVelocityClamped(mMotionProperties->GetAngularVelocity() + mMotionProperties->MultiplyWorldSpaceInverseInertiaByVector(mRotation, inAngularImpulse));
154 }
155
156 void Body::GetSleepTestPoints(RVec3 *outPoints) const
157 {
158 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess(), BodyAccess::EAccess::Read));
159
160 // Center of mass is the first position
161 outPoints[0] = mPosition;
162
163 // The second and third position are on the largest axis of the bounding box
164 Vec3 extent = mShape->GetLocalBounds().GetExtent();
165 int lowest_component = extent.GetLowestComponentIndex();
166 Mat44 rotation = Mat44::sRotation(mRotation);
167 switch (lowest_component)
168 {
169 case 0:
170 outPoints[1] = mPosition + extent.GetY() * rotation.GetColumn3(1);
171 outPoints[2] = mPosition + extent.GetZ() * rotation.GetColumn3(2);
172 break;
173
174 case 1:
175 outPoints[1] = mPosition + extent.GetX() * rotation.GetColumn3(0);
176 outPoints[2] = mPosition + extent.GetZ() * rotation.GetColumn3(2);
177 break;
178
179 case 2:
180 outPoints[1] = mPosition + extent.GetX() * rotation.GetColumn3(0);
181 outPoints[2] = mPosition + extent.GetY() * rotation.GetColumn3(1);
182 break;
183
184 default:
185 JPH_ASSERT(false);
186 break;
187 }
188 }
189
190 void Body::ResetSleepTimer()
191 {
192 RVec3 points[3];
193 GetSleepTestPoints(points);
194 mMotionProperties->ResetSleepTestSpheres(points);
195 }
196
197 JPH_NAMESPACE_END