Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/ActiveEdges.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Geometry/ClosestPoint.h>
8
9
JPH_NAMESPACE_BEGIN
10
11
/// An active edge is an edge that either has no neighbouring edge or if the angle between the two connecting faces is too large.
12
namespace ActiveEdges
13
{
14
/// Helper function to check if an edge is active or not
15
/// @param inNormal1 Triangle normal of triangle on the left side of the edge (when looking along the edge from the top)
16
/// @param inNormal2 Triangle normal of triangle on the right side of the edge
17
/// @param inEdgeDirection Vector that points along the edge
18
/// @param inCosThresholdAngle Cosine of the threshold angle (if the angle between the two triangles is bigger than this, the edge is active, note that a concave edge is always inactive)
19
inline static bool IsEdgeActive(Vec3Arg inNormal1, Vec3Arg inNormal2, Vec3Arg inEdgeDirection, float inCosThresholdAngle)
20
{
21
// If normals are opposite the edges are active (the triangles are back to back)
22
float cos_angle_normals = inNormal1.Dot(inNormal2);
23
if (cos_angle_normals < -0.999848f) // cos(179 degrees)
24
return true;
25
26
// Check if concave edge, if so we are not active
27
if (inNormal1.Cross(inNormal2).Dot(inEdgeDirection) < 0.0f)
28
return false;
29
30
// Convex edge, active when angle bigger than threshold
31
return cos_angle_normals < inCosThresholdAngle;
32
}
33
34
/// Replace normal by triangle normal if a hit is hitting an inactive edge
35
/// @param inV0 , inV1 , inV2 form the triangle
36
/// @param inTriangleNormal is the normal of the provided triangle (does not need to be normalized)
37
/// @param inActiveEdges bit 0 = edge v0..v1 is active, bit 1 = edge v1..v2 is active, bit 2 = edge v2..v0 is active
38
/// @param inPoint Collision point on the triangle
39
/// @param inNormal Collision normal on the triangle (does not need to be normalized)
40
/// @param inMovementDirection Can be zero. This gives an indication of in which direction the motion is to determine if when we hit an inactive edge/triangle we should return the triangle normal.
41
/// @return Returns inNormal if an active edge was hit, otherwise returns inTriangleNormal
42
inline static Vec3 FixNormal(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inTriangleNormal, uint8 inActiveEdges, Vec3Arg inPoint, Vec3Arg inNormal, Vec3Arg inMovementDirection)
43
{
44
// Check: All of the edges are active, we have the correct normal already. No need to call this function!
45
JPH_ASSERT(inActiveEdges != 0b111);
46
47
// If inNormal would affect movement less than inTriangleNormal use inNormal
48
// This is done since it is really hard to make a distinction between sliding over a horizontal triangulated grid and hitting an edge (in this case you want to use the triangle normal)
49
// and sliding over a triangulated grid and grazing a vertical triangle with an inactive edge (in this case using the triangle normal will cause the object to bounce back so we want to use the calculated normal).
50
// To solve this we take a movement hint to give an indication of what direction our object is moving. If the edge normal results in less motion difference than the triangle normal we use the edge normal.
51
float normal_length = inNormal.Length();
52
float triangle_normal_length = inTriangleNormal.Length();
53
if (inMovementDirection.Dot(inNormal) * triangle_normal_length < inMovementDirection.Dot(inTriangleNormal) * normal_length)
54
return inNormal;
55
56
// Check: None of the edges are active, we need to use the triangle normal
57
if (inActiveEdges == 0)
58
return inTriangleNormal;
59
60
// Some edges are active.
61
// If normal is parallel to the triangle normal we don't need to check the active edges.
62
if (inTriangleNormal.Dot(inNormal) > 0.999848f * normal_length * triangle_normal_length) // cos(1 degree)
63
return inNormal;
64
65
const float cEpsilon = 1.0e-4f;
66
const float cOneMinusEpsilon = 1.0f - cEpsilon;
67
68
uint colliding_edge;
69
70
// Test where the contact point is in the triangle
71
float u, v, w;
72
ClosestPoint::GetBaryCentricCoordinates(inV0 - inPoint, inV1 - inPoint, inV2 - inPoint, u, v, w);
73
if (u > cOneMinusEpsilon)
74
{
75
// Colliding with v0, edge 0 or 2 needs to be active
76
colliding_edge = 0b101;
77
}
78
else if (v > cOneMinusEpsilon)
79
{
80
// Colliding with v1, edge 0 or 1 needs to be active
81
colliding_edge = 0b011;
82
}
83
else if (w > cOneMinusEpsilon)
84
{
85
// Colliding with v2, edge 1 or 2 needs to be active
86
colliding_edge = 0b110;
87
}
88
else if (u < cEpsilon)
89
{
90
// Colliding with edge v1, v2, edge 1 needs to be active
91
colliding_edge = 0b010;
92
}
93
else if (v < cEpsilon)
94
{
95
// Colliding with edge v0, v2, edge 2 needs to be active
96
colliding_edge = 0b100;
97
}
98
else if (w < cEpsilon)
99
{
100
// Colliding with edge v0, v1, edge 0 needs to be active
101
colliding_edge = 0b001;
102
}
103
else
104
{
105
// Interior hit
106
return inTriangleNormal;
107
}
108
109
// If this edge is active, use the provided normal instead of the triangle normal
110
return (inActiveEdges & colliding_edge) != 0? inNormal : inTriangleNormal;
111
}
112
}
113
114
JPH_NAMESPACE_END
115
116