Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/ActiveEdges.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Geometry/ClosestPoint.h>78JPH_NAMESPACE_BEGIN910/// An active edge is an edge that either has no neighbouring edge or if the angle between the two connecting faces is too large.11namespace ActiveEdges12{13/// Helper function to check if an edge is active or not14/// @param inNormal1 Triangle normal of triangle on the left side of the edge (when looking along the edge from the top)15/// @param inNormal2 Triangle normal of triangle on the right side of the edge16/// @param inEdgeDirection Vector that points along the edge17/// @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)18inline static bool IsEdgeActive(Vec3Arg inNormal1, Vec3Arg inNormal2, Vec3Arg inEdgeDirection, float inCosThresholdAngle)19{20// If normals are opposite the edges are active (the triangles are back to back)21float cos_angle_normals = inNormal1.Dot(inNormal2);22if (cos_angle_normals < -0.999848f) // cos(179 degrees)23return true;2425// Check if concave edge, if so we are not active26if (inNormal1.Cross(inNormal2).Dot(inEdgeDirection) < 0.0f)27return false;2829// Convex edge, active when angle bigger than threshold30return cos_angle_normals < inCosThresholdAngle;31}3233/// Replace normal by triangle normal if a hit is hitting an inactive edge34/// @param inV0 , inV1 , inV2 form the triangle35/// @param inTriangleNormal is the normal of the provided triangle (does not need to be normalized)36/// @param inActiveEdges bit 0 = edge v0..v1 is active, bit 1 = edge v1..v2 is active, bit 2 = edge v2..v0 is active37/// @param inPoint Collision point on the triangle38/// @param inNormal Collision normal on the triangle (does not need to be normalized)39/// @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.40/// @return Returns inNormal if an active edge was hit, otherwise returns inTriangleNormal41inline static Vec3 FixNormal(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inTriangleNormal, uint8 inActiveEdges, Vec3Arg inPoint, Vec3Arg inNormal, Vec3Arg inMovementDirection)42{43// Check: All of the edges are active, we have the correct normal already. No need to call this function!44JPH_ASSERT(inActiveEdges != 0b111);4546// If inNormal would affect movement less than inTriangleNormal use inNormal47// 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)48// 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).49// 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.50float normal_length = inNormal.Length();51float triangle_normal_length = inTriangleNormal.Length();52if (inMovementDirection.Dot(inNormal) * triangle_normal_length < inMovementDirection.Dot(inTriangleNormal) * normal_length)53return inNormal;5455// Check: None of the edges are active, we need to use the triangle normal56if (inActiveEdges == 0)57return inTriangleNormal;5859// Some edges are active.60// If normal is parallel to the triangle normal we don't need to check the active edges.61if (inTriangleNormal.Dot(inNormal) > 0.999848f * normal_length * triangle_normal_length) // cos(1 degree)62return inNormal;6364const float cEpsilon = 1.0e-4f;65const float cOneMinusEpsilon = 1.0f - cEpsilon;6667uint colliding_edge;6869// Test where the contact point is in the triangle70float u, v, w;71ClosestPoint::GetBaryCentricCoordinates(inV0 - inPoint, inV1 - inPoint, inV2 - inPoint, u, v, w);72if (u > cOneMinusEpsilon)73{74// Colliding with v0, edge 0 or 2 needs to be active75colliding_edge = 0b101;76}77else if (v > cOneMinusEpsilon)78{79// Colliding with v1, edge 0 or 1 needs to be active80colliding_edge = 0b011;81}82else if (w > cOneMinusEpsilon)83{84// Colliding with v2, edge 1 or 2 needs to be active85colliding_edge = 0b110;86}87else if (u < cEpsilon)88{89// Colliding with edge v1, v2, edge 1 needs to be active90colliding_edge = 0b010;91}92else if (v < cEpsilon)93{94// Colliding with edge v0, v2, edge 2 needs to be active95colliding_edge = 0b100;96}97else if (w < cEpsilon)98{99// Colliding with edge v0, v1, edge 0 needs to be active100colliding_edge = 0b001;101}102else103{104// Interior hit105return inTriangleNormal;106}107108// If this edge is active, use the provided normal instead of the triangle normal109return (inActiveEdges & colliding_edge) != 0? inNormal : inTriangleNormal;110}111}112113JPH_NAMESPACE_END114115116