Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
CollideShapeVsShapePerLeaf.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2025 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
11
13
29template <class LeafCollector>
30void CollideShapeVsShapePerLeaf(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter = { })
31{
32 // Tracks information we need about a leaf shape
33 struct LeafShape
34 {
35 LeafShape() = default;
36
37 LeafShape(const AABox &inBounds, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Shape *inShape, const SubShapeIDCreator &inSubShapeIDCreator) :
38 mBounds(inBounds),
39 mCenterOfMassTransform(inCenterOfMassTransform),
40 mScale(inScale),
41 mShape(inShape),
42 mSubShapeIDCreator(inSubShapeIDCreator)
43 {
44 }
45
46 AABox mBounds;
47 Mat44 mCenterOfMassTransform;
48 Vec3 mScale;
49 const Shape * mShape;
50 SubShapeIDCreator mSubShapeIDCreator;
51 };
52
53 constexpr uint cMaxLocalLeafShapes = 32;
54
55 // A collector that stores the information we need from a leaf shape in an array that is usually on the stack but can fall back to the heap if needed
56 class MyCollector : public TransformedShapeCollector
57 {
58 public:
59 MyCollector()
60 {
61 mHits.reserve(cMaxLocalLeafShapes);
62 }
63
64 void AddHit(const TransformedShape &inShape) override
65 {
66 mHits.emplace_back(inShape.GetWorldSpaceBounds(), inShape.GetCenterOfMassTransform().ToMat44(), inShape.GetShapeScale(), inShape.mShape, inShape.mSubShapeIDCreator);
67 }
68
70 };
71
72 // Get bounds of both shapes
73 AABox bounds1 = inShape1->GetWorldSpaceBounds(inCenterOfMassTransform1, inScale1);
74 AABox bounds2 = inShape2->GetWorldSpaceBounds(inCenterOfMassTransform2, inScale2);
75
76 // Get leaf shapes that overlap with the bounds of the other shape
77 MyCollector leaf_shapes1, leaf_shapes2;
78 inShape1->CollectTransformedShapes(bounds2, inCenterOfMassTransform1.GetTranslation(), inCenterOfMassTransform1.GetQuaternion(), inScale1, inSubShapeIDCreator1, leaf_shapes1, inShapeFilter);
79 inShape2->CollectTransformedShapes(bounds1, inCenterOfMassTransform2.GetTranslation(), inCenterOfMassTransform2.GetQuaternion(), inScale2, inSubShapeIDCreator2, leaf_shapes2, inShapeFilter);
80
81 // Now test each leaf shape against each other leaf
82 for (const LeafShape &leaf1 : leaf_shapes1.mHits)
83 for (const LeafShape &leaf2 : leaf_shapes2.mHits)
84 if (leaf1.mBounds.Overlaps(leaf2.mBounds))
85 {
86 // Use the leaf collector to collect max 1 hit for this pair and pass it on to ioCollector
87 LeafCollector collector;
88 CollisionDispatch::sCollideShapeVsShape(leaf1.mShape, leaf2.mShape, leaf1.mScale, leaf2.mScale, leaf1.mCenterOfMassTransform, leaf2.mCenterOfMassTransform, leaf1.mSubShapeIDCreator, leaf2.mSubShapeIDCreator, inCollideShapeSettings, collector, inShapeFilter);
89 if (collector.HadHit())
90 ioCollector.AddHit(collector.mHit);
91 }
92}
93
JPH_NAMESPACE_BEGIN void CollideShapeVsShapePerLeaf(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter={ })
Definition CollideShapeVsShapePerLeaf.h:30
unsigned int uint
Definition Core.h:493
#define JPH_NAMESPACE_END
Definition Core.h:419
#define JPH_NAMESPACE_BEGIN
Definition Core.h:413
Axis aligned box.
Definition AABox.h:16
Definition Array.h:36
Settings to be passed with a collision query.
Definition CollideShape.h:94
Virtual interface that allows collecting multiple collision results.
Definition CollisionCollector.h:45
virtual void AddHit(const ResultType &inResult)=0
This function will be called for every hit found, it's up to the application to decide how to store t...
static void sCollideShapeVsShape(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter={ })
Definition CollisionDispatch.h:33
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
JPH_INLINE Quat GetQuaternion() const
Convert to quaternion.
Definition Mat44.inl:783
JPH_INLINE Mat44 ToMat44() const
In single precision mode just return the matrix itself.
Definition Mat44.h:225
JPH_INLINE Vec3 GetTranslation() const
Definition Mat44.h:152
Filter class.
Definition ShapeFilter.h:17
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition Shape.h:186
virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
Definition Shape.cpp:52
virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
Definition Shape.h:222
Definition SubShapeID.h:108
Definition TransformedShape.h:26
AABox GetWorldSpaceBounds() const
Get the world space bounding box for this transformed shape.
Definition TransformedShape.h:123
SubShapeIDCreator mSubShapeIDCreator
Optional sub shape ID creator for the shape (can be used when expanding compound shapes into multiple...
Definition TransformedShape.h:188
Vec3 GetShapeScale() const
Get/set the scale of the shape as a Vec3.
Definition TransformedShape.h:89
RefConst< Shape > mShape
The shape itself.
Definition TransformedShape.h:185
RMat44 GetCenterOfMassTransform() const
Calculates the transform for this shape's center of mass (excluding scale)
Definition TransformedShape.h:93
Definition Vec3.h:17