SensorTest.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <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. mSensorID = sensor.GetID();
  31. mBodyInterface->AddBody(mSensorID, EActivation::DontActivate);
  32. // Dynamic bodies
  33. for (int i = 0; i < 10; ++i)
  34. 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);
  35. // Load ragdoll
  36. Ref<RagdollSettings> ragdoll_settings = RagdollLoader::sLoad("Assets/Human.tof", EMotionType::Dynamic);
  37. if (ragdoll_settings == nullptr)
  38. FatalError("Could not load ragdoll");
  39. // Load animation
  40. Ref<SkeletalAnimation> animation;
  41. if (!ObjectStreamIn::sReadObject("Assets/Human/Dead_Pose1.tof", animation))
  42. FatalError("Could not open animation");
  43. // Create pose
  44. SkeletonPose ragdoll_pose;
  45. ragdoll_pose.SetSkeleton(ragdoll_settings->GetSkeleton());
  46. animation->Sample(0.0f, ragdoll_pose);
  47. SkeletonPose::JointState &root = ragdoll_pose.GetJoint(0);
  48. root.mTranslation = Vec3(0, 30, 0);
  49. ragdoll_pose.CalculateJointMatrices();
  50. // Create ragdoll
  51. mRagdoll = ragdoll_settings->CreateRagdoll(1, 0, mPhysicsSystem);
  52. mRagdoll->SetPose(ragdoll_pose);
  53. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  54. // Create kinematic body
  55. BodyCreationSettings kinematic_settings(new BoxShape(Vec3(0.25f, 0.5f, 1.0f)), Vec3(-15, 10, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::MOVING);
  56. Body &kinematic = *mBodyInterface->CreateBody(kinematic_settings);
  57. mKinematicBodyID = kinematic.GetID();
  58. mBodyInterface->AddBody(kinematic.GetID(), EActivation::Activate);
  59. }
  60. void SensorTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  61. {
  62. // Update time
  63. mTime += inParams.mDeltaTime;
  64. // Move kinematic body
  65. Vec3 kinematic_pos = Vec3(-15.0f * cos(mTime), 10, 0);
  66. mBodyInterface->MoveKinematic(mKinematicBodyID, kinematic_pos, Quat::sIdentity(), inParams.mDeltaTime);
  67. // Draw if the kinematic body is in the sensor
  68. if (mKinematicBodyInSensor)
  69. mDebugRenderer->DrawWireBox(mBodyInterface->GetTransformedShape(mKinematicBodyID).GetWorldSpaceBounds(), Color::sRed);
  70. // Apply forces to dynamic bodies in sensor
  71. lock_guard lock(mMutex);
  72. Vec3 center(0, 10, 0);
  73. float centrifugal_force = 10.0f;
  74. Vec3 gravity = mPhysicsSystem->GetGravity();
  75. for (const BodyAndCount &body_and_count : mBodiesInSensor)
  76. {
  77. BodyLockWrite body_lock(mPhysicsSystem->GetBodyLockInterface(), body_and_count.mBodyID);
  78. if (body_lock.Succeeded())
  79. {
  80. Body &body = body_lock.GetBody();
  81. // Calculate centrifugal acceleration
  82. Vec3 acceleration = center - body.GetPosition();
  83. float length = acceleration.Length();
  84. if (length > 0.0f)
  85. acceleration *= centrifugal_force / length;
  86. else
  87. acceleration = Vec3::sZero();
  88. // Draw acceleration
  89. mDebugRenderer->DrawArrow(body.GetPosition(), body.GetPosition() + acceleration, Color::sGreen, 0.1f);
  90. // Cancel gravity
  91. acceleration -= gravity;
  92. // Apply as force
  93. body.AddForce(acceleration / body.GetMotionProperties()->GetInverseMass());
  94. }
  95. }
  96. }
  97. void SensorTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  98. {
  99. // Check which body is the sensor
  100. BodyID body_id;
  101. if (inBody1.GetID() == mSensorID)
  102. body_id = inBody2.GetID();
  103. else if (inBody2.GetID() == mSensorID)
  104. body_id = inBody1.GetID();
  105. else
  106. return;
  107. lock_guard lock(mMutex);
  108. if (body_id == mKinematicBodyID)
  109. {
  110. JPH_ASSERT(!mKinematicBodyInSensor);
  111. mKinematicBodyInSensor = true;
  112. }
  113. else
  114. {
  115. // Add to list and make sure that the list remains sorted for determinism (contacts can be added from multiple threads)
  116. BodyAndCount body_and_count { body_id, 1 };
  117. BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
  118. if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
  119. {
  120. // This is the right body, increment reference
  121. b->mCount++;
  122. return;
  123. }
  124. mBodiesInSensor.insert(b, body_and_count);
  125. }
  126. }
  127. void SensorTest::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
  128. {
  129. // Check which body is the sensor
  130. BodyID body_id;
  131. if (inSubShapePair.GetBody1ID() == mSensorID)
  132. body_id = inSubShapePair.GetBody2ID();
  133. else if (inSubShapePair.GetBody2ID() == mSensorID)
  134. body_id = inSubShapePair.GetBody1ID();
  135. else
  136. return;
  137. lock_guard lock(mMutex);
  138. if (body_id == mKinematicBodyID)
  139. {
  140. JPH_ASSERT(mKinematicBodyInSensor);
  141. mKinematicBodyInSensor = false;
  142. }
  143. else
  144. {
  145. // Remove from list
  146. BodyAndCount body_and_count { body_id, 1 };
  147. BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
  148. if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
  149. {
  150. // This is the right body, increment reference
  151. JPH_ASSERT(b->mCount > 0);
  152. b->mCount--;
  153. // When last reference remove from the list
  154. if (b->mCount == 0)
  155. mBodiesInSensor.erase(b);
  156. return;
  157. }
  158. JPH_ASSERT(false, "Body pair not found");
  159. }
  160. }
  161. void SensorTest::SaveState(StateRecorder &inStream) const
  162. {
  163. inStream.Write(mTime);
  164. inStream.Write(mBodiesInSensor);
  165. inStream.Write(mKinematicBodyInSensor);
  166. }
  167. void SensorTest::RestoreState(StateRecorder &inStream)
  168. {
  169. inStream.Read(mTime);
  170. inStream.Read(mBodiesInSensor);
  171. inStream.Read(mKinematicBodyInSensor);
  172. }