ChangeMotionQualityTest.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.mLinearVelocity = Vec3(-240, 0, -120);
  38. settings.mFriction = 0.0f;
  39. settings.mRestitution = 1.0f;
  40. settings.mObjectLayer = Layers::MOVING;
  41. mBody = mBodyInterface->CreateBody(settings);
  42. mBodyInterface->AddBody(mBody->GetID(), EActivation::Activate);
  43. UpdateMotionQuality();
  44. }
  45. void ChangeMotionQualityTest::UpdateMotionQuality()
  46. {
  47. static EMotionQuality qualities[] = { EMotionQuality::LinearCast, EMotionQuality::Discrete };
  48. static const char *labels[] = { "LinearCast", "Discrete" };
  49. // Calculate desired motion quality
  50. int idx = int(mTime) & 1;
  51. mBodyInterface->SetMotionQuality(mBody->GetID(), qualities[idx]);
  52. SetBodyLabel(mBody->GetID(), labels[idx]);
  53. }
  54. void ChangeMotionQualityTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  55. {
  56. // Increment time
  57. mTime += inParams.mDeltaTime;
  58. UpdateMotionQuality();
  59. }
  60. void ChangeMotionQualityTest::SaveState(StateRecorder &inStream) const
  61. {
  62. inStream.Write(mTime);
  63. }
  64. void ChangeMotionQualityTest::RestoreState(StateRecorder &inStream)
  65. {
  66. inStream.Read(mTime);
  67. UpdateMotionQuality();
  68. }