Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Collision/TransformedShape.cpp

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #include <Jolt/Jolt.h>
6
7 #include <Jolt/Physics/Collision/TransformedShape.h>
8 #include <Jolt/Physics/Collision/RayCast.h>
9 #include <Jolt/Physics/Collision/ShapeCast.h>
10 #include <Jolt/Physics/Collision/CastResult.h>
11 #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
12 #include <Jolt/Physics/Collision/CollisionDispatch.h>
13 #include <Jolt/Geometry/OrientedBox.h>
14
15 JPH_NAMESPACE_BEGIN
16
17 bool TransformedShape::CastRay(const RRayCast &inRay, RayCastResult &ioHit) const
18 {
19 if (mShape != nullptr)
20 {
21 // Transform the ray to local space, note that this drops precision which is possible because we're in local space now
22 RayCast ray(inRay.Transformed(GetInverseCenterOfMassTransform()));
23
24 // Scale the ray
25 Vec3 inv_scale = GetShapeScale().Reciprocal();
26 ray.mOrigin *= inv_scale;
27 ray.mDirection *= inv_scale;
28
29 // Cast the ray on the shape
30 SubShapeIDCreator sub_shape_id(mSubShapeIDCreator);
31 if (mShape->CastRay(ray, sub_shape_id, ioHit))
32 {
33 // Set body ID on the hit result
34 ioHit.mBodyID = mBodyID;
35
36 return true;
37 }
38 }
39
40 return false;
41 }
42
43 void TransformedShape::CastRay(const RRayCast &inRay, const RayCastSettings &inRayCastSettings, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
44 {
45 if (mShape != nullptr)
46 {
47 // Set the context on the collector and filter
48 ioCollector.SetContext(this);
49 inShapeFilter.mBodyID2 = mBodyID;
50
51 // Transform the ray to local space, note that this drops precision which is possible because we're in local space now
52 RayCast ray(inRay.Transformed(GetInverseCenterOfMassTransform()));
53
54 // Scale the ray
55 Vec3 inv_scale = GetShapeScale().Reciprocal();
56 ray.mOrigin *= inv_scale;
57 ray.mDirection *= inv_scale;
58
59 // Cast the ray on the shape
60 SubShapeIDCreator sub_shape_id(mSubShapeIDCreator);
61 mShape->CastRay(ray, inRayCastSettings, sub_shape_id, ioCollector, inShapeFilter);
62 }
63 }
64
65 void TransformedShape::CollidePoint(RVec3Arg inPoint, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
66 {
67 if (mShape != nullptr)
68 {
69 // Set the context on the collector and filter
70 ioCollector.SetContext(this);
71 inShapeFilter.mBodyID2 = mBodyID;
72
73 // Transform and scale the point to local space
74 Vec3 point = Vec3(GetInverseCenterOfMassTransform() * inPoint) / GetShapeScale();
75
76 // Do point collide on the shape
77 SubShapeIDCreator sub_shape_id(mSubShapeIDCreator);
78 mShape->CollidePoint(point, sub_shape_id, ioCollector, inShapeFilter);
79 }
80 }
81
82 void TransformedShape::CollideShape(const Shape *inShape, Vec3Arg inShapeScale, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
83 {
84 if (mShape != nullptr)
85 {
86 // Set the context on the collector and filter
87 ioCollector.SetContext(this);
88 inShapeFilter.mBodyID2 = mBodyID;
89
90 SubShapeIDCreator sub_shape_id1, sub_shape_id2(mSubShapeIDCreator);
91 Mat44 transform1 = inCenterOfMassTransform.PostTranslated(-inBaseOffset).ToMat44();
92 Mat44 transform2 = GetCenterOfMassTransform().PostTranslated(-inBaseOffset).ToMat44();
93 CollisionDispatch::sCollideShapeVsShape(inShape, mShape, inShapeScale, GetShapeScale(), transform1, transform2, sub_shape_id1, sub_shape_id2, inCollideShapeSettings, ioCollector, inShapeFilter);
94 }
95 }
96
97 void TransformedShape::CastShape(const RShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
98 {
99 if (mShape != nullptr)
100 {
101 // Set the context on the collector and filter
102 ioCollector.SetContext(this);
103 inShapeFilter.mBodyID2 = mBodyID;
104
105 // Get the shape cast relative to the base offset and convert it to floats
106 ShapeCast shape_cast(inShapeCast.PostTranslated(-inBaseOffset));
107
108 // Get center of mass of object we're casting against relative to the base offset and convert it to floats
109 Mat44 center_of_mass_transform2 = GetCenterOfMassTransform().PostTranslated(-inBaseOffset).ToMat44();
110
111 SubShapeIDCreator sub_shape_id1, sub_shape_id2(mSubShapeIDCreator);
112 CollisionDispatch::sCastShapeVsShapeWorldSpace(shape_cast, inShapeCastSettings, mShape, GetShapeScale(), inShapeFilter, center_of_mass_transform2, sub_shape_id1, sub_shape_id2, ioCollector);
113 }
114 }
115
116 void TransformedShape::CollectTransformedShapes(const AABox &inBox, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
117 {
118 if (mShape != nullptr)
119 {
120 struct MyCollector : public TransformedShapeCollector
121 {
122 MyCollector(TransformedShapeCollector &ioCollector, RVec3 inShapePositionCOM) :
123 TransformedShapeCollector(ioCollector),
124 mCollector(ioCollector),
125 mShapePositionCOM(inShapePositionCOM)
126 {
127 }
128
129 virtual void AddHit(const TransformedShape &inResult) override
130 {
131 // Apply the center of mass offset
132 TransformedShape ts = inResult;
133 ts.mShapePositionCOM += mShapePositionCOM;
134
135 // Pass hit on to child collector
136 mCollector.AddHit(ts);
137
138 // Update early out fraction based on child collector
139 UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
140 }
141
142 TransformedShapeCollector & mCollector;
143 RVec3 mShapePositionCOM;
144 };
145
146 // Set the context on the collector
147 ioCollector.SetContext(this);
148
149 // Wrap the collector so we can add the center of mass precision, we do this to avoid losing precision because CollectTransformedShapes uses single precision floats
150 MyCollector collector(ioCollector, mShapePositionCOM);
151
152 // Take box to local space for the shape
153 AABox box = inBox;
154 box.Translate(-mShapePositionCOM);
155
156 mShape->CollectTransformedShapes(box, Vec3::sZero(), mShapeRotation, GetShapeScale(), mSubShapeIDCreator, collector, inShapeFilter);
157 }
158 }
159
160 void TransformedShape::GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, RVec3Arg inBaseOffset) const
161 {
162 if (mShape != nullptr)
163 {
164 // Take box to local space for the shape
165 AABox box = inBox;
166 box.Translate(-inBaseOffset);
167
168 mShape->GetTrianglesStart(ioContext, box, Vec3(mShapePositionCOM - inBaseOffset), mShapeRotation, GetShapeScale());
169 }
170 }
171
172 int TransformedShape::GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials) const
173 {
174 if (mShape != nullptr)
175 return mShape->GetTrianglesNext(ioContext, inMaxTrianglesRequested, outTriangleVertices, outMaterials);
176 else
177 return 0;
178 }
179
180 JPH_NAMESPACE_END