SensorTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/SensorTest.h>
  5. #include <Physics/Collision/Shape/BoxShape.h>
  6. #include <Physics/Collision/Shape/SphereShape.h>
  7. #include <Physics/Body/BodyCreationSettings.h>
  8. #include <Utils/RagdollLoader.h>
  9. #include <Utils/Log.h>
  10. #include <Layers.h>
  11. #include <Renderer/DebugRendererImp.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(SensorTest)
  13. {
  14. JPH_ADD_BASE_CLASS(SensorTest, Test)
  15. }
  16. SensorTest::~SensorTest()
  17. {
  18. // Destroy the ragdoll
  19. mRagdoll->RemoveFromPhysicsSystem();
  20. mRagdoll = nullptr;
  21. }
  22. void SensorTest::Initialize()
  23. {
  24. // Floor
  25. CreateFloor();
  26. // Sensor
  27. BodyCreationSettings sensor_settings(new SphereShape(10.0f), Vec3(0, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  28. sensor_settings.mIsSensor = true;
  29. Body &sensor = *mBodyInterface->CreateBody(sensor_settings);
  30. JPH_IF_DEBUG(sensor.SetDebugName("Sensor");)
  31. mSensorID = sensor.GetID();
  32. mBodyInterface->AddBody(mSensorID, EActivation::DontActivate);
  33. // Dynamic bodies
  34. for (int i = 0; i < 10; ++i)
  35. {
  36. Body &body = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(0.1f, 0.5f, 0.2f)), Vec3(-15.0f + i * 3.0f, 25, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  37. JPH_IF_DEBUG(body.SetDebugName("Body " + ConvertToString(i)));
  38. mBodyInterface->AddBody(body.GetID(), EActivation::Activate);
  39. }
  40. // Load ragdoll
  41. Ref<RagdollSettings> ragdoll_settings = RagdollLoader::sLoad("Assets/Human.tof", EMotionType::Dynamic);
  42. if (ragdoll_settings == nullptr)
  43. FatalError("Could not load ragdoll");
  44. // Load animation
  45. Ref<SkeletalAnimation> animation;
  46. if (!ObjectStreamIn::sReadObject("Assets/Human/Dead_Pose1.tof", animation))
  47. FatalError("Could not open animation");
  48. // Create pose
  49. SkeletonPose ragdoll_pose;
  50. ragdoll_pose.SetSkeleton(ragdoll_settings->GetSkeleton());
  51. animation->Sample(0.0f, ragdoll_pose);
  52. SkeletonPose::JointState &root = ragdoll_pose.GetJoint(0);
  53. root.mTranslation = Vec3(0, 30, 0);
  54. ragdoll_pose.CalculateJointMatrices();
  55. // Create ragdoll
  56. mRagdoll = ragdoll_settings->CreateRagdoll(1, nullptr, mPhysicsSystem);
  57. mRagdoll->SetPose(ragdoll_pose);
  58. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  59. }
  60. void SensorTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  61. {
  62. lock_guard lock(mMutex);
  63. Vec3 center(0, 10, 0);
  64. float centrifugal_force = 10.0f;
  65. Vec3 gravity = mPhysicsSystem->GetGravity();
  66. for (const BodyAndCount &body_and_count : mBodiesInSensor)
  67. {
  68. BodyLockWrite body_lock(mPhysicsSystem->GetBodyLockInterface(), body_and_count.mBodyID);
  69. if (body_lock.Succeeded())
  70. {
  71. Body &body = body_lock.GetBody();
  72. // Calculate centrifugal acceleration
  73. Vec3 acceleration = center - body.GetPosition();
  74. float length = acceleration.Length();
  75. if (length > 0.0f)
  76. acceleration *= centrifugal_force / length;
  77. else
  78. acceleration = Vec3::sZero();
  79. // Draw acceleration
  80. mDebugRenderer->DrawArrow(body.GetPosition(), body.GetPosition() + acceleration, Color::sGreen, 0.1f);
  81. // Cancel gravity
  82. acceleration -= gravity;
  83. // Apply as force
  84. body.AddForce(acceleration / body.GetMotionProperties()->GetInverseMass());
  85. }
  86. }
  87. }
  88. void SensorTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  89. {
  90. // Check which body is the sensor
  91. BodyID body_id;
  92. if (inBody1.GetID() == mSensorID)
  93. body_id = inBody2.GetID();
  94. else if (inBody2.GetID() == mSensorID)
  95. body_id = inBody1.GetID();
  96. else
  97. return;
  98. // Add to list and make sure that the list remains sorted for determinism (contacts can be added from multiple threads)
  99. lock_guard lock(mMutex);
  100. BodyAndCount body_and_count { body_id, 1 };
  101. BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
  102. if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
  103. {
  104. // This is the right body, increment reference
  105. b->mCount++;
  106. return;
  107. }
  108. mBodiesInSensor.insert(b, body_and_count);
  109. }
  110. void SensorTest::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
  111. {
  112. // Check which body is the sensor
  113. BodyID body_id;
  114. if (inSubShapePair.GetBody1ID() == mSensorID)
  115. body_id = inSubShapePair.GetBody2ID();
  116. else if (inSubShapePair.GetBody2ID() == mSensorID)
  117. body_id = inSubShapePair.GetBody1ID();
  118. else
  119. return;
  120. // Remove from list
  121. lock_guard lock(mMutex);
  122. BodyAndCount body_and_count { body_id, 1 };
  123. BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
  124. if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
  125. {
  126. // This is the right body, increment reference
  127. JPH_ASSERT(b->mCount > 0);
  128. b->mCount--;
  129. // When last reference remove from the list
  130. if (b->mCount == 0)
  131. mBodiesInSensor.erase(b);
  132. return;
  133. }
  134. JPH_ASSERT(false, "Body pair not found");
  135. }
  136. void SensorTest::SaveState(StateRecorder &inStream) const
  137. {
  138. inStream.Write(mBodiesInSensor);
  139. }
  140. void SensorTest::RestoreState(StateRecorder &inStream)
  141. {
  142. inStream.Read(mBodiesInSensor);
  143. }