Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
HairUpdateVelocity.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2026 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
6
7void ApplyCollisionAndUpdateVelocity(uint inVtx, JPH_IN_OUT(JPH_HairPosition) ioPos, JPH_IN(JPH_HairPosition) inPreviousPos, JPH_IN(JPH_HairMaterial) inMaterial, float inStrandFraction, JPH_OUT(JPH_HairVelocity) outVel)
8{
9 // Update velocities
10 outVel.mVelocity = (ioPos.mPosition - inPreviousPos.mPosition) / cDeltaTime;
11 outVel.mAngularVelocity = cTwoDivDeltaTime * JPH_QuatMulQuat(ioPos.mRotation, JPH_QuatConjugate(inPreviousPos.mRotation)).xyz;
12
13 if (inMaterial.mEnableCollision)
14 {
15 // Calculate closest point on the collision plane
16 JPH_HairCollisionPlane plane = gCollisionPlanes[inVtx];
17 float distance_to_plane = JPH_PlaneSignedDistance(plane.mPlane, ioPos.mPosition);
18 float3 contact_normal = JPH_PlaneGetNormal(plane.mPlane);
19 float3 point_on_plane = ioPos.mPosition - distance_to_plane * contact_normal;
20
21 // Calculate how much the plane moved in this time step
22 JPH_HairCollisionShape shape = gCollisionShapes[plane.mShapeIndex];
23 float3 plane_velocity = shape.mLinearVelocity + cross(shape.mAngularVelocity, point_on_plane - shape.mCenterOfMass);
24 float plane_movement = dot(plane_velocity, contact_normal) * cAccumulatedDeltaTime;
25
26 float projected_distance = -distance_to_plane + plane_movement + GradientSamplerSample(inMaterial.mHairRadius, inStrandFraction);
27 if (projected_distance > 0.0f)
28 {
29 // Resolve penetration
30 ioPos.mPosition += contact_normal * projected_distance;
31
32 // Only update velocity when moving towards each other
33 float3 v_relative = outVel.mVelocity - plane_velocity;
34 float v_relative_dot_normal = dot(contact_normal, v_relative);
35 if (v_relative_dot_normal < 0.0f)
36 {
37 // Calculate normal and tangential velocity (equation 30)
38 float3 v_normal = contact_normal * v_relative_dot_normal;
39 float3 v_tangential = v_relative - v_normal;
40 float v_tangential_length = length(v_tangential);
41
42 // Apply friction as described in Detailed Rigid Body Simulation with Extended Position Based Dynamics - Matthias Muller et al. (modified equation 31)
43 if (v_tangential_length > 0.0f)
44 outVel.mVelocity -= v_tangential * min(inMaterial.mFriction * projected_distance / (v_tangential_length * cDeltaTime), 1.0f);
45
46 // Apply restitution of zero (equation 35)
47 outVel.mVelocity -= v_normal;
48 }
49 }
50 }
51}
52
53void LimitVelocity(JPH_IN_OUT(JPH_HairVelocity) ioVel, JPH_IN(JPH_HairMaterial) inMaterial)
54{
55 // Limit linear velocity
56 float linear_velocity_sq = dot(ioVel.mVelocity, ioVel.mVelocity);
57 if (linear_velocity_sq > inMaterial.mMaxLinearVelocitySq)
58 ioVel.mVelocity *= sqrt(inMaterial.mMaxLinearVelocitySq / linear_velocity_sq);
59
60 // Limit angular velocity
61 float angular_velocity_sq = dot(ioVel.mAngularVelocity, ioVel.mAngularVelocity);
62 if (angular_velocity_sq > inMaterial.mMaxAngularVelocitySq)
63 ioVel.mAngularVelocity *= sqrt(inMaterial.mMaxAngularVelocitySq / angular_velocity_sq);
64}
unsigned int uint
Definition Core.h:500
float GradientSamplerSample(float4 inSampler, float inStrandFraction)
Definition HairCommon.h:40
void LimitVelocity(JPH_IN_OUT(JPH_HairVelocity) ioVel, JPH_IN(JPH_HairMaterial) inMaterial)
Definition HairUpdateVelocity.h:53
void ApplyCollisionAndUpdateVelocity(uint inVtx, JPH_IN_OUT(JPH_HairPosition) ioPos, JPH_IN(JPH_HairPosition) inPreviousPos, JPH_IN(JPH_HairMaterial) inMaterial, float inStrandFraction, JPH_OUT(JPH_HairVelocity) outVel)
Definition HairUpdateVelocity.h:7
#define JPH_IN_OUT(type)
Definition ShaderCore.h:83
#define JPH_OUT(type)
Definition ShaderCore.h:82
#define JPH_IN(type)
Definition ShaderCore.h:81
float JPH_PlaneSignedDistance(JPH_Plane inPlane, float3 inPoint)
Definition ShaderPlane.h:15
float3 JPH_PlaneGetNormal(JPH_Plane inPlane)
Definition ShaderPlane.h:10
JPH_Quat JPH_QuatMulQuat(JPH_Quat inLHS, JPH_Quat inRHS)
Definition ShaderQuat.h:15
JPH_Quat JPH_QuatConjugate(JPH_Quat inRotation)
Definition ShaderQuat.h:38