SensorTest.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // Create kinematic body
  60. BodyCreationSettings kinematic_settings(new BoxShape(Vec3(0.25f, 0.5f, 1.0f)), Vec3(-15, 10, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::MOVING);
  61. Body &kinematic = *mBodyInterface->CreateBody(kinematic_settings);
  62. mKinematicBodyID = kinematic.GetID();
  63. mBodyInterface->AddBody(kinematic.GetID(), EActivation::Activate);
  64. }
  65. void SensorTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  66. {
  67. // Update time
  68. mTime += inParams.mDeltaTime;
  69. // Move kinematic body
  70. Vec3 kinematic_pos = Vec3(-15.0f * cos(mTime), 10, 0);
  71. mBodyInterface->MoveKinematic(mKinematicBodyID, kinematic_pos, Quat::sIdentity(), inParams.mDeltaTime);
  72. // Draw if the kinematic body is in the sensor
  73. if (mKinematicBodyInSensor)
  74. mDebugRenderer->DrawWireBox(mBodyInterface->GetTransformedShape(mKinematicBodyID).GetWorldSpaceBounds(), Color::sRed);
  75. // Apply forces to dynamic bodies in sensor
  76. lock_guard lock(mMutex);
  77. Vec3 center(0, 10, 0);
  78. float centrifugal_force = 10.0f;
  79. Vec3 gravity = mPhysicsSystem->GetGravity();
  80. for (const BodyAndCount &body_and_count : mBodiesInSensor)
  81. {
  82. BodyLockWrite body_lock(mPhysicsSystem->GetBodyLockInterface(), body_and_count.mBodyID);
  83. if (body_lock.Succeeded())
  84. {
  85. Body &body = body_lock.GetBody();
  86. // Calculate centrifugal acceleration
  87. Vec3 acceleration = center - body.GetPosition();
  88. float length = acceleration.Length();
  89. if (length > 0.0f)
  90. acceleration *= centrifugal_force / length;
  91. else
  92. acceleration = Vec3::sZero();
  93. // Draw acceleration
  94. mDebugRenderer->DrawArrow(body.GetPosition(), body.GetPosition() + acceleration, Color::sGreen, 0.1f);
  95. // Cancel gravity
  96. acceleration -= gravity;
  97. // Apply as force
  98. body.AddForce(acceleration / body.GetMotionProperties()->GetInverseMass());
  99. }
  100. }
  101. }
  102. void SensorTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  103. {
  104. // Check which body is the sensor
  105. BodyID body_id;
  106. if (inBody1.GetID() == mSensorID)
  107. body_id = inBody2.GetID();
  108. else if (inBody2.GetID() == mSensorID)
  109. body_id = inBody1.GetID();
  110. else
  111. return;
  112. lock_guard lock(mMutex);
  113. if (body_id == mKinematicBodyID)
  114. {
  115. JPH_ASSERT(!mKinematicBodyInSensor);
  116. mKinematicBodyInSensor = true;
  117. }
  118. else
  119. {
  120. // Add to list and make sure that the list remains sorted for determinism (contacts can be added from multiple threads)
  121. BodyAndCount body_and_count { body_id, 1 };
  122. BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
  123. if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
  124. {
  125. // This is the right body, increment reference
  126. b->mCount++;
  127. return;
  128. }
  129. mBodiesInSensor.insert(b, body_and_count);
  130. }
  131. }
  132. void SensorTest::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
  133. {
  134. // Check which body is the sensor
  135. BodyID body_id;
  136. if (inSubShapePair.GetBody1ID() == mSensorID)
  137. body_id = inSubShapePair.GetBody2ID();
  138. else if (inSubShapePair.GetBody2ID() == mSensorID)
  139. body_id = inSubShapePair.GetBody1ID();
  140. else
  141. return;
  142. lock_guard lock(mMutex);
  143. if (body_id == mKinematicBodyID)
  144. {
  145. JPH_ASSERT(mKinematicBodyInSensor);
  146. mKinematicBodyInSensor = false;
  147. }
  148. else
  149. {
  150. // Remove from list
  151. BodyAndCount body_and_count { body_id, 1 };
  152. BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
  153. if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
  154. {
  155. // This is the right body, increment reference
  156. JPH_ASSERT(b->mCount > 0);
  157. b->mCount--;
  158. // When last reference remove from the list
  159. if (b->mCount == 0)
  160. mBodiesInSensor.erase(b);
  161. return;
  162. }
  163. JPH_ASSERT(false, "Body pair not found");
  164. }
  165. }
  166. void SensorTest::SaveState(StateRecorder &inStream) const
  167. {
  168. inStream.Write(mTime);
  169. inStream.Write(mBodiesInSensor);
  170. inStream.Write(mKinematicBodyInSensor);
  171. }
  172. void SensorTest::RestoreState(StateRecorder &inStream)
  173. {
  174. inStream.Read(mTime);
  175. inStream.Read(mBodiesInSensor);
  176. inStream.Read(mKinematicBodyInSensor);
  177. }