ChangeMotionQualityTest.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <TestFramework.h>
  2. #include <Tests/General/ChangeMotionQualityTest.h>
  3. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  4. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  5. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Layers.h>
  8. JPH_IMPLEMENT_RTTI_VIRTUAL(ChangeMotionQualityTest)
  9. {
  10. JPH_ADD_BASE_CLASS(ChangeMotionQualityTest, Test)
  11. }
  12. void ChangeMotionQualityTest::Initialize()
  13. {
  14. // Floor
  15. CreateFloor();
  16. // Single shape that has 4 walls to surround fast moving sphere
  17. BodyCreationSettings enclosing_settings;
  18. Ref<BoxShapeSettings> box_shape = new BoxShapeSettings(Vec3(5.0f, 1.0f, 0.1f));
  19. Ref<StaticCompoundShapeSettings> enclosing_shape = new StaticCompoundShapeSettings();
  20. enclosing_shape->AddShape(Vec3(0, 0, 5), Quat::sIdentity(), box_shape);
  21. enclosing_shape->AddShape(Vec3(0, 0, -5), Quat::sIdentity(), box_shape);
  22. enclosing_shape->AddShape(Vec3(5, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  23. enclosing_shape->AddShape(Vec3(-5, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  24. enclosing_settings.SetShapeSettings(enclosing_shape);
  25. enclosing_settings.mMotionType = EMotionType::Kinematic;
  26. enclosing_settings.mObjectLayer = Layers::MOVING;
  27. enclosing_settings.mPosition = RVec3(0, 1, 0);
  28. Body &enclosing = *mBodyInterface->CreateBody(enclosing_settings);
  29. mBodyInterface->AddBody(enclosing.GetID(), EActivation::Activate);
  30. // Create high speed sphere inside
  31. BodyCreationSettings settings;
  32. settings.SetShape(new SphereShape(1.0f));
  33. settings.mPosition = RVec3(0, 0.5f, 0);
  34. settings.mMotionType = EMotionType::Dynamic;
  35. settings.mMotionQuality = EMotionQuality::LinearCast;
  36. settings.mLinearVelocity = Vec3(-240, 0, -120);
  37. settings.mFriction = 0.0f;
  38. settings.mRestitution = 1.0f;
  39. settings.mObjectLayer = Layers::MOVING;
  40. mBody = mBodyInterface->CreateBody(settings);
  41. mBodyInterface->AddBody(mBody->GetID(), EActivation::Activate);
  42. }
  43. void ChangeMotionQualityTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  44. {
  45. // Increment time
  46. mTime += inParams.mDeltaTime;
  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::SaveState(StateRecorder &inStream) const
  52. {
  53. inStream.Write(mTime);
  54. }
  55. void ChangeMotionQualityTest::RestoreState(StateRecorder &inStream)
  56. {
  57. inStream.Read(mTime);
  58. }