Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/CastConvexVsTriangles.cpp
21461 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Physics/Collision/CastConvexVsTriangles.h>7#include <Jolt/Physics/Collision/TransformedShape.h>8#include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>9#include <Jolt/Physics/Collision/ActiveEdges.h>10#include <Jolt/Physics/Collision/NarrowPhaseStats.h>11#include <Jolt/Geometry/EPAPenetrationDepth.h>1213JPH_NAMESPACE_BEGIN1415CastConvexVsTriangles::CastConvexVsTriangles(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, CastShapeCollector &ioCollector) :16mShapeCast(inShapeCast),17mShapeCastSettings(inShapeCastSettings),18mCenterOfMassTransform2(inCenterOfMassTransform2),19mScale(inScale),20mSubShapeIDCreator1(inSubShapeIDCreator1),21mCollector(ioCollector)22{23JPH_ASSERT(inShapeCast.mShape->GetType() == EShapeType::Convex);2425// Determine if shape is inside out or not26mScaleSign = ScaleHelpers::IsInsideOut(inScale)? -1.0f : 1.0f;27}2829void CastConvexVsTriangles::Cast(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, uint8 inActiveEdges, const SubShapeID &inSubShapeID2)30{31// Scale triangle32Vec3 v0 = mScale * inV0;33Vec3 v1 = mScale * inV1;34Vec3 v2 = mScale * inV2;3536// Calculate triangle normal37Vec3 triangle_normal = mScaleSign * (v1 - v0).Cross(v2 - v0);3839// Backface check40bool back_facing = triangle_normal.Dot(mShapeCast.mDirection) > 0.0f;41if (mShapeCastSettings.mBackFaceModeTriangles == EBackFaceMode::IgnoreBackFaces && back_facing)42return;4344// Create triangle support function45TriangleConvexSupport triangle { v0, v1, v2 };4647// Check if we already created the cast shape support function48if (mSupport == nullptr)49{50// Determine if we want to use the actual shape or a shrunken shape with convex radius51ConvexShape::ESupportMode support_mode = mShapeCastSettings.mUseShrunkenShapeAndConvexRadius? ConvexShape::ESupportMode::ExcludeConvexRadius : ConvexShape::ESupportMode::Default;5253// Create support function54mSupport = static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportFunction(support_mode, mSupportBuffer, mShapeCast.mScale);55}5657EPAPenetrationDepth epa;58float fraction = mCollector.GetEarlyOutFraction();59Vec3 contact_point_a, contact_point_b, contact_normal;60if (epa.CastShape(mShapeCast.mCenterOfMassStart, mShapeCast.mDirection, mShapeCastSettings.mCollisionTolerance, mShapeCastSettings.mPenetrationTolerance, *mSupport, triangle, mSupport->GetConvexRadius(), 0.0f, mShapeCastSettings.mReturnDeepestPoint, fraction, contact_point_a, contact_point_b, contact_normal))61{62// Check if we have enabled active edge detection63if (mShapeCastSettings.mActiveEdgeMode == EActiveEdgeMode::CollideOnlyWithActive && inActiveEdges != 0b111)64{65// Convert the active edge velocity hint to local space66Vec3 active_edge_movement_direction = mCenterOfMassTransform2.Multiply3x3Transposed(mShapeCastSettings.mActiveEdgeMovementDirection);6768// Update the contact normal to account for active edges69// Note that we flip the triangle normal as the penetration axis is pointing towards the triangle instead of away70contact_normal = ActiveEdges::FixNormal(v0, v1, v2, back_facing? triangle_normal : -triangle_normal, inActiveEdges, contact_point_b, contact_normal, active_edge_movement_direction);71}7273// Convert to world space74contact_point_a = mCenterOfMassTransform2 * contact_point_a;75contact_point_b = mCenterOfMassTransform2 * contact_point_b;76Vec3 contact_normal_world = mCenterOfMassTransform2.Multiply3x3(contact_normal);7778// Its a hit, store the sub shape id's79ShapeCastResult result(fraction, contact_point_a, contact_point_b, contact_normal_world, back_facing, mSubShapeIDCreator1.GetID(), inSubShapeID2, TransformedShape::sGetBodyID(mCollector.GetContext()));8081// Early out if this hit is deeper than the collector's early out value82if (fraction == 0.0f && -result.mPenetrationDepth >= mCollector.GetEarlyOutFraction())83return;8485// Gather faces86if (mShapeCastSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)87{88// Get supporting face of shape 189Mat44 transform_1_to_2 = mShapeCast.mCenterOfMassStart;90transform_1_to_2.SetTranslation(transform_1_to_2.GetTranslation() + fraction * mShapeCast.mDirection);91static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportingFace(SubShapeID(), transform_1_to_2.Multiply3x3Transposed(-contact_normal), mShapeCast.mScale, mCenterOfMassTransform2 * transform_1_to_2, result.mShape1Face);9293// Get face of the triangle94result.mShape2Face.resize(3);95result.mShape2Face[0] = mCenterOfMassTransform2 * v0;96result.mShape2Face[1] = mCenterOfMassTransform2 * v1;97result.mShape2Face[2] = mCenterOfMassTransform2 * v2;9899// When inside out, we need to swap the triangle winding100if (mScaleSign < 0.0f)101std::swap(result.mShape2Face[1], result.mShape2Face[2]);102}103104JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)105mCollector.AddHit(result);106}107}108109JPH_NAMESPACE_END110111112