ChangeMotionQualityTest.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. mBodyInterface->CreateAndAddBody(enclosing_settings, EActivation::Activate);
  32. // Create high speed sphere inside
  33. BodyCreationSettings settings;
  34. settings.SetShape(new SphereShape(1.0f));
  35. settings.mPosition = RVec3(0, 0.5f, 0);
  36. settings.mMotionType = EMotionType::Dynamic;
  37. settings.mMotionQuality = EMotionQuality::LinearCast;
  38. settings.mLinearVelocity = Vec3(-240, 0, -120);
  39. settings.mFriction = 0.0f;
  40. settings.mRestitution = 1.0f;
  41. settings.mObjectLayer = Layers::MOVING;
  42. mBody = mBodyInterface->CreateBody(settings);
  43. mBodyInterface->AddBody(mBody->GetID(), EActivation::Activate);
  44. }
  45. void ChangeMotionQualityTest::UpdateMotionQuality()
  46. {
  47. // Calculate desired motion quality
  48. EMotionQuality motion_quality = (int(mTime) & 1) == 0? EMotionQuality::LinearCast : EMotionQuality::Discrete;
  49. mBodyInterface->SetMotionQuality(mBody->GetID(), motion_quality);
  50. }
  51. void ChangeMotionQualityTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  52. {
  53. // Increment time
  54. mTime += inParams.mDeltaTime;
  55. UpdateMotionQuality();
  56. }
  57. void ChangeMotionQualityTest::SaveState(StateRecorder &inStream) const
  58. {
  59. inStream.Write(mTime);
  60. }
  61. void ChangeMotionQualityTest::RestoreState(StateRecorder &inStream)
  62. {
  63. inStream.Read(mTime);
  64. UpdateMotionQuality();
  65. }