Quellcode durchsuchen

Added post step callback for VehicleConstraint (#597)

This callback can be used to modify the velocity of the vehicle just before the solver simulates the vehicle and can be used to e.g. control a vehicle while it is in the air.
Jorrit Rouwe vor 2 Jahren
Ursprung
Commit
bfe6b4ba3c

+ 4 - 0
Jolt/Physics/Vehicle/VehicleConstraint.cpp

@@ -243,6 +243,10 @@ void VehicleConstraint::OnStep(float inDeltaTime, PhysicsSystem &inPhysicsSystem
 	// Callback on our controller
 	mController->PostCollide(inDeltaTime, inPhysicsSystem);
 
+	// Callback to higher-level systems. We do it before the sleep section, in case velocities change.
+	if (mPostStepCallback != nullptr)
+		mPostStepCallback(*this, inDeltaTime, inPhysicsSystem);
+
 	// If the wheels are rotating, we don't want to go to sleep yet
 	bool allow_sleep = mController->AllowSleep();
 	if (allow_sleep)

+ 10 - 0
Jolt/Physics/Vehicle/VehicleConstraint.h

@@ -86,6 +86,15 @@ public:
 	void						SetCombineFriction(CombineFunction inCombineFriction) { mCombineFriction = inCombineFriction; }
 	CombineFunction				GetCombineFriction() const					{ return mCombineFriction; }
 
+	/// Callback function to notify that PhysicsStepListener::OnStep has completed for this vehicle.
+	using PostStepCallback = function<void(VehicleConstraint &inVehicle, float inDeltaTime, PhysicsSystem &inPhysicsSystem)>;
+
+	/// Callback function to notify that PhysicsStepListener::OnStep has completed for this vehicle. Default is to do nothing.
+	/// Can be used to adjust the velocity of the vehicle to allow higher-level code to e.g. control the vehicle in the air.
+	/// You should not change the position of the vehicle in this callback as the wheel collision checks have already been performed.
+	const PostStepCallback &	GetPostStepCallback() const					{ return mPostStepCallback; }
+	void						SetPostStepCallback(const PostStepCallback &inPostStepCallback) { mPostStepCallback = inPostStepCallback; }
+
 	/// Get the local space forward vector of the vehicle
 	Vec3						GetLocalForward() const						{ return mForward; }
 
@@ -179,6 +188,7 @@ private:
 	// Interfaces
 	RefConst<VehicleCollisionTester> mVehicleCollisionTester;				///< Class that performs testing of collision for the wheels
 	CombineFunction				mCombineFriction = [](float inTireFriction, const Body &inBody2, const SubShapeID &) { return sqrt(inTireFriction * inBody2.GetFriction()); };
+	PostStepCallback			mPostStepCallback;
 };
 
 JPH_NAMESPACE_END