Browse Source

Add AddLinearAndAngularVelocity helper (#81)

Allows adding to both of these in a single lock.
Joshie 3 years ago
parent
commit
150ad69e80
2 changed files with 18 additions and 0 deletions
  1. 17 0
      Jolt/Physics/Body/BodyInterface.cpp
  2. 1 0
      Jolt/Physics/Body/BodyInterface.h

+ 17 - 0
Jolt/Physics/Body/BodyInterface.cpp

@@ -498,6 +498,23 @@ void BodyInterface::AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVe
 	}
 }
 
+void BodyInterface::AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
+{
+	BodyLockWrite lock(*mBodyLockInterface, inBodyID);
+	if (lock.Succeeded())
+	{
+		Body &body = lock.GetBody();
+		if (!body.IsStatic())
+		{
+			body.SetLinearVelocityClamped(body.GetLinearVelocity() + inLinearVelocity);
+			body.SetAngularVelocityClamped(body.GetAngularVelocity() + inAngularVelocity);
+
+			if (!body.IsActive() && (!body.GetLinearVelocity().IsNearZero() || !body.GetAngularVelocity().IsNearZero()))
+				mBodyManager->ActivateBodies(&inBodyID, 1);
+		}
+	}
+}
+
 void BodyInterface::SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity)
 {
 	BodyLockWrite lock(*mBodyLockInterface, inBodyID);

+ 1 - 0
Jolt/Physics/Body/BodyInterface.h

@@ -128,6 +128,7 @@ public:
 	void						SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity);
 	Vec3						GetLinearVelocity(const BodyID &inBodyID) const;
 	void						AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity); ///< Add velocity to current velocity
+	void						AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity); ///< Add linear and angular to current velocities
 	void						SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity);
 	Vec3						GetAngularVelocity(const BodyID &inBodyID) const;
 	Vec3						GetPointVelocity(const BodyID &inBodyID, Vec3Arg inPoint) const; ///< Velocity of point inPoint (in world space, e.g. on the surface of the body) of the body