ChangeMotionQualityTest.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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::PrePhysicsUpdate(const PreUpdateParams &inParams)
  47. {
  48. // Increment time
  49. mTime += inParams.mDeltaTime;
  50. // Calculate desired motion quality
  51. EMotionQuality motion_quality = (int(mTime) & 1) == 0? EMotionQuality::LinearCast : EMotionQuality::Discrete;
  52. mBodyInterface->SetMotionQuality(mBody->GetID(), motion_quality);
  53. }
  54. void ChangeMotionQualityTest::SaveState(StateRecorder &inStream) const
  55. {
  56. inStream.Write(mTime);
  57. }
  58. void ChangeMotionQualityTest::RestoreState(StateRecorder &inStream)
  59. {
  60. inStream.Read(mTime);
  61. }