SensorTest.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/General/SensorTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  9. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  10. #include <Utils/RagdollLoader.h>
  11. #include <Utils/Log.h>
  12. #include <Utils/AssetStream.h>
  13. #include <Layers.h>
  14. #include <Renderer/DebugRendererImp.h>
  15. JPH_IMPLEMENT_RTTI_VIRTUAL(SensorTest)
  16. {
  17. JPH_ADD_BASE_CLASS(SensorTest, Test)
  18. }
  19. SensorTest::~SensorTest()
  20. {
  21. // Destroy the ragdoll
  22. mRagdoll->RemoveFromPhysicsSystem();
  23. mRagdoll = nullptr;
  24. }
  25. void SensorTest::Initialize()
  26. {
  27. // Floor
  28. CreateFloor(400.0f);
  29. {
  30. // A static sensor that attracts dynamic bodies that enter its area
  31. BodyCreationSettings sensor_settings(new SphereShape(10.0f), RVec3(0, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::SENSOR);
  32. sensor_settings.mIsSensor = true;
  33. mSensorID[StaticAttractor] = mBodyInterface->CreateAndAddBody(sensor_settings, EActivation::DontActivate);
  34. SetBodyLabel(mSensorID[StaticAttractor], "Static sensor that attracts dynamic bodies");
  35. }
  36. {
  37. // A static sensor that only detects active bodies
  38. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(5.0f)), RVec3(-10, 5.1f, 0), Quat::sIdentity(), EMotionType::Static, Layers::SENSOR);
  39. sensor_settings.mIsSensor = true;
  40. mSensorID[StaticSensor] = mBodyInterface->CreateAndAddBody(sensor_settings, EActivation::DontActivate);
  41. SetBodyLabel(mSensorID[StaticSensor], "Static sensor that detects active dynamic bodies");
  42. }
  43. {
  44. // A kinematic sensor that also detects sleeping bodies
  45. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(5.0f)), RVec3(10, 5.1f, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::SENSOR);
  46. sensor_settings.mIsSensor = true;
  47. mSensorID[KinematicSensor] = mBodyInterface->CreateAndAddBody(sensor_settings, EActivation::Activate);
  48. SetBodyLabel(mSensorID[KinematicSensor], "Kinematic sensor that also detects sleeping bodies");
  49. }
  50. {
  51. // A kinematic sensor that also detects static bodies
  52. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(5.0f)), RVec3(25, 5.1f, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::MOVING); // Put in a layer that collides with static
  53. sensor_settings.mIsSensor = true;
  54. sensor_settings.mCollideKinematicVsNonDynamic = true;
  55. mSensorID[SensorDetectingStatic] = mBodyInterface->CreateAndAddBody(sensor_settings, EActivation::Activate);
  56. SetBodyLabel(mSensorID[SensorDetectingStatic], "Kinematic sensor that also detects sleeping and static bodies");
  57. }
  58. // Dynamic bodies
  59. for (int i = 0; i < 15; ++i)
  60. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(0.1f, 0.5f, 0.2f)), RVec3(-15.0f + i * 3.0f, 25, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  61. // Static bodies
  62. RVec3 static_positions[] = { RVec3(-14, 1, 4), RVec3(6, 1, 4), RVec3(21, 1, 4) };
  63. for (const RVec3 &p : static_positions)
  64. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(0.5f)), p, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::Activate);
  65. #ifdef JPH_OBJECT_STREAM
  66. // Load ragdoll
  67. Ref<RagdollSettings> ragdoll_settings = RagdollLoader::sLoad("Human.tof", EMotionType::Dynamic);
  68. if (ragdoll_settings == nullptr)
  69. FatalError("Could not load ragdoll");
  70. #else
  71. Ref<RagdollSettings> ragdoll_settings = RagdollLoader::sCreate();
  72. #endif // JPH_OBJECT_STREAM
  73. // Create pose
  74. SkeletonPose ragdoll_pose;
  75. ragdoll_pose.SetSkeleton(ragdoll_settings->GetSkeleton());
  76. {
  77. #ifdef JPH_OBJECT_STREAM
  78. Ref<SkeletalAnimation> animation;
  79. AssetStream stream("Human/dead_pose1.tof", std::ios::in);
  80. if (!ObjectStreamIn::sReadObject(stream.Get(), animation))
  81. FatalError("Could not open animation");
  82. animation->Sample(0.0f, ragdoll_pose);
  83. #else
  84. Ref<Ragdoll> temp_ragdoll = ragdoll_settings->CreateRagdoll(0, 0, mPhysicsSystem);
  85. temp_ragdoll->GetPose(ragdoll_pose);
  86. ragdoll_pose.CalculateJointStates();
  87. #endif // JPH_OBJECT_STREAM
  88. }
  89. ragdoll_pose.SetRootOffset(RVec3(0, 30, 0));
  90. ragdoll_pose.CalculateJointMatrices();
  91. // Create ragdoll
  92. mRagdoll = ragdoll_settings->CreateRagdoll(1, 0, mPhysicsSystem);
  93. mRagdoll->SetPose(ragdoll_pose);
  94. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  95. // Create kinematic body
  96. BodyCreationSettings kinematic_settings(new BoxShape(Vec3(0.25f, 0.5f, 1.0f)), RVec3(-20, 10, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::MOVING);
  97. Body &kinematic = *mBodyInterface->CreateBody(kinematic_settings);
  98. mKinematicBodyID = kinematic.GetID();
  99. mBodyInterface->AddBody(kinematic.GetID(), EActivation::Activate);
  100. }
  101. void SensorTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  102. {
  103. // Update time
  104. mTime += inParams.mDeltaTime;
  105. // Move kinematic body
  106. RVec3 kinematic_pos = RVec3(-20.0f * Cos(mTime), 10, 0);
  107. mBodyInterface->MoveKinematic(mKinematicBodyID, kinematic_pos, Quat::sIdentity(), inParams.mDeltaTime);
  108. // Draw if body is in sensor
  109. Color sensor_color[] = { Color::sRed, Color::sGreen, Color::sBlue, Color::sPurple };
  110. for (int sensor = 0; sensor < NumSensors; ++sensor)
  111. for (const BodyAndCount &body_and_count : mBodiesInSensor[sensor])
  112. {
  113. AABox bounds = mBodyInterface->GetTransformedShape(body_and_count.mBodyID).GetWorldSpaceBounds();
  114. bounds.ExpandBy(Vec3::sReplicate(0.01f * sensor));
  115. mDebugRenderer->DrawWireBox(bounds, sensor_color[sensor]);
  116. }
  117. // Apply forces to dynamic bodies in sensor
  118. lock_guard lock(mMutex);
  119. RVec3 center(0, 10, 0);
  120. float centrifugal_force = 10.0f;
  121. Vec3 gravity = mPhysicsSystem->GetGravity();
  122. for (const BodyAndCount &body_and_count : mBodiesInSensor[StaticAttractor])
  123. {
  124. BodyLockWrite body_lock(mPhysicsSystem->GetBodyLockInterface(), body_and_count.mBodyID);
  125. if (body_lock.Succeeded())
  126. {
  127. Body &body = body_lock.GetBody();
  128. if (body.IsKinematic())
  129. continue;
  130. // Calculate centrifugal acceleration
  131. Vec3 acceleration = Vec3(center - body.GetPosition());
  132. float length = acceleration.Length();
  133. if (length > 0.0f)
  134. acceleration *= centrifugal_force / length;
  135. else
  136. acceleration = Vec3::sZero();
  137. // Draw acceleration
  138. mDebugRenderer->DrawArrow(body.GetPosition(), body.GetPosition() + acceleration, Color::sGreen, 0.1f);
  139. // Cancel gravity
  140. acceleration -= gravity;
  141. // Apply as force
  142. body.AddForce(acceleration / body.GetMotionProperties()->GetInverseMass());
  143. }
  144. }
  145. }
  146. void SensorTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  147. {
  148. for (int sensor = 0; sensor < NumSensors; ++sensor)
  149. {
  150. BodyID sensor_id = mSensorID[sensor];
  151. // Check which body is the sensor
  152. BodyID body_id;
  153. if (inBody1.GetID() == sensor_id)
  154. body_id = inBody2.GetID();
  155. else if (inBody2.GetID() == sensor_id)
  156. body_id = inBody1.GetID();
  157. else
  158. continue;
  159. lock_guard lock(mMutex);
  160. // Add to list and make sure that the list remains sorted for determinism (contacts can be added from multiple threads)
  161. BodyAndCount body_and_count { body_id, 1 };
  162. BodiesInSensor &bodies_in_sensor = mBodiesInSensor[sensor];
  163. BodiesInSensor::iterator b = lower_bound(bodies_in_sensor.begin(), bodies_in_sensor.end(), body_and_count);
  164. if (b != bodies_in_sensor.end() && b->mBodyID == body_id)
  165. {
  166. // This is the right body, increment reference
  167. b->mCount++;
  168. return;
  169. }
  170. bodies_in_sensor.insert(b, body_and_count);
  171. }
  172. }
  173. void SensorTest::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
  174. {
  175. for (int sensor = 0; sensor < NumSensors; ++sensor)
  176. {
  177. BodyID sensor_id = mSensorID[sensor];
  178. // Check which body is the sensor
  179. BodyID body_id;
  180. if (inSubShapePair.GetBody1ID() == sensor_id)
  181. body_id = inSubShapePair.GetBody2ID();
  182. else if (inSubShapePair.GetBody2ID() == sensor_id)
  183. body_id = inSubShapePair.GetBody1ID();
  184. else
  185. continue;
  186. lock_guard lock(mMutex);
  187. // Remove from list
  188. BodyAndCount body_and_count { body_id, 1 };
  189. BodiesInSensor &bodies_in_sensor = mBodiesInSensor[sensor];
  190. BodiesInSensor::iterator b = lower_bound(bodies_in_sensor.begin(), bodies_in_sensor.end(), body_and_count);
  191. if (b != bodies_in_sensor.end() && b->mBodyID == body_id)
  192. {
  193. // This is the right body, increment reference
  194. JPH_ASSERT(b->mCount > 0);
  195. b->mCount--;
  196. // When last reference remove from the list
  197. if (b->mCount == 0)
  198. bodies_in_sensor.erase(b);
  199. return;
  200. }
  201. JPH_ASSERT(false, "Body pair not found");
  202. }
  203. }
  204. void SensorTest::SaveState(StateRecorder &inStream) const
  205. {
  206. inStream.Write(mTime);
  207. for (const BodiesInSensor &b : mBodiesInSensor)
  208. inStream.Write(b);
  209. }
  210. void SensorTest::RestoreState(StateRecorder &inStream)
  211. {
  212. inStream.Read(mTime);
  213. for (BodiesInSensor &b : mBodiesInSensor)
  214. inStream.Read(b);
  215. }