EmptyShape.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Collision/Shape/Shape.h>
  6. #include <Jolt/Physics/Collision/PhysicsMaterial.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Class that constructs an EmptyShape
  9. class JPH_EXPORT EmptyShapeSettings final : public ShapeSettings
  10. {
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, EmptyShapeSettings)
  12. public:
  13. EmptyShapeSettings() = default;
  14. explicit EmptyShapeSettings(Vec3Arg inCenterOfMass) : mCenterOfMass(inCenterOfMass) { }
  15. ShapeResult Create() const override;
  16. Vec3 mCenterOfMass = Vec3::sZero(); ///< Determines the center of mass for this shape
  17. };
  18. /// An empty shape that has no volume and collides with nothing.
  19. ///
  20. /// Possible use cases:
  21. /// - As a placeholder for a shape that will be created later. E.g. if you first need to create a body and only then know what shape it will have.
  22. /// - If you need a kinematic body to attach a constraint to, but you don't want the body to collide with anything.
  23. ///
  24. /// Note that, if possible, you should also put your body in an ObjectLayer that doesn't collide with anything.
  25. /// This ensures that collisions will be filtered out at broad phase level instead of at narrow phase level, this is more efficient.
  26. class JPH_EXPORT EmptyShape final : public Shape
  27. {
  28. public:
  29. // Constructor
  30. EmptyShape() : Shape(EShapeType::Empty, EShapeSubType::Empty) { }
  31. explicit EmptyShape(Vec3Arg inCenterOfMass) : Shape(EShapeType::Empty, EShapeSubType::Empty), mCenterOfMass(inCenterOfMass) { }
  32. EmptyShape(const EmptyShapeSettings &inSettings, ShapeResult &outResult) : Shape(EShapeType::Empty, EShapeSubType::Empty, inSettings, outResult), mCenterOfMass(inSettings.mCenterOfMass) { outResult.Set(this); }
  33. // See: Shape
  34. Vec3 GetCenterOfMass() const override { return mCenterOfMass; }
  35. AABox GetLocalBounds() const override { return { Vec3::sZero(), Vec3::sZero() }; }
  36. uint GetSubShapeIDBitsRecursive() const override { return 0; }
  37. float GetInnerRadius() const override { return 0.0f; }
  38. MassProperties GetMassProperties() const override;
  39. const PhysicsMaterial * GetMaterial([[maybe_unused]] const SubShapeID &inSubShapeID) const override { return PhysicsMaterial::sDefault; }
  40. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override { return Vec3::sZero(); }
  41. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy
  42. #ifdef JPH_DEBUG_RENDERER // Not using JPH_IF_DEBUG_RENDERER for Doxygen
  43. , RVec3Arg inBaseOffset
  44. #endif
  45. ) const override { outTotalVolume = 0.0f; outSubmergedVolume = 0.0f; outCenterOfBuoyancy = Vec3::sZero(); }
  46. #ifdef JPH_DEBUG_RENDERER
  47. virtual void Draw([[maybe_unused]] DebugRenderer *inRenderer, [[maybe_unused]] RMat44Arg inCenterOfMassTransform, [[maybe_unused]] Vec3Arg inScale, [[maybe_unused]] ColorArg inColor, [[maybe_unused]] bool inUseMaterialColors, [[maybe_unused]] bool inDrawWireframe) const override;
  48. #endif // JPH_DEBUG_RENDERER
  49. virtual bool CastRay([[maybe_unused]] const RayCast &inRay, [[maybe_unused]] const SubShapeIDCreator &inSubShapeIDCreator, [[maybe_unused]] RayCastResult &ioHit) const override { return false; }
  50. virtual void CastRay([[maybe_unused]] const RayCast &inRay, [[maybe_unused]] const RayCastSettings &inRayCastSettings, [[maybe_unused]] const SubShapeIDCreator &inSubShapeIDCreator, [[maybe_unused]] CastRayCollector &ioCollector, [[maybe_unused]] const ShapeFilter &inShapeFilter = { }) const override { /* Do nothing */ }
  51. virtual void CollidePoint([[maybe_unused]] Vec3Arg inPoint, [[maybe_unused]] const SubShapeIDCreator &inSubShapeIDCreator, [[maybe_unused]] CollidePointCollector &ioCollector, [[maybe_unused]] const ShapeFilter &inShapeFilter = { }) const override { /* Do nothing */ }
  52. virtual void CollideSoftBodyVertices([[maybe_unused]] Mat44Arg inCenterOfMassTransform, [[maybe_unused]] Vec3Arg inScale, [[maybe_unused]] const CollideSoftBodyVertexIterator &inVertices, [[maybe_unused]] uint inNumVertices, [[maybe_unused]] int inCollidingShapeIndex) const override { /* Do nothing */ }
  53. virtual void GetTrianglesStart([[maybe_unused]] GetTrianglesContext &ioContext, [[maybe_unused]] const AABox &inBox, [[maybe_unused]] Vec3Arg inPositionCOM, [[maybe_unused]] QuatArg inRotation, [[maybe_unused]] Vec3Arg inScale) const override { /* Do nothing */ }
  54. virtual int GetTrianglesNext([[maybe_unused]] GetTrianglesContext &ioContext, [[maybe_unused]] int inMaxTrianglesRequested, [[maybe_unused]] Float3 *outTriangleVertices, [[maybe_unused]] const PhysicsMaterial **outMaterials = nullptr) const override { return 0; }
  55. Stats GetStats() const override { return { sizeof(*this), 0 }; }
  56. float GetVolume() const override { return 0.0f; }
  57. bool IsValidScale([[maybe_unused]] Vec3Arg inScale) const override { return true; }
  58. // Register shape functions with the registry
  59. static void sRegister();
  60. private:
  61. Vec3 mCenterOfMass = Vec3::sZero();
  62. };
  63. JPH_NAMESPACE_END