Skip to content
0.2.0-dev Download ZIP
44 lines ·3.0 KB Raw
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
9JPH_NAMESPACE_BEGIN
10
11/// A structure that contains the estimated contact and friction impulses and the resulting body velocities
12struct 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 mFrictionPoint; ///< Point at which friction was applied (relative to mBaseOffset of the manifold)
20 Vec3 mTangent1; ///< Normalized tangent of contact normal
21 Vec3 mTangent2; ///< Second normalized tangent of contact normal (forms a basis with mTangent1 and mWorldSpaceNormal)
22
23 float mFrictionImpulse1 = 0; ///< Estimated friction impulses in the direction of tangent 1 (kg m / s)
24 float mFrictionImpulse2 = 0; ///< Estimated friction impulses in the direction of tangent 2 (kg m / s)
25 float mAngularFrictionImpulse = 0; ///< Estimated angular friction impulse around the world space normal (kg m^2 / s)
26
27 StaticArray<float, ContactPoints::Capacity> mContactImpulse; ///< Estimated contact impulses (kg m / s)
28};
29
30/// This function estimates the contact impulses and body velocity changes as a result of a collision.
31/// 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.
32/// 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.
33///
34/// @param inBody1 Colliding body 1
35/// @param inBody2 Colliding body 2
36/// @param inManifold The collision manifold
37/// @param outResult A structure that contains the estimated contact and friction impulses and the resulting body velocities
38/// @param inCombinedFriction The combined friction of body 1 and body 2 (see ContactSettings::mCombinedFriction)
39/// @param inCombinedRestitution The combined restitution of body 1 and body 2 (see ContactSettings::mCombinedRestitution)
40/// @param inMinVelocityForRestitution Minimal velocity required for restitution to be applied (see PhysicsSettings::mMinVelocityForRestitution)
41/// @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.
42JPH_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);
43
44JPH_NAMESPACE_END