ChangeMotionTypeTest.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/ChangeMotionTypeTest.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  7. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  8. #include <Layers.h>
  9. JPH_IMPLEMENT_RTTI_VIRTUAL(ChangeMotionTypeTest)
  10. {
  11. JPH_ADD_BASE_CLASS(ChangeMotionTypeTest, Test)
  12. }
  13. void ChangeMotionTypeTest::Initialize()
  14. {
  15. // Floor
  16. CreateFloor();
  17. // Create body as static, but allow to become dynamic
  18. BodyCreationSettings settings;
  19. settings.SetShape(new BoxShape(Vec3(0.5f, 1.0f, 2.0f)));
  20. settings.mPosition = Vec3(0, 10, 0);
  21. settings.mMotionType = EMotionType::Static;
  22. settings.mObjectLayer = Layers::MOVING; // Put in moving layer, this will result in some overhead when the body is static
  23. settings.mAllowDynamicOrKinematic = true;
  24. mBody = mBodyInterface->CreateBody(settings);
  25. mBodyInterface->AddBody(mBody->GetID(), EActivation::DontActivate);
  26. }
  27. void ChangeMotionTypeTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  28. {
  29. // Increment time
  30. mTime += inParams.mDeltaTime;
  31. // Calculate desired motion type
  32. static const EMotionType cycle[] = { EMotionType::Dynamic, EMotionType::Kinematic, EMotionType::Static, EMotionType::Kinematic, EMotionType::Dynamic, EMotionType::Static };
  33. EMotionType motion_type = cycle[int(mTime) % size(cycle)];
  34. // Update motion type and reactivate the body
  35. if (motion_type != mBody->GetMotionType())
  36. mBodyInterface->SetMotionType(mBody->GetID(), motion_type, EActivation::Activate);
  37. // Provide kinematic body a target
  38. if (motion_type == EMotionType::Kinematic)
  39. mBody->MoveKinematic(Vec3(sin(mTime), 10, 0), Quat::sRotation(Vec3::sAxisX(), cos(mTime)), inParams.mDeltaTime);
  40. }
  41. void ChangeMotionTypeTest::SaveState(StateRecorder &inStream) const
  42. {
  43. inStream.Write(mTime);
  44. }
  45. void ChangeMotionTypeTest::RestoreState(StateRecorder &inStream)
  46. {
  47. inStream.Read(mTime);
  48. }