123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
- // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
- // SPDX-License-Identifier: MIT
- #include <Jolt/Jolt.h>
- #include <Jolt/Physics/Collision/Shape/BoxShape.h>
- #include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
- #include <Jolt/Physics/Collision/Shape/GetTrianglesContext.h>
- #include <Jolt/Physics/Collision/RayCast.h>
- #include <Jolt/Physics/Collision/CastResult.h>
- #include <Jolt/Physics/Collision/CollidePointResult.h>
- #include <Jolt/Physics/Collision/TransformedShape.h>
- #include <Jolt/Geometry/RayAABox.h>
- #include <Jolt/ObjectStream/TypeDeclarations.h>
- #include <Jolt/Core/StreamIn.h>
- #include <Jolt/Core/StreamOut.h>
- #ifdef JPH_DEBUG_RENDERER
- #include <Jolt/Renderer/DebugRenderer.h>
- #endif // JPH_DEBUG_RENDERER
- JPH_NAMESPACE_BEGIN
- JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(BoxShapeSettings)
- {
- JPH_ADD_BASE_CLASS(BoxShapeSettings, ConvexShapeSettings)
- JPH_ADD_ATTRIBUTE(BoxShapeSettings, mHalfExtent)
- JPH_ADD_ATTRIBUTE(BoxShapeSettings, mConvexRadius)
- }
- static const Vec3 sUnitBoxTriangles[] = {
- Vec3(-1, 1, -1), Vec3(-1, 1, 1), Vec3(1, 1, 1),
- Vec3(-1, 1, -1), Vec3(1, 1, 1), Vec3(1, 1, -1),
- Vec3(-1, -1, -1), Vec3(1, -1, -1), Vec3(1, -1, 1),
- Vec3(-1, -1, -1), Vec3(1, -1, 1), Vec3(-1, -1, 1),
- Vec3(-1, 1, -1), Vec3(-1, -1, -1), Vec3(-1, -1, 1),
- Vec3(-1, 1, -1), Vec3(-1, -1, 1), Vec3(-1, 1, 1),
- Vec3(1, 1, 1), Vec3(1, -1, 1), Vec3(1, -1, -1),
- Vec3(1, 1, 1), Vec3(1, -1, -1), Vec3(1, 1, -1),
- Vec3(-1, 1, 1), Vec3(-1, -1, 1), Vec3(1, -1, 1),
- Vec3(-1, 1, 1), Vec3(1, -1, 1), Vec3(1, 1, 1),
- Vec3(-1, 1, -1), Vec3(1, 1, -1), Vec3(1, -1, -1),
- Vec3(-1, 1, -1), Vec3(1, -1, -1), Vec3(-1, -1, -1)
- };
- ShapeSettings::ShapeResult BoxShapeSettings::Create() const
- {
- if (mCachedResult.IsEmpty())
- Ref<Shape> shape = new BoxShape(*this, mCachedResult);
- return mCachedResult;
- }
- BoxShape::BoxShape(const BoxShapeSettings &inSettings, ShapeResult &outResult) :
- ConvexShape(EShapeSubType::Box, inSettings, outResult),
- mHalfExtent(inSettings.mHalfExtent),
- mConvexRadius(inSettings.mConvexRadius)
- {
- // Check convex radius
- if (inSettings.mConvexRadius < 0.0f
- || inSettings.mHalfExtent.ReduceMin() <= inSettings.mConvexRadius)
- {
- outResult.SetError("Invalid convex radius");
- return;
- }
- // Result is valid
- outResult.Set(this);
- }
- class BoxShape::Box final : public Support
- {
- public:
- Box(const AABox &inBox, float inConvexRadius) :
- mBox(inBox),
- mConvexRadius(inConvexRadius)
- {
- static_assert(sizeof(Box) <= sizeof(SupportBuffer), "Buffer size too small");
- JPH_ASSERT(IsAligned(this, alignof(Box)));
- }
- virtual Vec3 GetSupport(Vec3Arg inDirection) const override
- {
- return mBox.GetSupport(inDirection);
- }
- virtual float GetConvexRadius() const override
- {
- return mConvexRadius;
- }
- private:
- AABox mBox;
- float mConvexRadius;
- };
- const ConvexShape::Support *BoxShape::GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const
- {
- // Scale our half extents
- Vec3 scaled_half_extent = inScale.Abs() * mHalfExtent;
- switch (inMode)
- {
- case ESupportMode::IncludeConvexRadius:
- {
- // Make box out of our half extents
- AABox box = AABox(-scaled_half_extent, scaled_half_extent);
- JPH_ASSERT(box.IsValid());
- return new (&inBuffer) Box(box, 0.0f);
- }
- case ESupportMode::ExcludeConvexRadius:
- {
- // Reduce the box by our convex radius
- float convex_radius = ScaleHelpers::ScaleConvexRadius(mConvexRadius, inScale);
- Vec3 convex_radius3 = Vec3::sReplicate(convex_radius);
- Vec3 reduced_half_extent = scaled_half_extent - convex_radius3;
- AABox box = AABox(-reduced_half_extent, reduced_half_extent);
- JPH_ASSERT(box.IsValid());
- return new (&inBuffer) Box(box, convex_radius);
- }
- }
- JPH_ASSERT(false);
- return nullptr;
- }
- void BoxShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
- {
- JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID");
- Vec3 scaled_half_extent = inScale.Abs() * mHalfExtent;
- AABox box(-scaled_half_extent, scaled_half_extent);
- box.GetSupportingFace(inDirection, outVertices);
- // Transform to world space
- for (Vec3 &v : outVertices)
- v = inCenterOfMassTransform * v;
- }
- MassProperties BoxShape::GetMassProperties() const
- {
- MassProperties p;
- p.SetMassAndInertiaOfSolidBox(2.0f * mHalfExtent, GetDensity());
- return p;
- }
- Vec3 BoxShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const
- {
- JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID");
- // Get component that is closest to the surface of the box
- int index = (inLocalSurfacePosition.Abs() - mHalfExtent).Abs().GetLowestComponentIndex();
- // Calculate normal
- Vec3 normal = Vec3::sZero();
- normal.SetComponent(index, inLocalSurfacePosition[index] > 0.0f? 1.0f : -1.0f);
- return normal;
- }
- #ifdef JPH_DEBUG_RENDERER
- void BoxShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
- {
- DebugRenderer::EDrawMode draw_mode = inDrawWireframe? DebugRenderer::EDrawMode::Wireframe : DebugRenderer::EDrawMode::Solid;
- inRenderer->DrawBox(inCenterOfMassTransform * Mat44::sScale(inScale.Abs()), GetLocalBounds(), inUseMaterialColors? GetMaterial()->GetDebugColor() : inColor, DebugRenderer::ECastShadow::On, draw_mode);
- }
- #endif // JPH_DEBUG_RENDERER
- bool BoxShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
- {
- // Test hit against box
- float fraction = max(RayAABox(inRay.mOrigin, RayInvDirection(inRay.mDirection), -mHalfExtent, mHalfExtent), 0.0f);
- if (fraction < ioHit.mFraction)
- {
- ioHit.mFraction = fraction;
- ioHit.mSubShapeID2 = inSubShapeIDCreator.GetID();
- return true;
- }
- return false;
- }
- void BoxShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
- {
- // Test shape filter
- if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
- return;
- float min_fraction, max_fraction;
- RayAABox(inRay.mOrigin, RayInvDirection(inRay.mDirection), -mHalfExtent, mHalfExtent, min_fraction, max_fraction);
- if (min_fraction <= max_fraction // Ray should intersect
- && max_fraction >= 0.0f // End of ray should be inside box
- && min_fraction < ioCollector.GetEarlyOutFraction()) // Start of ray should be before early out fraction
- {
- // Better hit than the current hit
- RayCastResult hit;
- hit.mBodyID = TransformedShape::sGetBodyID(ioCollector.GetContext());
- hit.mSubShapeID2 = inSubShapeIDCreator.GetID();
- // Check front side
- if (inRayCastSettings.mTreatConvexAsSolid || min_fraction > 0.0f)
- {
- hit.mFraction = max(0.0f, min_fraction);
- ioCollector.AddHit(hit);
- }
- // Check back side hit
- if (inRayCastSettings.mBackFaceMode == EBackFaceMode::CollideWithBackFaces
- && max_fraction < ioCollector.GetEarlyOutFraction())
- {
- hit.mFraction = max_fraction;
- ioCollector.AddHit(hit);
- }
- }
- }
- void BoxShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
- {
- // Test shape filter
- if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
- return;
- if (Vec3::sLessOrEqual(inPoint.Abs(), mHalfExtent).TestAllXYZTrue())
- ioCollector.AddHit({ TransformedShape::sGetBodyID(ioCollector.GetContext()), inSubShapeIDCreator.GetID() });
- }
- void BoxShape::GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const
- {
- new (&ioContext) GetTrianglesContextVertexList(inPositionCOM, inRotation, inScale, Mat44::sScale(mHalfExtent), sUnitBoxTriangles, sizeof(sUnitBoxTriangles) / sizeof(Vec3), GetMaterial());
- }
- int BoxShape::GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials) const
- {
- return ((GetTrianglesContextVertexList &)ioContext).GetTrianglesNext(inMaxTrianglesRequested, outTriangleVertices, outMaterials);
- }
- void BoxShape::SaveBinaryState(StreamOut &inStream) const
- {
- ConvexShape::SaveBinaryState(inStream);
- inStream.Write(mHalfExtent);
- inStream.Write(mConvexRadius);
- }
- void BoxShape::RestoreBinaryState(StreamIn &inStream)
- {
- ConvexShape::RestoreBinaryState(inStream);
- inStream.Read(mHalfExtent);
- inStream.Read(mConvexRadius);
- }
- void BoxShape::sRegister()
- {
- ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::Box);
- f.mConstruct = []() -> Shape * { return new BoxShape; };
- f.mColor = Color::sGreen;
- }
- JPH_NAMESPACE_END
|