Explorar o código

Implemented Ragdoll::IsActive function (#527)

Fixes #521
Jorrit Rouwe %!s(int64=2) %!d(string=hai) anos
pai
achega
111577604a
Modificáronse 2 ficheiros con 23 adicións e 0 borrados
  1. 17 0
      Jolt/Physics/Ragdoll/Ragdoll.cpp
  2. 6 0
      Jolt/Physics/Ragdoll/Ragdoll.h

+ 17 - 0
Jolt/Physics/Ragdoll/Ragdoll.cpp

@@ -455,6 +455,23 @@ void Ragdoll::Activate(bool inLockBodies)
 	sGetBodyInterface(mSystem, inLockBodies).ActivateBodies(mBodyIDs.data(), (int)mBodyIDs.size());
 }
 
+bool Ragdoll::IsActive(bool inLockBodies) const
+{
+	// Lock the bodies
+	int body_count = (int)mBodyIDs.size();
+	BodyLockMultiRead lock(sGetBodyLockInterface(mSystem, inLockBodies), mBodyIDs.data(), body_count);
+
+	// Test if any body is active
+	for (int b = 0; b < body_count; ++b)
+	{
+		const Body *body = lock.GetBody(b);
+		if (body->IsActive())
+			return true;
+	}
+
+	return false;
+}
+
 void Ragdoll::SetGroupID(CollisionGroup::GroupID inGroupID, bool inLockBodies)
 {
 	// Lock the bodies

+ 6 - 0
Jolt/Physics/Ragdoll/Ragdoll.h

@@ -127,6 +127,12 @@ public:
 	/// Wake up all bodies in the ragdoll
 	void								Activate(bool inLockBodies = true);
 
+	/// Check if one or more of the bodies in the ragdoll are active.
+	/// Note that this involves locking the bodies (if inLockBodies is true) and looping over them. An alternative and possibly faster
+	/// way could be to install a BodyActivationListener and count the number of active bodies of a ragdoll as they're activated / deactivated
+	/// (basically check if the body that activates / deactivates is in GetBodyIDs() and increment / decrement a counter).
+	bool								IsActive(bool inLockBodies = true) const;
+
 	/// Set the group ID on all bodies in the ragdoll
 	void								SetGroupID(CollisionGroup::GroupID inGroupID, bool inLockBodies = true);