| 1 | /**************************************************************************/ |
| 2 | /* shape_2d.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 "shape_2d.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "servers/physics_2d/physics_server_2d.h" |
| 35 | |
| 36 | RID Shape2D::get_rid() const { |
| 37 | return shape; |
| 38 | } |
| 39 | |
| 40 | void Shape2D::set_custom_solver_bias(real_t p_bias) { |
| 41 | custom_bias = p_bias; |
| 42 | PhysicsServer2D::get_singleton()->shape_set_custom_solver_bias(shape, custom_bias); |
| 43 | } |
| 44 | |
| 45 | real_t Shape2D::get_custom_solver_bias() const { |
| 46 | return custom_bias; |
| 47 | } |
| 48 | |
| 49 | bool Shape2D::collide_with_motion(const Transform2D &p_local_xform, const Vector2 &p_local_motion, RequiredParam<Shape2D> rp_shape, const Transform2D &p_shape_xform, const Vector2 &p_shape_motion) { |
| 50 | EXTRACT_PARAM_OR_FAIL_V(p_shape, rp_shape, false); |
| 51 | int r; |
| 52 | return PhysicsServer2D::get_singleton()->shape_collide(get_rid(), p_local_xform, p_local_motion, p_shape->get_rid(), p_shape_xform, p_shape_motion, nullptr, 0, r); |
| 53 | } |
| 54 | |
| 55 | bool Shape2D::collide(const Transform2D &p_local_xform, RequiredParam<Shape2D> rp_shape, const Transform2D &p_shape_xform) { |
| 56 | EXTRACT_PARAM_OR_FAIL_V(p_shape, rp_shape, false); |
| 57 | int r; |
| 58 | return PhysicsServer2D::get_singleton()->shape_collide(get_rid(), p_local_xform, Vector2(), p_shape->get_rid(), p_shape_xform, Vector2(), nullptr, 0, r); |
| 59 | } |
| 60 | |
| 61 | PackedVector2Array Shape2D::collide_with_motion_and_get_contacts(const Transform2D &p_local_xform, const Vector2 &p_local_motion, RequiredParam<Shape2D> rp_shape, const Transform2D &p_shape_xform, const Vector2 &p_shape_motion) { |
| 62 | EXTRACT_PARAM_OR_FAIL_V(p_shape, rp_shape, PackedVector2Array()); |
| 63 | const int max_contacts = 16; |
| 64 | Vector2 result[max_contacts * 2]; |
| 65 | int contacts = 0; |
| 66 | |
| 67 | if (!PhysicsServer2D::get_singleton()->shape_collide(get_rid(), p_local_xform, p_local_motion, p_shape->get_rid(), p_shape_xform, p_shape_motion, result, max_contacts, contacts)) { |
| 68 | return PackedVector2Array(); |
| 69 | } |
| 70 | |
| 71 | PackedVector2Array results; |
| 72 | results.resize(contacts * 2); |
| 73 | for (int i = 0; i < contacts * 2; i++) { |
| 74 | results.write[i] = result[i]; |
| 75 | } |
| 76 | |
| 77 | return results; |
| 78 | } |
| 79 | |
| 80 | PackedVector2Array Shape2D::collide_and_get_contacts(const Transform2D &p_local_xform, RequiredParam<Shape2D> rp_shape, const Transform2D &p_shape_xform) { |
| 81 | EXTRACT_PARAM_OR_FAIL_V(p_shape, rp_shape, PackedVector2Array()); |
| 82 | const int max_contacts = 16; |
| 83 | Vector2 result[max_contacts * 2]; |
| 84 | int contacts = 0; |
| 85 | |
| 86 | if (!PhysicsServer2D::get_singleton()->shape_collide(get_rid(), p_local_xform, Vector2(), p_shape->get_rid(), p_shape_xform, Vector2(), result, max_contacts, contacts)) { |
| 87 | return PackedVector2Array(); |
| 88 | } |
| 89 | |
| 90 | PackedVector2Array results; |
| 91 | results.resize(contacts * 2); |
| 92 | for (int i = 0; i < contacts * 2; i++) { |
| 93 | results.write[i] = result[i]; |
| 94 | } |
| 95 | |
| 96 | return results; |
| 97 | } |
| 98 | |
| 99 | void Shape2D::_bind_methods() { |
| 100 | ClassDB::bind_method(D_METHOD("set_custom_solver_bias", "bias"), &Shape2D::set_custom_solver_bias); |
| 101 | ClassDB::bind_method(D_METHOD("get_custom_solver_bias"), &Shape2D::get_custom_solver_bias); |
| 102 | ClassDB::bind_method(D_METHOD("collide", "local_xform", "with_shape", "shape_xform"), &Shape2D::collide); |
| 103 | ClassDB::bind_method(D_METHOD("collide_with_motion", "local_xform", "local_motion", "with_shape", "shape_xform", "shape_motion"), &Shape2D::collide_with_motion); |
| 104 | ClassDB::bind_method(D_METHOD("collide_and_get_contacts", "local_xform", "with_shape", "shape_xform"), &Shape2D::collide_and_get_contacts); |
| 105 | ClassDB::bind_method(D_METHOD("collide_with_motion_and_get_contacts", "local_xform", "local_motion", "with_shape", "shape_xform", "shape_motion"), &Shape2D::collide_with_motion_and_get_contacts); |
| 106 | ClassDB::bind_method(D_METHOD("draw", "canvas_item", "color"), &Shape2D::draw); |
| 107 | ClassDB::bind_method(D_METHOD("get_rect"), &Shape2D::get_rect); |
| 108 | |
| 109 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_solver_bias", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_custom_solver_bias", "get_custom_solver_bias"); |
| 110 | } |
| 111 | |
| 112 | bool Shape2D::is_collision_outline_enabled() { |
| 113 | return GLOBAL_GET_CACHED(bool, "debug/shapes/collision/draw_2d_outlines"); |
| 114 | } |
| 115 | |
| 116 | Shape2D::Shape2D(const RID &p_rid) { |
| 117 | shape = p_rid; |
| 118 | } |
| 119 | |
| 120 | Shape2D::~Shape2D() { |
| 121 | ERR_FAIL_NULL(PhysicsServer2D::get_singleton()); |
| 122 | PhysicsServer2D::get_singleton()->free_rid(shape); |
| 123 | } |