Path: blob/master/modules/godot_physics_3d/godot_area_3d.h
10277 views
/**************************************************************************/1/* godot_area_3d.h */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#pragma once3132#include "godot_collision_object_3d.h"3334#include "core/templates/self_list.h"35#include "servers/physics_server_3d.h"3637class GodotSpace3D;38class GodotBody3D;39class GodotSoftBody3D;40class GodotConstraint3D;4142class GodotArea3D : public GodotCollisionObject3D {43PhysicsServer3D::AreaSpaceOverrideMode gravity_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;44PhysicsServer3D::AreaSpaceOverrideMode linear_damping_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;45PhysicsServer3D::AreaSpaceOverrideMode angular_damping_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;4647real_t gravity = 9.80665;48Vector3 gravity_vector = Vector3(0, -1, 0);49bool gravity_is_point = false;50real_t gravity_point_unit_distance = 0.0;51real_t linear_damp = 0.1;52real_t angular_damp = 0.1;53real_t wind_force_magnitude = 0.0;54real_t wind_attenuation_factor = 0.0;55Vector3 wind_source;56Vector3 wind_direction;57int priority = 0;58bool monitorable = false;5960Callable monitor_callback;61Callable area_monitor_callback;6263SelfList<GodotArea3D> monitor_query_list;64SelfList<GodotArea3D> moved_list;6566struct BodyKey {67RID rid;68ObjectID instance_id;69uint32_t body_shape = 0;70uint32_t area_shape = 0;7172static uint32_t hash(const BodyKey &p_key) {73uint32_t h = hash_one_uint64(p_key.rid.get_id());74h = hash_murmur3_one_64(p_key.instance_id, h);75h = hash_murmur3_one_32(p_key.area_shape, h);76return hash_fmix32(hash_murmur3_one_32(p_key.body_shape, h));77}7879_FORCE_INLINE_ bool operator==(const BodyKey &p_key) const {80return rid == p_key.rid && instance_id == p_key.instance_id && body_shape == p_key.body_shape && area_shape == p_key.area_shape;81}8283_FORCE_INLINE_ BodyKey() {}84BodyKey(GodotSoftBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);85BodyKey(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);86BodyKey(GodotArea3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);87};8889struct BodyState {90int state = 0;91_FORCE_INLINE_ void inc() { state++; }92_FORCE_INLINE_ void dec() { state--; }93};9495HashMap<BodyKey, BodyState, BodyKey> monitored_soft_bodies;96HashMap<BodyKey, BodyState, BodyKey> monitored_bodies;97HashMap<BodyKey, BodyState, BodyKey> monitored_areas;9899HashSet<GodotConstraint3D *> constraints;100101virtual void _shapes_changed() override;102void _queue_monitor_update();103104void _set_space_override_mode(PhysicsServer3D::AreaSpaceOverrideMode &r_mode, PhysicsServer3D::AreaSpaceOverrideMode p_new_mode);105106public:107void set_monitor_callback(const Callable &p_callback);108_FORCE_INLINE_ bool has_monitor_callback() const { return monitor_callback.is_valid(); }109110void set_area_monitor_callback(const Callable &p_callback);111_FORCE_INLINE_ bool has_area_monitor_callback() const { return area_monitor_callback.is_valid(); }112113_FORCE_INLINE_ void add_body_to_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);114_FORCE_INLINE_ void remove_body_from_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);115116_FORCE_INLINE_ void add_soft_body_to_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape);117_FORCE_INLINE_ void remove_soft_body_from_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape);118119_FORCE_INLINE_ void add_area_to_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);120_FORCE_INLINE_ void remove_area_from_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);121122void set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value);123Variant get_param(PhysicsServer3D::AreaParameter p_param) const;124125_FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; }126_FORCE_INLINE_ real_t get_gravity() const { return gravity; }127128_FORCE_INLINE_ void set_gravity_vector(const Vector3 &p_gravity) { gravity_vector = p_gravity; }129_FORCE_INLINE_ Vector3 get_gravity_vector() const { return gravity_vector; }130131_FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; }132_FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; }133134_FORCE_INLINE_ void set_gravity_point_unit_distance(real_t scale) { gravity_point_unit_distance = scale; }135_FORCE_INLINE_ real_t get_gravity_point_unit_distance() const { return gravity_point_unit_distance; }136137_FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; }138_FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }139140_FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; }141_FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }142143_FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; }144_FORCE_INLINE_ int get_priority() const { return priority; }145146_FORCE_INLINE_ void set_wind_force_magnitude(real_t p_wind_force_magnitude) { wind_force_magnitude = p_wind_force_magnitude; }147_FORCE_INLINE_ real_t get_wind_force_magnitude() const { return wind_force_magnitude; }148149_FORCE_INLINE_ void set_wind_attenuation_factor(real_t p_wind_attenuation_factor) { wind_attenuation_factor = p_wind_attenuation_factor; }150_FORCE_INLINE_ real_t get_wind_attenuation_factor() const { return wind_attenuation_factor; }151152_FORCE_INLINE_ void set_wind_source(const Vector3 &p_wind_source) { wind_source = p_wind_source; }153_FORCE_INLINE_ const Vector3 &get_wind_source() const { return wind_source; }154155_FORCE_INLINE_ void set_wind_direction(const Vector3 &p_wind_direction) { wind_direction = p_wind_direction; }156_FORCE_INLINE_ const Vector3 &get_wind_direction() const { return wind_direction; }157158_FORCE_INLINE_ void add_constraint(GodotConstraint3D *p_constraint) { constraints.insert(p_constraint); }159_FORCE_INLINE_ void remove_constraint(GodotConstraint3D *p_constraint) { constraints.erase(p_constraint); }160_FORCE_INLINE_ const HashSet<GodotConstraint3D *> &get_constraints() const { return constraints; }161_FORCE_INLINE_ void clear_constraints() { constraints.clear(); }162163void set_monitorable(bool p_monitorable);164_FORCE_INLINE_ bool is_monitorable() const { return monitorable; }165166void set_transform(const Transform3D &p_transform);167168void set_space(GodotSpace3D *p_space) override;169170void call_queries();171172void compute_gravity(const Vector3 &p_position, Vector3 &r_gravity) const;173174GodotArea3D();175~GodotArea3D();176};177178void GodotArea3D::add_soft_body_to_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape) {179BodyKey bk(p_soft_body, p_soft_body_shape, p_area_shape);180monitored_soft_bodies[bk].inc();181if (!monitor_query_list.in_list()) {182_queue_monitor_update();183}184}185186void GodotArea3D::remove_soft_body_from_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape) {187BodyKey bk(p_soft_body, p_soft_body_shape, p_area_shape);188monitored_soft_bodies[bk].dec();189if (get_space() && !monitor_query_list.in_list()) {190_queue_monitor_update();191}192}193194void GodotArea3D::add_body_to_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {195BodyKey bk(p_body, p_body_shape, p_area_shape);196monitored_bodies[bk].inc();197if (!monitor_query_list.in_list()) {198_queue_monitor_update();199}200}201202void GodotArea3D::remove_body_from_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {203BodyKey bk(p_body, p_body_shape, p_area_shape);204monitored_bodies[bk].dec();205if (get_space() && !monitor_query_list.in_list()) {206_queue_monitor_update();207}208}209210void GodotArea3D::add_area_to_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {211BodyKey bk(p_area, p_area_shape, p_self_shape);212monitored_areas[bk].inc();213if (!monitor_query_list.in_list()) {214_queue_monitor_update();215}216}217218void GodotArea3D::remove_area_from_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {219BodyKey bk(p_area, p_area_shape, p_self_shape);220monitored_areas[bk].dec();221if (get_space() && !monitor_query_list.in_list()) {222_queue_monitor_update();223}224}225226struct AreaCMP {227GodotArea3D *area = nullptr;228int refCount = 0;229_FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }230_FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }231_FORCE_INLINE_ AreaCMP() {}232_FORCE_INLINE_ AreaCMP(GodotArea3D *p_area) {233area = p_area;234refCount = 1;235}236};237238239