0.2.0-dev Download ZIP
125 lines ·8.8 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* jolt_custom_instance_overrides_shape.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "jolt_custom_instance_overrides_shape.h" |
| 32 | |
| 33 | #include "core/error/error_macros.h" |
| 34 | |
| 35 | #include <Jolt/Physics/Collision/CollisionDispatch.h> |
| 36 | #include <Jolt/Physics/Collision/RayCast.h> |
| 37 | #include <Jolt/Physics/Collision/ShapeCast.h> |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | JPH::Shape *construct_instance_overrides_shape() { |
| 42 | return new JoltCustomInstanceOverridesShape(); |
| 43 | } |
| 44 | |
| 45 | void collide_instance_overrides_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) { |
| 46 | ERR_FAIL_COND(p_shape1->GetSubType() != JoltCustomShapeSubType::INSTANCE_OVERRIDES); |
| 47 | |
| 48 | const JoltCustomInstanceOverridesShape *shape1 = static_cast<const JoltCustomInstanceOverridesShape *>(p_shape1); |
| 49 | |
| 50 | JPH::CollideShapeSettings new_collide_shape_settings = p_collide_shape_settings; |
| 51 | if (shape1->object_has_back_face_collision() || shape1->shape_has_back_face_collision()) { |
| 52 | new_collide_shape_settings.mBackFaceMode = JPH::EBackFaceMode::CollideWithBackFaces; |
| 53 | } |
| 54 | |
| 55 | JPH::CollisionDispatch::sCollideShapeVsShape(shape1->GetInnerShape(), p_shape2, p_scale1, p_scale2, p_center_of_mass_transform1, p_center_of_mass_transform2, p_sub_shape_id_creator1, p_sub_shape_id_creator2, new_collide_shape_settings, p_collector, p_shape_filter); |
| 56 | } |
| 57 | |
| 58 | void collide_shape_vs_instance_overrides(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) { |
| 59 | ERR_FAIL_COND(p_shape2->GetSubType() != JoltCustomShapeSubType::INSTANCE_OVERRIDES); |
| 60 | |
| 61 | const JoltCustomInstanceOverridesShape *shape2 = static_cast<const JoltCustomInstanceOverridesShape *>(p_shape2); |
| 62 | |
| 63 | JPH::CollideShapeSettings new_collide_shape_settings = p_collide_shape_settings; |
| 64 | if (shape2->object_has_back_face_collision() || shape2->shape_has_back_face_collision()) { |
| 65 | new_collide_shape_settings.mBackFaceMode = JPH::EBackFaceMode::CollideWithBackFaces; |
| 66 | } |
| 67 | |
| 68 | JPH::CollisionDispatch::sCollideShapeVsShape(p_shape1, shape2->GetInnerShape(), p_scale1, p_scale2, p_center_of_mass_transform1, p_center_of_mass_transform2, p_sub_shape_id_creator1, p_sub_shape_id_creator2, new_collide_shape_settings, p_collector, p_shape_filter); |
| 69 | } |
| 70 | |
| 71 | void cast_instance_overrides_vs_shape(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) { |
| 72 | ERR_FAIL_COND(p_shape_cast.mShape->GetSubType() != JoltCustomShapeSubType::INSTANCE_OVERRIDES); |
| 73 | |
| 74 | const JoltCustomInstanceOverridesShape *shape = static_cast<const JoltCustomInstanceOverridesShape *>(p_shape_cast.mShape); |
| 75 | const JPH::ShapeCast shape_cast(shape->GetInnerShape(), p_shape_cast.mScale, p_shape_cast.mCenterOfMassStart, p_shape_cast.mDirection); |
| 76 | |
| 77 | JPH::CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, p_shape_cast_settings, p_shape, p_scale, p_shape_filter, p_center_of_mass_transform2, p_sub_shape_id_creator1, p_sub_shape_id_creator2, p_collector); |
| 78 | } |
| 79 | |
| 80 | void cast_shape_vs_instance_overrides(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) { |
| 81 | ERR_FAIL_COND(p_shape->GetSubType() != JoltCustomShapeSubType::INSTANCE_OVERRIDES); |
| 82 | |
| 83 | const JoltCustomInstanceOverridesShape *shape = static_cast<const JoltCustomInstanceOverridesShape *>(p_shape); |
| 84 | |
| 85 | JPH::ShapeCastSettings new_shape_cast_settings = p_shape_cast_settings; |
| 86 | if (shape->object_has_back_face_collision() || shape->shape_has_back_face_collision()) { |
| 87 | new_shape_cast_settings.mBackFaceModeTriangles = JPH::EBackFaceMode::CollideWithBackFaces; |
| 88 | } |
| 89 | |
| 90 | JPH::CollisionDispatch::sCastShapeVsShapeLocalSpace(p_shape_cast, new_shape_cast_settings, shape->GetInnerShape(), p_scale, p_shape_filter, p_center_of_mass_transform2, p_sub_shape_id_creator1, p_sub_shape_id_creator2, p_collector); |
| 91 | } |
| 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | JPH::ShapeSettings::ShapeResult JoltCustomInstanceOverridesShapeSettings::Create() const { |
| 96 | if (mCachedResult.IsEmpty()) { |
| 97 | new JoltCustomInstanceOverridesShape(*this, mCachedResult); |
| 98 | } |
| 99 | |
| 100 | return mCachedResult; |
| 101 | } |
| 102 | |
| 103 | void JoltCustomInstanceOverridesShape::register_type() { |
| 104 | JPH::ShapeFunctions &shape_functions = JPH::ShapeFunctions::sGet(JoltCustomShapeSubType::INSTANCE_OVERRIDES); |
| 105 | |
| 106 | shape_functions.mConstruct = construct_instance_overrides_shape; |
| 107 | shape_functions.mColor = JPH::Color::sCyan; |
| 108 | |
| 109 | for (const JPH::EShapeSubType sub_type : JPH::sAllSubShapeTypes) { |
| 110 | JPH::CollisionDispatch::sRegisterCollideShape(JoltCustomShapeSubType::INSTANCE_OVERRIDES, sub_type, collide_instance_overrides_vs_shape); |
| 111 | JPH::CollisionDispatch::sRegisterCollideShape(sub_type, JoltCustomShapeSubType::INSTANCE_OVERRIDES, collide_shape_vs_instance_overrides); |
| 112 | JPH::CollisionDispatch::sRegisterCastShape(JoltCustomShapeSubType::INSTANCE_OVERRIDES, sub_type, cast_instance_overrides_vs_shape); |
| 113 | JPH::CollisionDispatch::sRegisterCastShape(sub_type, JoltCustomShapeSubType::INSTANCE_OVERRIDES, cast_shape_vs_instance_overrides); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void JoltCustomInstanceOverridesShape::CastRay(const JPH::RayCast &p_ray, const JPH::RayCastSettings &p_ray_cast_settings, const JPH::SubShapeIDCreator &p_sub_shape_id_creator, JPH::CastRayCollector &p_collector, const JPH::ShapeFilter &p_shape_filter) const { |
| 118 | JPH::RayCastSettings new_ray_cast_settings = p_ray_cast_settings; |
| 119 | if (!shape_has_back_face_collision()) { |
| 120 | // While ray-casts in Godot can be configured to hit back-faces, it's only allowed to do so if the `backface_collision` property of the shape is enabled as well. |
| 121 | new_ray_cast_settings.mBackFaceModeTriangles = JPH::EBackFaceMode::IgnoreBackFaces; |
| 122 | } |
| 123 | |
| 124 | mInnerShape->CastRay(p_ray, new_ray_cast_settings, p_sub_shape_id_creator, p_collector, p_shape_filter); |
| 125 | } |