Path: blob/master/modules/jolt_physics/shapes/jolt_custom_ray_shape.cpp
10278 views
/**************************************************************************/1/* jolt_custom_ray_shape.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "jolt_custom_ray_shape.h"3132#include "../spaces/jolt_query_collectors.h"3334#include "Jolt/Physics/Collision/CastResult.h"35#include "Jolt/Physics/Collision/RayCast.h"36#include "Jolt/Physics/Collision/TransformedShape.h"3738#ifdef JPH_DEBUG_RENDERER39#include "Jolt/Renderer/DebugRenderer.h"40#endif4142namespace {4344class JoltCustomRayShapeSupport final : public JPH::ConvexShape::Support {45public:46explicit JoltCustomRayShapeSupport(float p_length) :47length(p_length) {}4849virtual JPH::Vec3 GetSupport(JPH::Vec3Arg p_direction) const override {50if (p_direction.GetZ() > 0.0f) {51return JPH::Vec3(0.0f, 0.0f, length);52} else {53return JPH::Vec3::sZero();54}55}5657virtual float GetConvexRadius() const override { return 0.0f; }5859private:60float length = 0.0f;61};6263static_assert(sizeof(JoltCustomRayShapeSupport) <= sizeof(JPH::ConvexShape::SupportBuffer), "Size of SeparationRayShape3D support is larger than size of support buffer.");6465JPH::Shape *construct_ray() {66return new JoltCustomRayShape();67}6869void collide_ray_vs_shape(const JPH::Shape *p_shape1, const JPH::Shape *p_shape2, JPH::Vec3Arg p_scale1, JPH::Vec3Arg p_scale2, JPH::Mat44Arg p_center_of_mass_transform1, JPH::Mat44Arg p_center_of_mass_transform2, const JPH::SubShapeIDCreator &p_sub_shape_id_creator1, const JPH::SubShapeIDCreator &p_sub_shape_id_creator2, const JPH::CollideShapeSettings &p_collide_shape_settings, JPH::CollideShapeCollector &p_collector, const JPH::ShapeFilter &p_shape_filter) {70ERR_FAIL_COND(p_shape1->GetSubType() != JoltCustomShapeSubType::RAY);7172const JoltCustomRayShape *shape1 = static_cast<const JoltCustomRayShape *>(p_shape1);7374const float margin = p_collide_shape_settings.mMaxSeparationDistance;75const float ray_length = shape1->length;76const float ray_length_padded = ray_length + margin;7778const JPH::Mat44 transform1 = p_center_of_mass_transform1 * JPH::Mat44::sScale(p_scale1);79const JPH::Mat44 transform2 = p_center_of_mass_transform2 * JPH::Mat44::sScale(p_scale2);80const JPH::Mat44 transform_inv2 = transform2.Inversed();8182const JPH::Vec3 ray_start = transform1.GetTranslation();83const JPH::Vec3 ray_direction = transform1.GetAxisZ();84const JPH::Vec3 ray_vector = ray_direction * ray_length;85const JPH::Vec3 ray_vector_padded = ray_direction * ray_length_padded;8687const JPH::Vec3 ray_start2 = transform_inv2 * ray_start;88const JPH::Vec3 ray_direction2 = transform_inv2.Multiply3x3(ray_direction);89const JPH::Vec3 ray_vector_padded2 = transform_inv2.Multiply3x3(ray_vector_padded);9091const JPH::RayCast ray_cast(ray_start2, ray_vector_padded2);9293JPH::RayCastSettings ray_cast_settings;94ray_cast_settings.mTreatConvexAsSolid = false;95ray_cast_settings.mBackFaceModeTriangles = p_collide_shape_settings.mBackFaceMode;9697JoltQueryCollectorClosest<JPH::CastRayCollector> ray_collector;9899p_shape2->CastRay(ray_cast, ray_cast_settings, p_sub_shape_id_creator2, ray_collector);100101if (!ray_collector.had_hit()) {102return;103}104105const JPH::RayCastResult &hit = ray_collector.get_hit();106107const float hit_distance = ray_length_padded * hit.mFraction;108const float hit_depth = ray_length - hit_distance;109110if (-hit_depth >= p_collector.GetEarlyOutFraction()) {111return;112}113114// Since `hit.mSubShapeID2` could represent a path not only from `p_shape2` but also any115// compound shape that it's contained within, we need to split this path into something that116// `p_shape2` can actually understand.117JPH::SubShapeID local_sub_shape_id2;118hit.mSubShapeID2.PopID(p_sub_shape_id_creator2.GetNumBitsWritten(), local_sub_shape_id2);119120const JPH::Vec3 hit_point2 = ray_cast.GetPointOnRay(hit.mFraction);121122const JPH::Vec3 hit_point_on_1 = ray_start + ray_vector;123const JPH::Vec3 hit_point_on_2 = transform2 * hit_point2;124125JPH::Vec3 hit_normal2 = JPH::Vec3::sZero();126127if (shape1->slide_on_slope) {128hit_normal2 = p_shape2->GetSurfaceNormal(local_sub_shape_id2, hit_point2);129130// If we got a back-face normal we need to flip it.131if (hit_normal2.Dot(ray_direction2) > 0) {132hit_normal2 = -hit_normal2;133}134} else {135hit_normal2 = -ray_direction2;136}137138const JPH::Vec3 hit_normal = transform2.Multiply3x3(hit_normal2);139140JPH::CollideShapeResult result(hit_point_on_1, hit_point_on_2, -hit_normal, hit_depth, p_sub_shape_id_creator1.GetID(), hit.mSubShapeID2, JPH::TransformedShape::sGetBodyID(p_collector.GetContext()));141142if (p_collide_shape_settings.mCollectFacesMode == JPH::ECollectFacesMode::CollectFaces) {143p_shape2->GetSupportingFace(local_sub_shape_id2, ray_direction2, p_scale2, p_center_of_mass_transform2, result.mShape2Face);144}145146p_collector.AddHit(result);147}148149void collide_noop(const JPH::Shape *p_shape1, const JPH::Shape *p_shape2, JPH::Vec3Arg p_scale1, JPH::Vec3Arg p_scale2, JPH::Mat44Arg p_center_of_mass_transform1, JPH::Mat44Arg p_center_of_mass_transform2, const JPH::SubShapeIDCreator &p_sub_shape_id_creator1, const JPH::SubShapeIDCreator &p_sub_shape_id_creator2, const JPH::CollideShapeSettings &p_collide_shape_settings, JPH::CollideShapeCollector &p_collector, const JPH::ShapeFilter &p_shape_filter) {150}151152void cast_noop(const JPH::ShapeCast &p_shape_cast, const JPH::ShapeCastSettings &p_shape_cast_settings, const JPH::Shape *p_shape, JPH::Vec3Arg p_scale, const JPH::ShapeFilter &p_shape_filter, JPH::Mat44Arg p_center_of_mass_transform2, const JPH::SubShapeIDCreator &p_sub_shape_id_creator1, const JPH::SubShapeIDCreator &p_sub_shape_id_creator2, JPH::CastShapeCollector &p_collector) {153}154155} // namespace156157JPH::ShapeSettings::ShapeResult JoltCustomRayShapeSettings::Create() const {158if (mCachedResult.IsEmpty()) {159new JoltCustomRayShape(*this, mCachedResult);160}161162return mCachedResult;163}164165void JoltCustomRayShape::register_type() {166JPH::ShapeFunctions &shape_functions = JPH::ShapeFunctions::sGet(JoltCustomShapeSubType::RAY);167168shape_functions.mConstruct = construct_ray;169shape_functions.mColor = JPH::Color::sDarkRed;170171static constexpr JPH::EShapeSubType concrete_sub_types[] = {172JPH::EShapeSubType::Sphere,173JPH::EShapeSubType::Box,174JPH::EShapeSubType::Triangle,175JPH::EShapeSubType::Capsule,176JPH::EShapeSubType::TaperedCapsule,177JPH::EShapeSubType::Cylinder,178JPH::EShapeSubType::ConvexHull,179JPH::EShapeSubType::Mesh,180JPH::EShapeSubType::HeightField,181JPH::EShapeSubType::Plane,182JPH::EShapeSubType::TaperedCylinder183};184185for (const JPH::EShapeSubType concrete_sub_type : concrete_sub_types) {186JPH::CollisionDispatch::sRegisterCollideShape(JoltCustomShapeSubType::RAY, concrete_sub_type, collide_ray_vs_shape);187JPH::CollisionDispatch::sRegisterCollideShape(concrete_sub_type, JoltCustomShapeSubType::RAY, JPH::CollisionDispatch::sReversedCollideShape);188}189190JPH::CollisionDispatch::sRegisterCollideShape(JoltCustomShapeSubType::RAY, JoltCustomShapeSubType::RAY, collide_noop);191192for (const JPH::EShapeSubType sub_type : JPH::sAllSubShapeTypes) {193JPH::CollisionDispatch::sRegisterCastShape(JoltCustomShapeSubType::RAY, sub_type, cast_noop);194JPH::CollisionDispatch::sRegisterCastShape(sub_type, JoltCustomShapeSubType::RAY, cast_noop);195}196}197198JPH::AABox JoltCustomRayShape::GetLocalBounds() const {199const float radius = GetInnerRadius();200return JPH::AABox(JPH::Vec3(-radius, -radius, 0.0f), JPH::Vec3(radius, radius, length));201}202203float JoltCustomRayShape::GetInnerRadius() const {204// There is no sensible value here, since this shape is infinitely thin, so we pick something205// that's hopefully small enough to effectively be zero, but big enough to not cause any206// numerical issues.207return 0.0001f;208}209210JPH::MassProperties JoltCustomRayShape::GetMassProperties() const {211JPH::MassProperties mass_properties;212213// Since this shape has no volume we can't really give it a correct set of mass properties, so214// instead we just give it some arbitrary ones.215mass_properties.mMass = 1.0f;216mass_properties.mInertia = JPH::Mat44::sIdentity();217218return mass_properties;219}220221#ifdef JPH_DEBUG_RENDERER222223void JoltCustomRayShape::Draw(JPH::DebugRenderer *p_renderer, JPH::RMat44Arg p_center_of_mass_transform, JPH::Vec3Arg p_scale, JPH::ColorArg p_color, bool p_use_material_colors, bool p_draw_wireframe) const {224p_renderer->DrawArrow(p_center_of_mass_transform.GetTranslation(), p_center_of_mass_transform * JPH::Vec3(0, 0, length * p_scale.GetZ()), p_use_material_colors ? GetMaterial()->GetDebugColor() : p_color, 0.1f);225}226227#endif228229const JPH::ConvexShape::Support *JoltCustomRayShape::GetSupportFunction(JPH::ConvexShape::ESupportMode p_mode, JPH::ConvexShape::SupportBuffer &p_buffer, JPH::Vec3Arg p_scale) const {230return new (&p_buffer) JoltCustomRayShapeSupport(p_scale.GetZ() * length);231}232233234