Godot Engine – Multi-platform 2D and 3D game engine
/**************************************************************************/
/* physics_direct_space_state_2d.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_2d.h"
#include "core/object/class_db.h"
#include "core/variant/typed_array.h"
Dictionary PhysicsDirectSpaceState2D::_intersect_ray(RequiredParam<PhysicsRayQueryParameters2D> p_ray_query) {
EXTRACT_PARAM_OR_FAIL_V(ray_query, p_ray_query, Dictionary());
PS2DT::RayResult result;
bool res = intersect_ray(ray_query->get_parameters(), result);
if (!res) {
return Dictionary();
}
Dictionary d;
d["position"] = result.position;
d["normal"] = result.normal;
d["collider_id"] = result.collider_id;
d["collider"] = result.get_collider();
d["shape"] = result.shape;
d["rid"] = result.rid;
return d;
}
TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_point(RequiredParam<PhysicsPointQueryParameters2D> p_point_query, int p_max_results) {
EXTRACT_PARAM_OR_FAIL_V(point_query, p_point_query, TypedArray<Dictionary>());
Vector<PS2DT::ShapeResult> ret;
ret.resize(p_max_results);
int rc = intersect_point(point_query->get_parameters(), ret.ptrw(), ret.size());
if (rc == 0) {
return TypedArray<Dictionary>();
}
TypedArray<Dictionary> 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].get_collider();
d["shape"] = ret[i].shape;
r[i] = d;
}
return r;
}
TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_shape(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query, int p_max_results) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, TypedArray<Dictionary>());
Vector<PS2DT::ShapeResult> sr;
sr.resize(p_max_results);
int rc = intersect_shape(shape_query->get_parameters(), sr.ptrw(), sr.size());
TypedArray<Dictionary> 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].get_collider();
d["shape"] = sr[i].shape;
ret[i] = d;
}
return ret;
}
Vector<real_t> PhysicsDirectSpaceState2D::_cast_motion(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, Vector<real_t>());
real_t closest_safe, closest_unsafe;
bool res = cast_motion(shape_query->get_parameters(), closest_safe, closest_unsafe);
if (!res) {
return Vector<real_t>();
}
Vector<real_t> ret;
ret.resize(2);
ret.write[0] = closest_safe;
ret.write[1] = closest_unsafe;
return ret;
}
TypedArray<Vector2> PhysicsDirectSpaceState2D::_collide_shape(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query, int p_max_results) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, TypedArray<Vector2>());
Vector<Vector2> ret;
ret.resize(p_max_results * 2);
int rc = 0;
bool res = collide_shape(shape_query->get_parameters(), ret.ptrw(), p_max_results, rc);
if (!res) {
return TypedArray<Vector2>();
}
TypedArray<Vector2> r;
r.resize(rc * 2);
for (int i = 0; i < rc * 2; i++) {
r[i] = ret[i];
}
return r;
}
Dictionary PhysicsDirectSpaceState2D::_get_rest_info(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, Dictionary());
PS2DT::ShapeRestInfo sri;
bool res = rest_info(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;
}
bool PhysicsDirectSpaceState2D::_intersect_ray_into(RequiredParam<PhysicsRayQueryParameters2D> p_ray_query, RequiredParam<PhysicsIntersectRayResult2D> p_result) {
EXTRACT_PARAM_OR_FAIL_V(ray_query, p_ray_query, false);
EXTRACT_PARAM_OR_FAIL_V(result, p_result, false);
return intersect_ray(ray_query->get_parameters(), result->result);
}
bool PhysicsDirectSpaceState2D::_intersect_point_into(RequiredParam<PhysicsPointQueryParameters2D> p_point_query, RequiredParam<PhysicsIntersectPointResult2D> p_result) {
EXTRACT_PARAM_OR_FAIL_V(point_query, p_point_query, false);
EXTRACT_PARAM_OR_FAIL_V(result, p_result, false);
result->intersection_count = intersect_point(point_query->get_parameters(), result->result.ptr(), result->get_max_intersections());
return result->intersection_count > 0;
}
bool PhysicsDirectSpaceState2D::_intersect_shape_into(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query, RequiredParam<PhysicsIntersectShapeResult2D> p_result) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, false);
EXTRACT_PARAM_OR_FAIL_V(result, p_result, false);
result->intersection_count = intersect_shape(shape_query->get_parameters(), result->result.ptr(), result->get_max_intersections());
return result->intersection_count > 0;
}
bool PhysicsDirectSpaceState2D::_cast_motion_into(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query, RequiredParam<PhysicsCastMotionResult2D> p_result) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, false);
EXTRACT_PARAM_OR_FAIL_V(result, p_result, false);
result->safe_fraction = 1.0;
result->unsafe_fraction = 1.0;
return cast_motion(shape_query->get_parameters(), result->safe_fraction, result->unsafe_fraction) && result->safe_fraction < 1.0;
}
bool PhysicsDirectSpaceState2D::_collide_shape_into(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query, RequiredParam<PhysicsCollideShapeResult2D> p_result) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, false);
EXTRACT_PARAM_OR_FAIL_V(result, p_result, false);
return collide_shape(shape_query->get_parameters(), result->result.ptr(), result->get_max_collisions(), result->collision_count);
}
bool PhysicsDirectSpaceState2D::_get_rest_info_into(RequiredParam<PhysicsShapeQueryParameters2D> p_shape_query, RequiredParam<PhysicsGetRestInfoResult2D> p_result) {
EXTRACT_PARAM_OR_FAIL_V(shape_query, p_shape_query, false);
EXTRACT_PARAM_OR_FAIL_V(result, p_result, false);
return rest_info(shape_query->get_parameters(), &result->result);
}
PhysicsDirectSpaceState2D::PhysicsDirectSpaceState2D() {
}
void PhysicsDirectSpaceState2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("intersect_ray", "parameters"), &PhysicsDirectSpaceState2D::_intersect_ray);
ClassDB::bind_method(D_METHOD("intersect_point", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_intersect_point, DEFVAL(32));
ClassDB::bind_method(D_METHOD("intersect_shape", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_intersect_shape, DEFVAL(32));
ClassDB::bind_method(D_METHOD("cast_motion", "parameters"), &PhysicsDirectSpaceState2D::_cast_motion);
ClassDB::bind_method(D_METHOD("collide_shape", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_collide_shape, DEFVAL(32));
ClassDB::bind_method(D_METHOD("get_rest_info", "parameters"), &PhysicsDirectSpaceState2D::_get_rest_info);
ClassDB::bind_method(D_METHOD("intersect_ray_into", "parameters", "result"), &PhysicsDirectSpaceState2D::_intersect_ray_into);
ClassDB::bind_method(D_METHOD("intersect_point_into", "parameters", "result"), &PhysicsDirectSpaceState2D::_intersect_point_into);
ClassDB::bind_method(D_METHOD("intersect_shape_into", "parameters", "result"), &PhysicsDirectSpaceState2D::_intersect_shape_into);
ClassDB::bind_method(D_METHOD("cast_motion_into", "parameters", "result"), &PhysicsDirectSpaceState2D::_cast_motion_into);
ClassDB::bind_method(D_METHOD("collide_shape_into", "parameters", "result"), &PhysicsDirectSpaceState2D::_collide_shape_into);
ClassDB::bind_method(D_METHOD("get_rest_info_into", "parameters", "result"), &PhysicsDirectSpaceState2D::_get_rest_info_into);
}