Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/RayCast.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/Physics/Collision/BackFaceMode.h>78JPH_NAMESPACE_BEGIN910/// Structure that holds a single ray cast11template <class Vec, class Mat, class RayCastType>12struct RayCastT13{14JPH_OVERRIDE_NEW_DELETE1516/// Constructors17RayCastT() = default; // Allow raycast to be created uninitialized18RayCastT(typename Vec::ArgType inOrigin, Vec3Arg inDirection) : mOrigin(inOrigin), mDirection(inDirection) { }19RayCastT(const RayCastT<Vec, Mat, RayCastType> &) = default;2021/// Transform this ray using inTransform22RayCastType Transformed(typename Mat::ArgType inTransform) const23{24Vec ray_origin = inTransform * mOrigin;25Vec3 ray_direction(inTransform * (mOrigin + mDirection) - ray_origin);26return { ray_origin, ray_direction };27}2829/// Translate ray using inTranslation30RayCastType Translated(typename Vec::ArgType inTranslation) const31{32return { inTranslation + mOrigin, mDirection };33}3435/// Get point with fraction inFraction on ray (0 = start of ray, 1 = end of ray)36inline Vec GetPointOnRay(float inFraction) const37{38return mOrigin + inFraction * mDirection;39}4041Vec mOrigin; ///< Origin of the ray42Vec3 mDirection; ///< Direction and length of the ray (anything beyond this length will not be reported as a hit)43};4445struct RayCast : public RayCastT<Vec3, Mat44, RayCast>46{47using RayCastT<Vec3, Mat44, RayCast>::RayCastT;48};4950struct RRayCast : public RayCastT<RVec3, RMat44, RRayCast>51{52using RayCastT<RVec3, RMat44, RRayCast>::RayCastT;5354/// Convert from RayCast, converts single to double precision55explicit RRayCast(const RayCast &inRay) :56RRayCast(RVec3(inRay.mOrigin), inRay.mDirection)57{58}5960/// Convert to RayCast, which implies casting from double precision to single precision61explicit operator RayCast() const62{63return RayCast(Vec3(mOrigin), mDirection);64}65};6667/// Settings to be passed with a ray cast68class RayCastSettings69{70public:71JPH_OVERRIDE_NEW_DELETE7273/// Set the backfacing mode for all shapes74void SetBackFaceMode(EBackFaceMode inMode) { mBackFaceModeTriangles = mBackFaceModeConvex = inMode; }7576/// How backfacing triangles should be treated (should we report back facing hits for triangle based shapes, e.g. MeshShape/HeightFieldShape?)77EBackFaceMode mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;7879/// How backfacing convex objects should be treated (should we report back facing hits for convex shapes?)80EBackFaceMode mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;8182/// If convex shapes should be treated as solid. When true, a ray starting inside a convex shape will generate a hit at fraction 0.83bool mTreatConvexAsSolid = true;84};8586JPH_NAMESPACE_END878889