EstimateCollisionResponse.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Collision/ContactListener.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// A structure that contains the estimated contact and friction impulses and the resulting body velocities
  8. struct CollisionEstimationResult
  9. {
  10. Vec3 mLinearVelocity1; ///< The estimated linear velocity of body 1 after collision
  11. Vec3 mAngularVelocity1; ///< The estimated angular velocity of body 1 after collision
  12. Vec3 mLinearVelocity2; ///< The estimated linear velocity of body 2 after collision
  13. Vec3 mAngularVelocity2; ///< The estimated angular velocity of body 2 after collision
  14. Vec3 mTangent1; ///< Normalized tangent of contact normal
  15. Vec3 mTangent2; ///< Second normalized tangent of contact normal (forms a basis with mTangent1 and mWorldSpaceNormal)
  16. struct Impulse
  17. {
  18. float mContactImpulse; ///< Estimated contact impulses (kg m / s)
  19. float mFrictionImpulse1; ///< Estimated friction impulses in the direction of tangent 1 (kg m / s)
  20. float mFrictionImpulse2; ///< Estimated friction impulses in the direction of tangent 2 (kg m / s)
  21. };
  22. using Impulses = StaticArray<Impulse, ContactPoints::Capacity>;
  23. Impulses mImpulses;
  24. };
  25. /// This function estimates the contact impulses and body velocity changes as a result of a collision.
  26. /// 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.
  27. /// 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.
  28. ///
  29. /// @param inBody1 Colliding body 1
  30. /// @param inBody2 Colliding body 2
  31. /// @param inManifold The collision manifold
  32. /// @param outResult A structure that contains the estimated contact and friction impulses and the resulting body velocities
  33. /// @param inCombinedFriction The combined friction of body 1 and body 2 (see ContactSettings::mCombinedFriction)
  34. /// @param inCombinedRestitution The combined restitution of body 1 and body 2 (see ContactSettings::mCombinedRestitution)
  35. /// @param inMinVelocityForRestitution Minimal velocity required for restitution to be applied (see PhysicsSettings::mMinVelocityForRestitution)
  36. /// @param inNumIterations Number of iterations to use for the impulse estimation (see PhysicsSettings::mNumVelocitySteps, note you can probably use a lower number for a decent estimate). If you set the number of iterations to 1 then no friction will be calculated.
  37. JPH_EXPORT void EstimateCollisionResponse(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, CollisionEstimationResult &outResult, float inCombinedFriction, float inCombinedRestitution, float inMinVelocityForRestitution = 1.0f, uint inNumIterations = 10);
  38. JPH_NAMESPACE_END