EstimateCollisionResponse.h 2.1 KB

123456789101112131415161718192021222324252627282930
  1. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/ContactListener.h>
  5. JPH_NAMESPACE_BEGIN
  6. using ContactImpulses = StaticArray<float, ContactPoints::capacity()>;
  7. /// This function estimates the contact impulses and body velocity changes as a result of a collision.
  8. /// It can be used in the ContactListener::OnContactAdded to determine the strength of the collision to e.g. play a sound or trigger a particle system.
  9. /// It only uses the contact points and restitution to estimate the velocity changes and will ignore friction.
  10. /// This function is accurate when two bodies collide but will not be accurate when more than 2 bodies collide at the same time as it does not know about these other collisions.
  11. ///
  12. /// @param inBody1 Colliding body 1
  13. /// @param inBody2 Colliding body 2
  14. /// @param inManifold The collision manifold
  15. /// @param outLinearVelocity1 Outputs the estimated linear velocity of body 1 after collision
  16. /// @param outAngularVelocity1 Outputs the estimated angular velocity of body 1 after collision
  17. /// @param outLinearVelocity2 Outputs the estimated linear velocity of body 2 after collision
  18. /// @param outAngularVelocity2 Outputs the estimated angular velocity of body 2 after collision
  19. /// @param outContactImpulses An array that will contain the estimated contact impulses when the function returns
  20. /// @param inCombinedRestitution The combined restitution of body 1 and body 2 (see ContactSettings::mCombinedRestitution)
  21. /// @param inMinVelocityForRestitution Minimal velocity required for restitution to be applied (see PhysicsSettings::mMinVelocityForRestitution)
  22. /// @param inNumIterations Number of iterations to use for the impulse estimation (default = 4)
  23. void EstimateCollisionResponse(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, Vec3 &outLinearVelocity1, Vec3 &outAngularVelocity1, Vec3 &outLinearVelocity2, Vec3 &outAngularVelocity2, ContactImpulses &outContactImpulses, float inCombinedRestitution, float inMinVelocityForRestitution = 1.0f, uint inNumIterations = 10);
  24. JPH_NAMESPACE_END