SensorTest.cpp 7.2 KB

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