Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/ConvexSupport.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Math/Mat44.h>78JPH_NAMESPACE_BEGIN910/// Helper functions to get the support point for a convex object11/// Structure that transforms a convex object (supports only uniform scaling)12template <typename ConvexObject>13struct TransformedConvexObject14{15/// Create transformed convex object.16TransformedConvexObject(Mat44Arg inTransform, const ConvexObject &inObject) :17mTransform(inTransform),18mObject(inObject)19{20}2122/// Calculate the support vector for this convex shape.23Vec3 GetSupport(Vec3Arg inDirection) const24{25return mTransform * mObject.GetSupport(mTransform.Multiply3x3Transposed(inDirection));26}2728/// Get the vertices of the face that faces inDirection the most29template <class VERTEX_ARRAY>30void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const31{32mObject.GetSupportingFace(mTransform.Multiply3x3Transposed(inDirection), outVertices);3334for (Vec3 &v : outVertices)35v = mTransform * v;36}3738Mat44 mTransform;39const ConvexObject & mObject;40};4142/// Structure that adds a convex radius43template <typename ConvexObject>44struct AddConvexRadius45{46AddConvexRadius(const ConvexObject &inObject, float inRadius) :47mObject(inObject),48mRadius(inRadius)49{50}5152/// Calculate the support vector for this convex shape.53Vec3 GetSupport(Vec3Arg inDirection) const54{55float length = inDirection.Length();56return length > 0.0f ? mObject.GetSupport(inDirection) + (mRadius / length) * inDirection : mObject.GetSupport(inDirection);57}5859const ConvexObject & mObject;60float mRadius;61};6263/// Structure that performs a Minkowski difference A - B64template <typename ConvexObjectA, typename ConvexObjectB>65struct MinkowskiDifference66{67MinkowskiDifference(const ConvexObjectA &inObjectA, const ConvexObjectB &inObjectB) :68mObjectA(inObjectA),69mObjectB(inObjectB)70{71}7273/// Calculate the support vector for this convex shape.74Vec3 GetSupport(Vec3Arg inDirection) const75{76return mObjectA.GetSupport(inDirection) - mObjectB.GetSupport(-inDirection);77}7879const ConvexObjectA & mObjectA;80const ConvexObjectB & mObjectB;81};8283/// Class that wraps a point so that it can be used with convex collision detection84struct PointConvexSupport85{86/// Calculate the support vector for this convex shape.87Vec3 GetSupport([[maybe_unused]] Vec3Arg inDirection) const88{89return mPoint;90}9192Vec3 mPoint;93};9495/// Class that wraps a triangle so that it can used with convex collision detection96struct TriangleConvexSupport97{98/// Constructor99TriangleConvexSupport(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3) :100mV1(inV1),101mV2(inV2),102mV3(inV3)103{104}105106/// Calculate the support vector for this convex shape.107Vec3 GetSupport(Vec3Arg inDirection) const108{109// Project vertices on inDirection110float d1 = mV1.Dot(inDirection);111float d2 = mV2.Dot(inDirection);112float d3 = mV3.Dot(inDirection);113114// Return vertex with biggest projection115if (d1 > d2)116{117if (d1 > d3)118return mV1;119else120return mV3;121}122else123{124if (d2 > d3)125return mV2;126else127return mV3;128}129}130131/// Get the vertices of the face that faces inDirection the most132template <class VERTEX_ARRAY>133void GetSupportingFace([[maybe_unused]] Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const134{135outVertices.push_back(mV1);136outVertices.push_back(mV2);137outVertices.push_back(mV3);138}139140/// The three vertices of the triangle141Vec3 mV1;142Vec3 mV2;143Vec3 mV3;144};145146/// Class that wraps a polygon so that it can used with convex collision detection147template <class VERTEX_ARRAY>148struct PolygonConvexSupport149{150/// Constructor151explicit PolygonConvexSupport(const VERTEX_ARRAY &inVertices) :152mVertices(inVertices)153{154}155156/// Calculate the support vector for this convex shape.157Vec3 GetSupport(Vec3Arg inDirection) const158{159Vec3 support_point = mVertices[0];160float best_dot = mVertices[0].Dot(inDirection);161162for (typename VERTEX_ARRAY::const_iterator v = mVertices.begin() + 1; v < mVertices.end(); ++v)163{164float dot = v->Dot(inDirection);165if (dot > best_dot)166{167best_dot = dot;168support_point = *v;169}170}171172return support_point;173}174175/// Get the vertices of the face that faces inDirection the most176template <class VERTEX_ARRAY_ARG>177void GetSupportingFace([[maybe_unused]] Vec3Arg inDirection, VERTEX_ARRAY_ARG &outVertices) const178{179for (Vec3 v : mVertices)180outVertices.push_back(v);181}182183/// The vertices of the polygon184const VERTEX_ARRAY & mVertices;185};186187JPH_NAMESPACE_END188189190