DecoratedShape.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/Shape/DecoratedShape.h>
  6. #include <Jolt/ObjectStream/TypeDeclarations.h>
  7. JPH_NAMESPACE_BEGIN
  8. JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(DecoratedShapeSettings)
  9. {
  10. JPH_ADD_BASE_CLASS(DecoratedShapeSettings, ShapeSettings)
  11. JPH_ADD_ATTRIBUTE(DecoratedShapeSettings, mInnerShape)
  12. }
  13. DecoratedShape::DecoratedShape(EShapeSubType inSubType, const DecoratedShapeSettings &inSettings, ShapeResult &outResult) :
  14. Shape(EShapeType::Decorated, inSubType, inSettings, outResult)
  15. {
  16. // Check that there's a shape
  17. if (inSettings.mInnerShape == nullptr && inSettings.mInnerShapePtr == nullptr)
  18. {
  19. outResult.SetError("Inner shape is null!");
  20. return;
  21. }
  22. if (inSettings.mInnerShapePtr != nullptr)
  23. {
  24. // Use provided shape
  25. mInnerShape = inSettings.mInnerShapePtr;
  26. }
  27. else
  28. {
  29. // Create child shape
  30. ShapeResult child_result = inSettings.mInnerShape->Create();
  31. if (!child_result.IsValid())
  32. {
  33. outResult = child_result;
  34. return;
  35. }
  36. mInnerShape = child_result.Get();
  37. }
  38. }
  39. const PhysicsMaterial *DecoratedShape::GetMaterial(const SubShapeID &inSubShapeID) const
  40. {
  41. return mInnerShape->GetMaterial(inSubShapeID);
  42. }
  43. void DecoratedShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
  44. {
  45. mInnerShape->GetSupportingFace(inSubShapeID, inDirection, inScale, inCenterOfMassTransform, outVertices);
  46. }
  47. uint64 DecoratedShape::GetSubShapeUserData(const SubShapeID &inSubShapeID) const
  48. {
  49. return mInnerShape->GetSubShapeUserData(inSubShapeID);
  50. }
  51. void DecoratedShape::SaveSubShapeState(ShapeList &outSubShapes) const
  52. {
  53. outSubShapes.clear();
  54. outSubShapes.push_back(mInnerShape);
  55. }
  56. void DecoratedShape::RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes)
  57. {
  58. JPH_ASSERT(inNumShapes == 1);
  59. mInnerShape = inSubShapes[0];
  60. }
  61. Shape::Stats DecoratedShape::GetStatsRecursive(VisitedShapes &ioVisitedShapes) const
  62. {
  63. // Get own stats
  64. Stats stats = Shape::GetStatsRecursive(ioVisitedShapes);
  65. // Add child stats
  66. Stats child_stats = mInnerShape->GetStatsRecursive(ioVisitedShapes);
  67. stats.mSizeBytes += child_stats.mSizeBytes;
  68. stats.mNumTriangles += child_stats.mNumTriangles;
  69. return stats;
  70. }
  71. JPH_NAMESPACE_END