Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Collision/Shape/DecoratedShape.h>7#include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>89JPH_NAMESPACE_BEGIN1011class CollideShapeSettings;1213/// Class that constructs a RotatedTranslatedShape14class JPH_EXPORT RotatedTranslatedShapeSettings final : public DecoratedShapeSettings15{16JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, RotatedTranslatedShapeSettings)1718public:19/// Constructor20RotatedTranslatedShapeSettings() = default;2122/// Construct with shape settings, can be serialized.23RotatedTranslatedShapeSettings(Vec3Arg inPosition, QuatArg inRotation, const ShapeSettings *inShape) : DecoratedShapeSettings(inShape), mPosition(inPosition), mRotation(inRotation) { }2425/// Variant that uses a concrete shape, which means this object cannot be serialized.26RotatedTranslatedShapeSettings(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape): DecoratedShapeSettings(inShape), mPosition(inPosition), mRotation(inRotation) { }2728// See: ShapeSettings29virtual ShapeResult Create() const override;3031Vec3 mPosition; ///< Position of the sub shape32Quat mRotation; ///< Rotation of the sub shape33};3435/// A rotated translated shape will rotate and translate a child shape.36/// Shifts the child object so that it is centered around the center of mass.37class JPH_EXPORT RotatedTranslatedShape final : public DecoratedShape38{39public:40JPH_OVERRIDE_NEW_DELETE4142/// Constructor43RotatedTranslatedShape() : DecoratedShape(EShapeSubType::RotatedTranslated) { }44RotatedTranslatedShape(const RotatedTranslatedShapeSettings &inSettings, ShapeResult &outResult);45RotatedTranslatedShape(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape);4647/// Access the rotation that is applied to the inner shape48Quat GetRotation() const { return mRotation; }4950/// Access the translation that has been applied to the inner shape51Vec3 GetPosition() const { return mCenterOfMass - mRotation * mInnerShape->GetCenterOfMass(); }5253// See Shape::GetCenterOfMass54virtual Vec3 GetCenterOfMass() const override { return mCenterOfMass; }5556// See Shape::GetLocalBounds57virtual AABox GetLocalBounds() const override;5859// See Shape::GetWorldSpaceBounds60virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;61using Shape::GetWorldSpaceBounds;6263// See Shape::GetInnerRadius64virtual float GetInnerRadius() const override { return mInnerShape->GetInnerRadius(); }6566// See Shape::GetMassProperties67virtual MassProperties GetMassProperties() const override;6869// See Shape::GetSubShapeTransformedShape70virtual TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const override;7172// See Shape::GetSurfaceNormal73virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;7475// See Shape::GetSupportingFace76virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;7778// See Shape::GetSubmergedVolume79virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;8081#ifdef JPH_DEBUG_RENDERER82// See Shape::Draw83virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;8485// See Shape::DrawGetSupportFunction86virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;8788// See Shape::DrawGetSupportingFace89virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;90#endif // JPH_DEBUG_RENDERER9192// See Shape::CastRay93virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;94virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;9596// See: Shape::CollidePoint97virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;9899// See: Shape::CollideSoftBodyVertices100virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;101102// See Shape::CollectTransformedShapes103virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override;104105// See Shape::TransformShape106virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;107108// See Shape::GetTrianglesStart109virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override { JPH_ASSERT(false, "Cannot call on non-leaf shapes, use CollectTransformedShapes to collect the leaves first!"); }110111// See Shape::GetTrianglesNext112virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override { JPH_ASSERT(false, "Cannot call on non-leaf shapes, use CollectTransformedShapes to collect the leaves first!"); return 0; }113114// See Shape115virtual void SaveBinaryState(StreamOut &inStream) const override;116117// See Shape::GetStats118virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }119120// See Shape::GetVolume121virtual float GetVolume() const override { return mInnerShape->GetVolume(); }122123// See Shape::IsValidScale124virtual bool IsValidScale(Vec3Arg inScale) const override;125126// See Shape::MakeScaleValid127virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;128129/// Transform the scale to the local space of the child shape130inline Vec3 TransformScale(Vec3Arg inScale) const131{132// We don't need to transform uniform scale or if the rotation is identity133if (mIsRotationIdentity || ScaleHelpers::IsUniformScale(inScale))134return inScale;135136return ScaleHelpers::RotateScale(mRotation, inScale);137}138139// Register shape functions with the registry140static void sRegister();141142protected:143// See: Shape::RestoreBinaryState144virtual void RestoreBinaryState(StreamIn &inStream) override;145146private:147// Helper functions called by CollisionDispatch148static void sCollideRotatedTranslatedVsShape(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);149static void sCollideShapeVsRotatedTranslated(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);150static void sCollideRotatedTranslatedVsRotatedTranslated(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);151static void sCastRotatedTranslatedVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);152static void sCastShapeVsRotatedTranslated(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);153static void sCastRotatedTranslatedVsRotatedTranslated(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);154155bool mIsRotationIdentity; ///< If mRotation is close to identity (put here because it falls in padding bytes)156Vec3 mCenterOfMass; ///< Position of the center of mass157Quat mRotation; ///< Rotation of the child shape158};159160JPH_NAMESPACE_END161162163