2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-10-18 19:24:30 +00:00
|
|
|
/* godot_area_2d.cpp */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#include "godot_area_2d.h"
|
|
|
|
#include "godot_body_2d.h"
|
|
|
|
#include "godot_space_2d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotArea2D::BodyKey::BodyKey(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
2017-03-05 15:44:50 +00:00
|
|
|
rid = p_body->get_self();
|
|
|
|
instance_id = p_body->get_instance_id();
|
|
|
|
body_shape = p_body_shape;
|
|
|
|
area_shape = p_area_shape;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotArea2D::BodyKey::BodyKey(GodotArea2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
2017-03-05 15:44:50 +00:00
|
|
|
rid = p_body->get_self();
|
|
|
|
instance_id = p_body->get_instance_id();
|
|
|
|
body_shape = p_body_shape;
|
|
|
|
area_shape = p_area_shape;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::_shapes_changed() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2019-11-14 07:48:41 +00:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::set_transform(const Transform2D &p_transform) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_set_transform(p_transform);
|
2014-09-15 14:33:30 +00:00
|
|
|
_set_inv_transform(p_transform.affine_inverse());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::set_space(GodotSpace2D *p_space) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (get_space()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (monitor_query_list.in_list()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (moved_list.in_list()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
get_space()->area_remove_from_moved_list(&moved_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
monitored_bodies.clear();
|
2015-03-17 03:45:25 +00:00
|
|
|
monitored_areas.clear();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_set_space(p_space);
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
void GodotArea2D::set_monitor_callback(const Callable &p_callback) {
|
|
|
|
ObjectID id = p_callback.get_object_id();
|
|
|
|
|
|
|
|
if (id == monitor_callback.get_object_id()) {
|
|
|
|
monitor_callback = p_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_unregister_shapes();
|
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
monitor_callback = p_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
monitored_bodies.clear();
|
2015-03-17 03:45:25 +00:00
|
|
|
monitored_areas.clear();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_shape_changed();
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2015-09-04 02:24:55 +00:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
void GodotArea2D::set_area_monitor_callback(const Callable &p_callback) {
|
|
|
|
ObjectID id = p_callback.get_object_id();
|
|
|
|
|
|
|
|
if (id == area_monitor_callback.get_object_id()) {
|
|
|
|
area_monitor_callback = p_callback;
|
2015-03-17 03:45:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_unregister_shapes();
|
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
area_monitor_callback = p_callback;
|
2015-03-17 03:45:25 +00:00
|
|
|
|
|
|
|
monitored_bodies.clear();
|
|
|
|
monitored_areas.clear();
|
|
|
|
|
|
|
|
_shape_changed();
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2015-09-04 02:24:55 +00:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-03-17 03:45:25 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-11-05 01:12:00 +00:00
|
|
|
void GodotArea2D::_set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode) {
|
|
|
|
bool do_override = p_new_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
|
|
|
|
if (do_override == (r_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
_unregister_shapes();
|
2021-11-05 01:12:00 +00:00
|
|
|
r_mode = p_new_mode;
|
2014-02-10 01:10:30 +00:00
|
|
|
_shape_changed();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_param) {
|
2021-11-05 01:12:00 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
|
|
|
|
_set_space_override_mode(gravity_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
|
|
|
|
break;
|
2020-05-10 11:00:47 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY:
|
|
|
|
gravity = p_value;
|
|
|
|
break;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
|
|
|
|
gravity_vector = p_value;
|
|
|
|
break;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
|
|
|
|
gravity_is_point = p_value;
|
|
|
|
break;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
|
|
|
|
gravity_distance_scale = p_value;
|
|
|
|
break;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
|
|
|
|
point_attenuation = p_value;
|
|
|
|
break;
|
2021-11-05 01:12:00 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
|
|
|
|
_set_space_override_mode(linear_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
|
|
|
|
break;
|
2020-05-10 11:00:47 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
|
|
|
|
linear_damp = p_value;
|
|
|
|
break;
|
2021-11-05 01:12:00 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
|
|
|
|
_set_space_override_mode(angular_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
|
|
|
|
break;
|
2020-05-10 11:00:47 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
|
|
|
|
angular_damp = p_value;
|
|
|
|
break;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_PRIORITY:
|
|
|
|
priority = p_value;
|
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Variant GodotArea2D::get_param(PhysicsServer2D::AreaParameter p_param) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_param) {
|
2021-11-05 01:12:00 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
|
|
|
|
return gravity_override_mode;
|
2020-05-10 11:00:47 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY:
|
|
|
|
return gravity;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
|
|
|
|
return gravity_vector;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
|
|
|
|
return gravity_is_point;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
|
|
|
|
return gravity_distance_scale;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
|
|
|
|
return point_attenuation;
|
2021-11-05 01:12:00 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
|
|
|
|
return linear_damping_override_mode;
|
2020-05-10 11:00:47 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
|
|
|
|
return linear_damp;
|
2021-11-05 01:12:00 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
|
|
|
|
return angular_damping_override_mode;
|
2020-05-10 11:00:47 +00:00
|
|
|
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
|
|
|
|
return angular_damp;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_PRIORITY:
|
|
|
|
return priority;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::_queue_monitor_update() {
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!get_space());
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!monitor_query_list.in_list()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
get_space()->area_add_to_monitor_query_list(&monitor_query_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::set_monitorable(bool p_monitorable) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (monitorable == p_monitorable) {
|
2015-03-17 03:45:25 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-03-17 03:45:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
monitorable = p_monitorable;
|
2015-03-17 03:45:25 +00:00
|
|
|
_set_static(!monitorable);
|
2021-11-20 06:57:34 +00:00
|
|
|
_shapes_changed();
|
2015-03-17 03:45:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::call_queries() {
|
2021-10-31 21:45:58 +00:00
|
|
|
if (!monitor_callback.is_null() && !monitored_bodies.is_empty()) {
|
|
|
|
if (monitor_callback.is_valid()) {
|
|
|
|
Variant res[5];
|
|
|
|
Variant *resptr[5];
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
resptr[i] = &res[i];
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_bodies.begin(); E;) {
|
|
|
|
if (E->value.state == 0) { // Nothing happened
|
|
|
|
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
|
|
|
|
++next;
|
|
|
|
monitored_bodies.remove(E);
|
2021-10-31 21:45:58 +00:00
|
|
|
E = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
|
|
|
|
res[1] = E->key.rid;
|
|
|
|
res[2] = E->key.instance_id;
|
|
|
|
res[3] = E->key.body_shape;
|
|
|
|
res[4] = E->key.area_shape;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
|
|
|
|
++next;
|
|
|
|
monitored_bodies.remove(E);
|
2020-12-26 09:27:42 +00:00
|
|
|
E = next;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
Callable::CallError ce;
|
|
|
|
Variant ret;
|
|
|
|
monitor_callback.call((const Variant **)resptr, 5, ret, ce);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
monitored_bodies.clear();
|
|
|
|
monitor_callback = Callable();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
if (!area_monitor_callback.is_null() && !monitored_areas.is_empty()) {
|
|
|
|
if (area_monitor_callback.is_valid()) {
|
|
|
|
Variant res[5];
|
|
|
|
Variant *resptr[5];
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
resptr[i] = &res[i];
|
|
|
|
}
|
2015-03-17 03:45:25 +00:00
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_areas.begin(); E;) {
|
|
|
|
if (E->value.state == 0) { // Nothing happened
|
|
|
|
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
|
|
|
|
++next;
|
|
|
|
monitored_areas.remove(E);
|
2021-10-31 21:45:58 +00:00
|
|
|
E = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
|
|
|
|
res[1] = E->key.rid;
|
|
|
|
res[2] = E->key.instance_id;
|
|
|
|
res[3] = E->key.body_shape;
|
|
|
|
res[4] = E->key.area_shape;
|
2015-03-17 03:45:25 +00:00
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
|
|
|
|
++next;
|
|
|
|
monitored_areas.remove(E);
|
2020-12-26 09:27:42 +00:00
|
|
|
E = next;
|
2020-07-04 09:17:57 +00:00
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
Callable::CallError ce;
|
|
|
|
Variant ret;
|
|
|
|
area_monitor_callback.call((const Variant **)resptr, 5, ret, ce);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
monitored_areas.clear();
|
|
|
|
area_monitor_callback = Callable();
|
2015-03-17 03:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const {
|
2021-08-24 16:30:31 +00:00
|
|
|
if (is_gravity_point()) {
|
|
|
|
const real_t gravity_distance_scale = get_gravity_distance_scale();
|
|
|
|
Vector2 v = get_transform().xform(get_gravity_vector()) - p_position;
|
|
|
|
if (gravity_distance_scale > 0) {
|
|
|
|
const real_t v_length = v.length();
|
|
|
|
if (v_length > 0) {
|
|
|
|
const real_t v_scaled = v_length * gravity_distance_scale;
|
|
|
|
r_gravity = (v.normalized() * (get_gravity() / (v_scaled * v_scaled)));
|
|
|
|
} else {
|
|
|
|
r_gravity = Vector2();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r_gravity = v.normalized() * get_gravity();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r_gravity = get_gravity_vector() * get_gravity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotArea2D::GodotArea2D() :
|
|
|
|
GodotCollisionObject2D(TYPE_AREA),
|
2017-12-06 20:36:34 +00:00
|
|
|
monitor_query_list(this),
|
|
|
|
moved_list(this) {
|
2015-03-17 03:45:25 +00:00
|
|
|
_set_static(true); //areas are not active by default
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotArea2D::~GodotArea2D() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|