ChangeMotionQualityTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/General/ChangeMotionQualityTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  9. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  10. #include <Layers.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(ChangeMotionQualityTest)
  12. {
  13. JPH_ADD_BASE_CLASS(ChangeMotionQualityTest, Test)
  14. }
  15. void ChangeMotionQualityTest::Initialize()
  16. {
  17. // Floor
  18. CreateFloor();
  19. // Single shape that has 4 walls to surround fast moving sphere
  20. BodyCreationSettings enclosing_settings;
  21. Ref<BoxShapeSettings> box_shape = new BoxShapeSettings(Vec3(5.0f, 1.0f, 0.1f));
  22. Ref<StaticCompoundShapeSettings> enclosing_shape = new StaticCompoundShapeSettings();
  23. enclosing_shape->AddShape(Vec3(0, 0, 5), Quat::sIdentity(), box_shape);
  24. enclosing_shape->AddShape(Vec3(0, 0, -5), Quat::sIdentity(), box_shape);
  25. enclosing_shape->AddShape(Vec3(5, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  26. enclosing_shape->AddShape(Vec3(-5, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  27. enclosing_settings.SetShapeSettings(enclosing_shape);
  28. enclosing_settings.mMotionType = EMotionType::Kinematic;
  29. enclosing_settings.mObjectLayer = Layers::MOVING;
  30. enclosing_settings.mPosition = RVec3(0, 1, 0);
  31. Body &enclosing = *mBodyInterface->CreateBody(enclosing_settings);
  32. mBodyInterface->AddBody(enclosing.GetID(), EActivation::Activate);
  33. // Create high speed sphere inside
  34. BodyCreationSettings settings;
  35. settings.SetShape(new SphereShape(1.0f));
  36. settings.mPosition = RVec3(0, 0.5f, 0);
  37. settings.mMotionType = EMotionType::Dynamic;
  38. settings.mMotionQuality = EMotionQuality::LinearCast;
  39. settings.mLinearVelocity = Vec3(-240, 0, -120);
  40. settings.mFriction = 0.0f;
  41. settings.mRestitution = 1.0f;
  42. settings.mObjectLayer = Layers::MOVING;
  43. mBody = mBodyInterface->CreateBody(settings);
  44. mBodyInterface->AddBody(mBody->GetID(), EActivation::Activate);
  45. }
  46. void ChangeMotionQualityTest::UpdateMotionQuality()
  47. {
  48. // Calculate desired motion quality
  49. EMotionQuality motion_quality = (int(mTime) & 1) == 0? EMotionQuality::LinearCast : EMotionQuality::Discrete;
  50. mBodyInterface->SetMotionQuality(mBody->GetID(), motion_quality);
  51. }
  52. void ChangeMotionQualityTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  53. {
  54. // Increment time
  55. mTime += inParams.mDeltaTime;
  56. UpdateMotionQuality();
  57. }
  58. void ChangeMotionQualityTest::SaveState(StateRecorder &inStream) const
  59. {
  60. inStream.Write(mTime);
  61. }
  62. void ChangeMotionQualityTest::RestoreState(StateRecorder &inStream)
  63. {
  64. inStream.Read(mTime);
  65. UpdateMotionQuality();
  66. }