Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/ScaledShape.cpp
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Physics/Collision/Shape/ScaledShape.h>7#include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>8#include <Jolt/Physics/Collision/RayCast.h>9#include <Jolt/Physics/Collision/ShapeCast.h>10#include <Jolt/Physics/Collision/TransformedShape.h>11#include <Jolt/Physics/Collision/CollisionDispatch.h>12#include <Jolt/ObjectStream/TypeDeclarations.h>13#include <Jolt/Core/StreamIn.h>14#include <Jolt/Core/StreamOut.h>1516JPH_NAMESPACE_BEGIN1718JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(ScaledShapeSettings)19{20JPH_ADD_BASE_CLASS(ScaledShapeSettings, DecoratedShapeSettings)2122JPH_ADD_ATTRIBUTE(ScaledShapeSettings, mScale)23}2425ShapeSettings::ShapeResult ScaledShapeSettings::Create() const26{27if (mCachedResult.IsEmpty())28Ref<Shape> shape = new ScaledShape(*this, mCachedResult);29return mCachedResult;30}3132ScaledShape::ScaledShape(const ScaledShapeSettings &inSettings, ShapeResult &outResult) :33DecoratedShape(EShapeSubType::Scaled, inSettings, outResult),34mScale(inSettings.mScale)35{36if (outResult.HasError())37return;3839if (ScaleHelpers::IsZeroScale(inSettings.mScale))40{41outResult.SetError("Can't use zero scale!");42return;43}4445outResult.Set(this);46}4748MassProperties ScaledShape::GetMassProperties() const49{50MassProperties p = mInnerShape->GetMassProperties();51p.Scale(mScale);52return p;53}5455AABox ScaledShape::GetLocalBounds() const56{57return mInnerShape->GetLocalBounds().Scaled(mScale);58}5960AABox ScaledShape::GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const61{62return mInnerShape->GetWorldSpaceBounds(inCenterOfMassTransform, inScale * mScale);63}6465TransformedShape ScaledShape::GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const66{67// We don't use any bits in the sub shape ID68outRemainder = inSubShapeID;6970TransformedShape ts(RVec3(inPositionCOM), inRotation, mInnerShape, BodyID());71ts.SetShapeScale(inScale * mScale);72return ts;73}7475Vec3 ScaledShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const76{77// Transform the surface point to local space and pass the query on78Vec3 normal = mInnerShape->GetSurfaceNormal(inSubShapeID, inLocalSurfacePosition / mScale);7980// Need to transform the plane normals using inScale81// Transforming a direction with matrix M is done through multiplying by (M^-1)^T82// In this case M is a diagonal matrix with the scale vector, so we need to multiply our normal by 1 / scale and renormalize afterwards83return (normal / mScale).Normalized();84}8586void ScaledShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const87{88mInnerShape->GetSupportingFace(inSubShapeID, inDirection, inScale * mScale, inCenterOfMassTransform, outVertices);89}9091void ScaledShape::GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const92{93mInnerShape->GetSubmergedVolume(inCenterOfMassTransform, inScale * mScale, inSurface, outTotalVolume, outSubmergedVolume, outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, inBaseOffset));94}9596#ifdef JPH_DEBUG_RENDERER97void ScaledShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const98{99mInnerShape->Draw(inRenderer, inCenterOfMassTransform, inScale * mScale, inColor, inUseMaterialColors, inDrawWireframe);100}101102void ScaledShape::DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const103{104mInnerShape->DrawGetSupportFunction(inRenderer, inCenterOfMassTransform, inScale * mScale, inColor, inDrawSupportDirection);105}106107void ScaledShape::DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const108{109mInnerShape->DrawGetSupportingFace(inRenderer, inCenterOfMassTransform, inScale * mScale);110}111#endif // JPH_DEBUG_RENDERER112113bool ScaledShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const114{115Vec3 inv_scale = mScale.Reciprocal();116RayCast scaled_ray { inv_scale * inRay.mOrigin, inv_scale * inRay.mDirection };117return mInnerShape->CastRay(scaled_ray, inSubShapeIDCreator, ioHit);118}119120void ScaledShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const121{122// Test shape filter123if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))124return;125126Vec3 inv_scale = mScale.Reciprocal();127RayCast scaled_ray { inv_scale * inRay.mOrigin, inv_scale * inRay.mDirection };128return mInnerShape->CastRay(scaled_ray, inRayCastSettings, inSubShapeIDCreator, ioCollector, inShapeFilter);129}130131void ScaledShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const132{133// Test shape filter134if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))135return;136137Vec3 inv_scale = mScale.Reciprocal();138mInnerShape->CollidePoint(inv_scale * inPoint, inSubShapeIDCreator, ioCollector, inShapeFilter);139}140141void ScaledShape::CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const142{143mInnerShape->CollideSoftBodyVertices(inCenterOfMassTransform, inScale * mScale, inVertices, inNumVertices, inCollidingShapeIndex);144}145146void ScaledShape::CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const147{148// Test shape filter149if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))150return;151152mInnerShape->CollectTransformedShapes(inBox, inPositionCOM, inRotation, inScale * mScale, inSubShapeIDCreator, ioCollector, inShapeFilter);153}154155void ScaledShape::TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const156{157mInnerShape->TransformShape(inCenterOfMassTransform * Mat44::sScale(mScale), ioCollector);158}159160void ScaledShape::SaveBinaryState(StreamOut &inStream) const161{162DecoratedShape::SaveBinaryState(inStream);163164inStream.Write(mScale);165}166167void ScaledShape::RestoreBinaryState(StreamIn &inStream)168{169DecoratedShape::RestoreBinaryState(inStream);170171inStream.Read(mScale);172}173174float ScaledShape::GetVolume() const175{176return abs(mScale.GetX() * mScale.GetY() * mScale.GetZ()) * mInnerShape->GetVolume();177}178179bool ScaledShape::IsValidScale(Vec3Arg inScale) const180{181return mInnerShape->IsValidScale(inScale * mScale);182}183184Vec3 ScaledShape::MakeScaleValid(Vec3Arg inScale) const185{186return mInnerShape->MakeScaleValid(mScale * inScale) / mScale;187}188189void ScaledShape::sCollideScaledVsShape(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)190{191JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::Scaled);192const ScaledShape *shape1 = static_cast<const ScaledShape *>(inShape1);193194CollisionDispatch::sCollideShapeVsShape(shape1->GetInnerShape(), inShape2, inScale1 * shape1->GetScale(), inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);195}196197void ScaledShape::sCollideShapeVsScaled(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)198{199JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::Scaled);200const ScaledShape *shape2 = static_cast<const ScaledShape *>(inShape2);201202CollisionDispatch::sCollideShapeVsShape(inShape1, shape2->GetInnerShape(), inScale1, inScale2 * shape2->GetScale(), inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);203}204205void ScaledShape::sCastScaledVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)206{207JPH_ASSERT(inShapeCast.mShape->GetSubType() == EShapeSubType::Scaled);208const ScaledShape *shape = static_cast<const ScaledShape *>(inShapeCast.mShape);209210ShapeCast scaled_cast(shape->GetInnerShape(), inShapeCast.mScale * shape->GetScale(), inShapeCast.mCenterOfMassStart, inShapeCast.mDirection);211CollisionDispatch::sCastShapeVsShapeLocalSpace(scaled_cast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);212}213214void ScaledShape::sCastShapeVsScaled(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)215{216JPH_ASSERT(inShape->GetSubType() == EShapeSubType::Scaled);217const ScaledShape *shape = static_cast<const ScaledShape *>(inShape);218219CollisionDispatch::sCastShapeVsShapeLocalSpace(inShapeCast, inShapeCastSettings, shape->mInnerShape, inScale * shape->mScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);220}221222void ScaledShape::sRegister()223{224ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::Scaled);225f.mConstruct = []() -> Shape * { return new ScaledShape; };226f.mColor = Color::sYellow;227228for (EShapeSubType s : sAllSubShapeTypes)229{230CollisionDispatch::sRegisterCollideShape(EShapeSubType::Scaled, s, sCollideScaledVsShape);231CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::Scaled, sCollideShapeVsScaled);232CollisionDispatch::sRegisterCastShape(EShapeSubType::Scaled, s, sCastScaledVsShape);233CollisionDispatch::sRegisterCastShape(s, EShapeSubType::Scaled, sCastShapeVsScaled);234}235}236237JPH_NAMESPACE_END238239240