2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* area_2d_sw.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01: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-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "area_2d_sw.h"
|
|
|
|
#include "body_2d_sw.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "space_2d_sw.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Area2DSW::BodyKey::BodyKey(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
|
|
|
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 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Area2DSW::BodyKey::BodyKey(Area2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
|
|
|
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 02:10:30 +01:00
|
|
|
|
|
|
|
void Area2DSW::_shapes_changed() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2019-11-14 08:48:41 +01:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Area2DSW::set_transform(const Transform2D &p_transform) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_set_transform(p_transform);
|
2014-09-15 16:33:30 +02:00
|
|
|
_set_inv_transform(p_transform.affine_inverse());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Area2DSW::set_space(Space2DSW *p_space) {
|
|
|
|
if (get_space()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (monitor_query_list.in_list()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (moved_list.in_list()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
get_space()->area_remove_from_moved_list(&moved_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
monitored_bodies.clear();
|
2015-03-17 04:45:25 +01:00
|
|
|
monitored_areas.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_set_space(p_space);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Area2DSW::set_monitor_callback(ObjectID p_id, const StringName &p_method) {
|
|
|
|
if (p_id == monitor_callback_id) {
|
|
|
|
monitor_callback_method = p_method;
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_unregister_shapes();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
monitor_callback_id = p_id;
|
|
|
|
monitor_callback_method = p_method;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
monitored_bodies.clear();
|
2015-03-17 04:45:25 +01:00
|
|
|
monitored_areas.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_shape_changed();
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2015-09-04 04:24:55 +02:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Area2DSW::set_area_monitor_callback(ObjectID p_id, const StringName &p_method) {
|
|
|
|
if (p_id == area_monitor_callback_id) {
|
|
|
|
area_monitor_callback_method = p_method;
|
2015-03-17 04:45:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_unregister_shapes();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
area_monitor_callback_id = p_id;
|
|
|
|
area_monitor_callback_method = p_method;
|
2015-03-17 04:45:25 +01:00
|
|
|
|
|
|
|
monitored_bodies.clear();
|
|
|
|
monitored_areas.clear();
|
|
|
|
|
|
|
|
_shape_changed();
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!moved_list.in_list() && get_space()) {
|
2015-09-04 04:24:55 +02:00
|
|
|
get_space()->area_add_to_moved_list(&moved_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-03-17 04:45:25 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Area2DSW::set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode p_mode) {
|
|
|
|
bool do_override = p_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (do_override == (space_override_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
_unregister_shapes();
|
2017-03-05 16:44:50 +01:00
|
|
|
space_override_mode = p_mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
_shape_changed();
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Area2DSW::set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2020-05-10 13:00:47 +02: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;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
|
|
|
|
linear_damp = p_value;
|
|
|
|
break;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
|
|
|
|
angular_damp = p_value;
|
|
|
|
break;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_PRIORITY:
|
|
|
|
priority = p_value;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Variant Area2DSW::get_param(PhysicsServer2D::AreaParameter p_param) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2020-05-10 13:00:47 +02: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;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
|
|
|
|
return linear_damp;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
|
|
|
|
return angular_damp;
|
|
|
|
case PhysicsServer2D::AREA_PARAM_PRIORITY:
|
|
|
|
return priority;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area2DSW::_queue_monitor_update() {
|
|
|
|
ERR_FAIL_COND(!get_space());
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!monitor_query_list.in_list()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
get_space()->area_add_to_monitor_query_list(&monitor_query_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 04:45:25 +01:00
|
|
|
void Area2DSW::set_monitorable(bool p_monitorable) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (monitorable == p_monitorable) {
|
2015-03-17 04:45:25 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-03-17 04:45:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
monitorable = p_monitorable;
|
2015-03-17 04:45:25 +01:00
|
|
|
_set_static(!monitorable);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Area2DSW::call_queries() {
|
2020-02-12 18:24:06 +01:00
|
|
|
if (monitor_callback_id.is_valid() && !monitored_bodies.empty()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Variant res[5];
|
|
|
|
Variant *resptr[5];
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int i = 0; i < 5; i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
resptr[i] = &res[i];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Object *obj = ObjectDB::get_instance(monitor_callback_id);
|
|
|
|
if (!obj) {
|
|
|
|
monitored_bodies.clear();
|
2020-02-12 18:24:06 +01:00
|
|
|
monitor_callback_id = ObjectID();
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E; E = E->next()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (E->get().state == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue; //nothing happened
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
res[0] = E->get().state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
|
2017-03-05 16:44:50 +01:00
|
|
|
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 02:10:30 +01:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::CallError ce;
|
2017-03-05 16:44:50 +01:00
|
|
|
obj->call(monitor_callback_method, (const Variant **)resptr, 5, ce);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monitored_bodies.clear();
|
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
if (area_monitor_callback_id.is_valid() && !monitored_areas.empty()) {
|
2015-03-17 04:45:25 +01:00
|
|
|
Variant res[5];
|
|
|
|
Variant *resptr[5];
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int i = 0; i < 5; i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
resptr[i] = &res[i];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-03-17 04:45:25 +01:00
|
|
|
|
|
|
|
Object *obj = ObjectDB::get_instance(area_monitor_callback_id);
|
|
|
|
if (!obj) {
|
|
|
|
monitored_areas.clear();
|
2020-02-12 18:24:06 +01:00
|
|
|
area_monitor_callback_id = ObjectID();
|
2015-03-17 04:45:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E; E = E->next()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (E->get().state == 0) {
|
2015-03-17 04:45:25 +01:00
|
|
|
continue; //nothing happened
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-03-17 04:45:25 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
res[0] = E->get().state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
|
2017-03-05 16:44:50 +01:00
|
|
|
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 04:45:25 +01:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::CallError ce;
|
2017-03-05 16:44:50 +01:00
|
|
|
obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);
|
2015-03-17 04:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monitored_areas.clear();
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
Area2DSW::Area2DSW() :
|
|
|
|
CollisionObject2DSW(TYPE_AREA),
|
|
|
|
monitor_query_list(this),
|
|
|
|
moved_list(this) {
|
2015-03-17 04:45:25 +01:00
|
|
|
_set_static(true); //areas are not active by default
|
2020-03-27 19:21:27 +01:00
|
|
|
space_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity = 9.80665;
|
|
|
|
gravity_vector = Vector2(0, -1);
|
|
|
|
gravity_is_point = false;
|
|
|
|
gravity_distance_scale = 0;
|
|
|
|
point_attenuation = 1;
|
|
|
|
|
|
|
|
angular_damp = 1.0;
|
|
|
|
linear_damp = 0.1;
|
|
|
|
priority = 0;
|
|
|
|
monitorable = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Area2DSW::~Area2DSW() {
|
|
|
|
}
|