| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2023 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <Jolt/Physics/Collision/ContactListener.h> |
| 8 | |
| 9 | JPH_NAMESPACE_BEGIN |
| 10 | |
| 11 | /// A structure that contains the estimated contact and friction impulses and the resulting body velocities |
| 12 | struct CollisionEstimationResult |
| 13 | { |
| 14 | Vec3 mLinearVelocity1; ///< The estimated linear velocity of body 1 after collision |
| 15 | Vec3 mAngularVelocity1; ///< The estimated angular velocity of body 1 after collision |
| 16 | Vec3 mLinearVelocity2; ///< The estimated linear velocity of body 2 after collision |
| 17 | Vec3 mAngularVelocity2; ///< The estimated angular velocity of body 2 after collision |
| 18 | |
| 19 | Vec3 mTangent1; ///< Normalized tangent of contact normal |
| 20 | Vec3 mTangent2; ///< Second normalized tangent of contact normal (forms a basis with mTangent1 and mWorldSpaceNormal) |
| 21 | |
| 22 | struct Impulse |
| 23 | { |
| 24 | float mContactImpulse; ///< Estimated contact impulses (kg m / s) |
| 25 | float mFrictionImpulse1; ///< Estimated friction impulses in the direction of tangent 1 (kg m / s) |
| 26 | float mFrictionImpulse2; ///< Estimated friction impulses in the direction of tangent 2 (kg m / s) |
| 27 | }; |
| 28 | |
| 29 | using Impulses = StaticArray<Impulse, ContactPoints::Capacity>; |
| 30 | |
| 31 | Impulses mImpulses; |
| 32 | }; |
| 33 | |
| 34 | /// This function estimates the contact impulses and body velocity changes as a result of a collision. |
| 35 | /// 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. |
| 36 | /// 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. |
| 37 | /// |
| 38 | /// @param inBody1 Colliding body 1 |
| 39 | /// @param inBody2 Colliding body 2 |
| 40 | /// @param inManifold The collision manifold |
| 41 | /// @param outResult A structure that contains the estimated contact and friction impulses and the resulting body velocities |
| 42 | /// @param inCombinedFriction The combined friction of body 1 and body 2 (see ContactSettings::mCombinedFriction) |
| 43 | /// @param inCombinedRestitution The combined restitution of body 1 and body 2 (see ContactSettings::mCombinedRestitution) |
| 44 | /// @param inMinVelocityForRestitution Minimal velocity required for restitution to be applied (see PhysicsSettings::mMinVelocityForRestitution) |
| 45 | /// @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. |
| 46 | 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); |
| 47 | |
| 48 | JPH_NAMESPACE_END |