Path: blob/master/modules/godot_physics_3d/godot_area_3d.cpp
10278 views
/**************************************************************************/1/* godot_area_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "godot_area_3d.h"3132#include "godot_body_3d.h"33#include "godot_soft_body_3d.h"34#include "godot_space_3d.h"3536GodotArea3D::BodyKey::BodyKey(GodotSoftBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {37rid = p_body->get_self();38instance_id = p_body->get_instance_id();39body_shape = p_body_shape;40area_shape = p_area_shape;41}4243GodotArea3D::BodyKey::BodyKey(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {44rid = p_body->get_self();45instance_id = p_body->get_instance_id();46body_shape = p_body_shape;47area_shape = p_area_shape;48}4950GodotArea3D::BodyKey::BodyKey(GodotArea3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {51rid = p_body->get_self();52instance_id = p_body->get_instance_id();53body_shape = p_body_shape;54area_shape = p_area_shape;55}5657void GodotArea3D::_shapes_changed() {58if (!moved_list.in_list() && get_space()) {59get_space()->area_add_to_moved_list(&moved_list);60}61}6263void GodotArea3D::set_transform(const Transform3D &p_transform) {64if (!moved_list.in_list() && get_space()) {65get_space()->area_add_to_moved_list(&moved_list);66}6768_set_transform(p_transform);69_set_inv_transform(p_transform.affine_inverse());70}7172void GodotArea3D::set_space(GodotSpace3D *p_space) {73if (get_space()) {74if (monitor_query_list.in_list()) {75get_space()->area_remove_from_monitor_query_list(&monitor_query_list);76}77if (moved_list.in_list()) {78get_space()->area_remove_from_moved_list(&moved_list);79}80}8182monitored_bodies.clear();83monitored_areas.clear();8485_set_space(p_space);86}8788void GodotArea3D::set_monitor_callback(const Callable &p_callback) {89_unregister_shapes();9091monitor_callback = p_callback;9293monitored_bodies.clear();94monitored_areas.clear();9596_shape_changed();9798if (!moved_list.in_list() && get_space()) {99get_space()->area_add_to_moved_list(&moved_list);100}101}102103void GodotArea3D::set_area_monitor_callback(const Callable &p_callback) {104_unregister_shapes();105106area_monitor_callback = p_callback;107108monitored_bodies.clear();109monitored_areas.clear();110111_shape_changed();112113if (!moved_list.in_list() && get_space()) {114get_space()->area_add_to_moved_list(&moved_list);115}116}117118void GodotArea3D::_set_space_override_mode(PhysicsServer3D::AreaSpaceOverrideMode &r_mode, PhysicsServer3D::AreaSpaceOverrideMode p_new_mode) {119bool do_override = p_new_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;120if (do_override == (r_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED)) {121return;122}123_unregister_shapes();124r_mode = p_new_mode;125_shape_changed();126}127128void GodotArea3D::set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value) {129switch (p_param) {130case PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:131_set_space_override_mode(gravity_override_mode, (PhysicsServer3D::AreaSpaceOverrideMode)(int)p_value);132break;133case PhysicsServer3D::AREA_PARAM_GRAVITY:134gravity = p_value;135break;136case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:137gravity_vector = p_value;138break;139case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:140gravity_is_point = p_value;141break;142case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE:143gravity_point_unit_distance = p_value;144break;145case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:146_set_space_override_mode(linear_damping_override_mode, (PhysicsServer3D::AreaSpaceOverrideMode)(int)p_value);147break;148case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:149linear_damp = p_value;150break;151case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:152_set_space_override_mode(angular_damping_override_mode, (PhysicsServer3D::AreaSpaceOverrideMode)(int)p_value);153break;154case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:155angular_damp = p_value;156break;157case PhysicsServer3D::AREA_PARAM_PRIORITY:158priority = p_value;159break;160case PhysicsServer3D::AREA_PARAM_WIND_FORCE_MAGNITUDE:161ERR_FAIL_COND_MSG(wind_force_magnitude < 0, "Wind force magnitude must be a non-negative real number, but a negative number was specified.");162wind_force_magnitude = p_value;163break;164case PhysicsServer3D::AREA_PARAM_WIND_SOURCE:165wind_source = p_value;166break;167case PhysicsServer3D::AREA_PARAM_WIND_DIRECTION:168wind_direction = p_value;169break;170case PhysicsServer3D::AREA_PARAM_WIND_ATTENUATION_FACTOR:171ERR_FAIL_COND_MSG(wind_attenuation_factor < 0, "Wind attenuation factor must be a non-negative real number, but a negative number was specified.");172wind_attenuation_factor = p_value;173break;174}175}176177Variant GodotArea3D::get_param(PhysicsServer3D::AreaParameter p_param) const {178switch (p_param) {179case PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:180return gravity_override_mode;181case PhysicsServer3D::AREA_PARAM_GRAVITY:182return gravity;183case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:184return gravity_vector;185case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:186return gravity_is_point;187case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE:188return gravity_point_unit_distance;189case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:190return linear_damping_override_mode;191case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:192return linear_damp;193case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:194return angular_damping_override_mode;195case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:196return angular_damp;197case PhysicsServer3D::AREA_PARAM_PRIORITY:198return priority;199case PhysicsServer3D::AREA_PARAM_WIND_FORCE_MAGNITUDE:200return wind_force_magnitude;201case PhysicsServer3D::AREA_PARAM_WIND_SOURCE:202return wind_source;203case PhysicsServer3D::AREA_PARAM_WIND_DIRECTION:204return wind_direction;205case PhysicsServer3D::AREA_PARAM_WIND_ATTENUATION_FACTOR:206return wind_attenuation_factor;207}208209return Variant();210}211212void GodotArea3D::_queue_monitor_update() {213ERR_FAIL_NULL(get_space());214215if (!monitor_query_list.in_list()) {216get_space()->area_add_to_monitor_query_list(&monitor_query_list);217}218}219220void GodotArea3D::set_monitorable(bool p_monitorable) {221if (monitorable == p_monitorable) {222return;223}224225monitorable = p_monitorable;226_set_static(!monitorable);227_shapes_changed();228}229230void GodotArea3D::call_queries() {231if (!monitor_callback.is_null() && !monitored_bodies.is_empty()) {232if (monitor_callback.is_valid()) {233Variant res[5];234Variant *resptr[5];235for (int i = 0; i < 5; i++) {236resptr[i] = &res[i];237}238239for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_bodies.begin(); E;) {240if (E->value.state == 0) { // Nothing happened241HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;242++next;243monitored_bodies.remove(E);244E = next;245continue;246}247248res[0] = E->value.state > 0 ? PhysicsServer3D::AREA_BODY_ADDED : PhysicsServer3D::AREA_BODY_REMOVED;249res[1] = E->key.rid;250res[2] = E->key.instance_id;251res[3] = E->key.body_shape;252res[4] = E->key.area_shape;253254HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;255++next;256monitored_bodies.remove(E);257E = next;258259Callable::CallError ce;260Variant ret;261monitor_callback.callp((const Variant **)resptr, 5, ret, ce);262263if (ce.error != Callable::CallError::CALL_OK) {264ERR_PRINT_ONCE("Error calling monitor callback method " + Variant::get_callable_error_text(monitor_callback, (const Variant **)resptr, 5, ce));265}266}267} else {268monitored_bodies.clear();269monitor_callback = Callable();270}271}272273if (!area_monitor_callback.is_null() && !monitored_areas.is_empty()) {274if (area_monitor_callback.is_valid()) {275Variant res[5];276Variant *resptr[5];277for (int i = 0; i < 5; i++) {278resptr[i] = &res[i];279}280281for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_areas.begin(); E;) {282if (E->value.state == 0) { // Nothing happened283HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;284++next;285monitored_areas.remove(E);286E = next;287continue;288}289290res[0] = E->value.state > 0 ? PhysicsServer3D::AREA_BODY_ADDED : PhysicsServer3D::AREA_BODY_REMOVED;291res[1] = E->key.rid;292res[2] = E->key.instance_id;293res[3] = E->key.body_shape;294res[4] = E->key.area_shape;295296HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;297++next;298monitored_areas.remove(E);299E = next;300301Callable::CallError ce;302Variant ret;303area_monitor_callback.callp((const Variant **)resptr, 5, ret, ce);304305if (ce.error != Callable::CallError::CALL_OK) {306ERR_PRINT_ONCE("Error calling area monitor callback method " + Variant::get_callable_error_text(area_monitor_callback, (const Variant **)resptr, 5, ce));307}308}309} else {310monitored_areas.clear();311area_monitor_callback = Callable();312}313}314}315316void GodotArea3D::compute_gravity(const Vector3 &p_position, Vector3 &r_gravity) const {317if (is_gravity_point()) {318const real_t gr_unit_dist = get_gravity_point_unit_distance();319Vector3 v = get_transform().xform(get_gravity_vector()) - p_position;320if (gr_unit_dist > 0) {321const real_t v_length_sq = v.length_squared();322if (v_length_sq > 0) {323const real_t gravity_strength = get_gravity() * gr_unit_dist * gr_unit_dist / v_length_sq;324r_gravity = v.normalized() * gravity_strength;325} else {326r_gravity = Vector3();327}328} else {329r_gravity = v.normalized() * get_gravity();330}331} else {332r_gravity = get_gravity_vector() * get_gravity();333}334}335336GodotArea3D::GodotArea3D() :337GodotCollisionObject3D(TYPE_AREA),338monitor_query_list(this),339moved_list(this) {340_set_static(true); //areas are never active341set_ray_pickable(false);342}343344GodotArea3D::~GodotArea3D() {345}346347348