/**************************************************************************/ /* physics_direct_space_state_3d.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "physics_direct_space_state_3d.h" #include "core/object/class_db.h" #include "core/variant/typed_array.h" Dictionary PhysicsDirectSpaceState3D::_intersect_ray(RequiredParam rp_ray_query) { EXTRACT_PARAM_OR_FAIL_V(p_ray_query, rp_ray_query, Dictionary()); PS3DT::RayResult result; bool res = intersect_ray(p_ray_query->get_parameters(), result); if (!res) { return Dictionary(); } Dictionary d; d["position"] = result.position; d["normal"] = result.normal; d["face_index"] = result.face_index; d["collider_id"] = result.collider_id; d["collider"] = result.collider; d["shape"] = result.shape; d["rid"] = result.rid; return d; } TypedArray PhysicsDirectSpaceState3D::_intersect_point(RequiredParam rp_point_query, int p_max_results) { EXTRACT_PARAM_OR_FAIL_V(p_point_query, rp_point_query, TypedArray()); Vector ret; ret.resize(p_max_results); int rc = intersect_point(p_point_query->get_parameters(), ret.ptrw(), ret.size()); if (rc == 0) { return TypedArray(); } TypedArray r; r.resize(rc); for (int i = 0; i < rc; i++) { Dictionary d; d["rid"] = ret[i].rid; d["collider_id"] = ret[i].collider_id; d["collider"] = ret[i].collider; d["shape"] = ret[i].shape; r[i] = d; } return r; } TypedArray PhysicsDirectSpaceState3D::_intersect_shape(RequiredParam rp_shape_query, int p_max_results) { EXTRACT_PARAM_OR_FAIL_V(p_shape_query, rp_shape_query, TypedArray()); Vector sr; sr.resize(p_max_results); int rc = intersect_shape(p_shape_query->get_parameters(), sr.ptrw(), sr.size()); TypedArray ret; ret.resize(rc); for (int i = 0; i < rc; i++) { Dictionary d; d["rid"] = sr[i].rid; d["collider_id"] = sr[i].collider_id; d["collider"] = sr[i].collider; d["shape"] = sr[i].shape; ret[i] = d; } return ret; } Vector PhysicsDirectSpaceState3D::_cast_motion(RequiredParam rp_shape_query) { EXTRACT_PARAM_OR_FAIL_V(p_shape_query, rp_shape_query, Vector()); real_t closest_safe = 1.0f, closest_unsafe = 1.0f; bool res = cast_motion(p_shape_query->get_parameters(), closest_safe, closest_unsafe); if (!res) { return Vector(); } Vector ret; ret.resize(2); ret.write[0] = closest_safe; ret.write[1] = closest_unsafe; return ret; } TypedArray PhysicsDirectSpaceState3D::_collide_shape(RequiredParam rp_shape_query, int p_max_results) { EXTRACT_PARAM_OR_FAIL_V(p_shape_query, rp_shape_query, TypedArray()); Vector ret; ret.resize(p_max_results * 2); int rc = 0; bool res = collide_shape(p_shape_query->get_parameters(), ret.ptrw(), p_max_results, rc); if (!res) { return TypedArray(); } TypedArray r; r.resize(rc * 2); for (int i = 0; i < rc * 2; i++) { r[i] = ret[i]; } return r; } Dictionary PhysicsDirectSpaceState3D::_get_rest_info(RequiredParam rp_shape_query) { EXTRACT_PARAM_OR_FAIL_V(p_shape_query, rp_shape_query, Dictionary()); PS3DT::ShapeRestInfo sri; bool res = rest_info(p_shape_query->get_parameters(), &sri); Dictionary r; if (!res) { return r; } r["point"] = sri.point; r["normal"] = sri.normal; r["rid"] = sri.rid; r["collider_id"] = sri.collider_id; r["shape"] = sri.shape; r["linear_velocity"] = sri.linear_velocity; return r; } PhysicsDirectSpaceState3D::PhysicsDirectSpaceState3D() { } void PhysicsDirectSpaceState3D::_bind_methods() { ClassDB::bind_method(D_METHOD("intersect_point", "parameters", "max_results"), &PhysicsDirectSpaceState3D::_intersect_point, DEFVAL(32)); ClassDB::bind_method(D_METHOD("intersect_ray", "parameters"), &PhysicsDirectSpaceState3D::_intersect_ray); ClassDB::bind_method(D_METHOD("intersect_shape", "parameters", "max_results"), &PhysicsDirectSpaceState3D::_intersect_shape, DEFVAL(32)); ClassDB::bind_method(D_METHOD("cast_motion", "parameters"), &PhysicsDirectSpaceState3D::_cast_motion); ClassDB::bind_method(D_METHOD("collide_shape", "parameters", "max_results"), &PhysicsDirectSpaceState3D::_collide_shape, DEFVAL(32)); ClassDB::bind_method(D_METHOD("get_rest_info", "parameters"), &PhysicsDirectSpaceState3D::_get_rest_info); }