2014-02-10 01:10:30 +00:00
|
|
|
/**************************************************************************/
|
2021-10-18 19:24:30 +00:00
|
|
|
/* godot_area_2d.h */
|
2014-02-10 01:10:30 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#ifndef GODOT_AREA_2D_H
|
|
|
|
#define GODOT_AREA_2D_H
|
|
|
|
|
|
|
|
#include "godot_collision_object_2d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/templates/self_list.h"
|
2020-03-27 18:21:27 +00:00
|
|
|
#include "servers/physics_server_2d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
class GodotSpace2D;
|
|
|
|
class GodotBody2D;
|
|
|
|
class GodotConstraint2D;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
class GodotArea2D : public GodotCollisionObject2D {
|
2021-11-05 01:12:00 +00:00
|
|
|
PhysicsServer2D::AreaSpaceOverrideMode gravity_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
|
|
|
|
PhysicsServer2D::AreaSpaceOverrideMode linear_damping_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
|
|
|
|
PhysicsServer2D::AreaSpaceOverrideMode angular_damping_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
|
|
|
|
|
2021-09-14 09:01:49 +00:00
|
|
|
real_t gravity = 9.80665;
|
|
|
|
Vector2 gravity_vector = Vector2(0, -1);
|
|
|
|
bool gravity_is_point = false;
|
2023-01-30 00:11:41 +00:00
|
|
|
real_t gravity_point_unit_distance = 0.0;
|
2021-09-14 09:01:49 +00:00
|
|
|
real_t linear_damp = 0.1;
|
|
|
|
real_t angular_damp = 1.0;
|
|
|
|
int priority = 0;
|
|
|
|
bool monitorable = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
Callable monitor_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
Callable area_monitor_callback;
|
2015-03-17 03:45:25 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
SelfList<GodotArea2D> monitor_query_list;
|
|
|
|
SelfList<GodotArea2D> moved_list;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
struct BodyKey {
|
|
|
|
RID rid;
|
|
|
|
ObjectID instance_id;
|
2021-09-14 09:01:49 +00:00
|
|
|
uint32_t body_shape = 0;
|
|
|
|
uint32_t area_shape = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
static uint32_t hash(const BodyKey &p_key) {
|
|
|
|
uint32_t h = hash_one_uint64(p_key.rid.get_id());
|
2022-06-18 14:20:55 +00:00
|
|
|
h = hash_murmur3_one_64(p_key.instance_id, h);
|
|
|
|
h = hash_murmur3_one_32(p_key.area_shape, h);
|
|
|
|
return hash_fmix32(hash_murmur3_one_32(p_key.body_shape, h));
|
2022-05-13 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const BodyKey &p_key) const {
|
|
|
|
return rid == p_key.rid && instance_id == p_key.instance_id && body_shape == p_key.body_shape && area_shape == p_key.area_shape;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ BodyKey() {}
|
2021-10-18 19:24:30 +00:00
|
|
|
BodyKey(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
|
|
|
BodyKey(GodotArea2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BodyState {
|
2021-09-14 09:01:49 +00:00
|
|
|
int state = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
_FORCE_INLINE_ void inc() { state++; }
|
|
|
|
_FORCE_INLINE_ void dec() { state--; }
|
|
|
|
};
|
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
HashMap<BodyKey, BodyState, BodyKey> monitored_bodies;
|
|
|
|
HashMap<BodyKey, BodyState, BodyKey> monitored_areas;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-05-19 15:00:06 +00:00
|
|
|
HashSet<GodotConstraint2D *> constraints;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
virtual void _shapes_changed() override;
|
2014-02-10 01:10:30 +00:00
|
|
|
void _queue_monitor_update();
|
|
|
|
|
2021-11-05 01:12:00 +00:00
|
|
|
void _set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
public:
|
2021-10-31 21:45:58 +00:00
|
|
|
void set_monitor_callback(const Callable &p_callback);
|
|
|
|
_FORCE_INLINE_ bool has_monitor_callback() const { return !monitor_callback.is_null(); }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-10-31 21:45:58 +00:00
|
|
|
void set_area_monitor_callback(const Callable &p_callback);
|
|
|
|
_FORCE_INLINE_ bool has_area_monitor_callback() const { return !area_monitor_callback.is_null(); }
|
2015-03-17 03:45:25 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
_FORCE_INLINE_ void add_body_to_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
|
|
|
_FORCE_INLINE_ void remove_body_from_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
2015-03-17 03:45:25 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
_FORCE_INLINE_ void add_area_to_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
|
|
|
|
_FORCE_INLINE_ void remove_area_from_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value);
|
|
|
|
Variant get_param(PhysicsServer2D::AreaParameter p_param) const;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; }
|
|
|
|
_FORCE_INLINE_ real_t get_gravity() const { return gravity; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_gravity_vector(const Vector2 &p_gravity) { gravity_vector = p_gravity; }
|
|
|
|
_FORCE_INLINE_ Vector2 get_gravity_vector() const { return gravity_vector; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; }
|
|
|
|
_FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; }
|
|
|
|
|
2023-01-30 00:11:41 +00:00
|
|
|
_FORCE_INLINE_ void set_gravity_point_unit_distance(real_t scale) { gravity_point_unit_distance = scale; }
|
|
|
|
_FORCE_INLINE_ real_t get_gravity_point_unit_distance() const { return gravity_point_unit_distance; }
|
2015-04-29 16:06:25 +00:00
|
|
|
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; }
|
|
|
|
_FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
|
2015-01-05 21:37:12 +00:00
|
|
|
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; }
|
|
|
|
_FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; }
|
|
|
|
_FORCE_INLINE_ int get_priority() const { return priority; }
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
_FORCE_INLINE_ void add_constraint(GodotConstraint2D *p_constraint) { constraints.insert(p_constraint); }
|
|
|
|
_FORCE_INLINE_ void remove_constraint(GodotConstraint2D *p_constraint) { constraints.erase(p_constraint); }
|
2022-05-19 15:00:06 +00:00
|
|
|
_FORCE_INLINE_ const HashSet<GodotConstraint2D *> &get_constraints() const { return constraints; }
|
2017-07-25 02:26:47 +00:00
|
|
|
_FORCE_INLINE_ void clear_constraints() { constraints.clear(); }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-03-17 03:45:25 +00:00
|
|
|
void set_monitorable(bool p_monitorable);
|
|
|
|
_FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
|
|
|
|
|
2017-01-11 03:52:51 +00:00
|
|
|
void set_transform(const Transform2D &p_transform);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
void set_space(GodotSpace2D *p_space) override;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void call_queries();
|
|
|
|
|
2021-08-24 16:30:31 +00:00
|
|
|
void compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const;
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotArea2D();
|
|
|
|
~GodotArea2D();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::add_body_to_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
2014-02-10 01:10:30 +00:00
|
|
|
BodyKey bk(p_body, p_body_shape, p_area_shape);
|
|
|
|
monitored_bodies[bk].inc();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!monitor_query_list.in_list()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_queue_monitor_update();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::remove_body_from_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
2014-02-10 01:10:30 +00:00
|
|
|
BodyKey bk(p_body, p_body_shape, p_area_shape);
|
|
|
|
monitored_bodies[bk].dec();
|
2023-12-15 19:51:44 +00:00
|
|
|
if (get_space() && !monitor_query_list.in_list()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_queue_monitor_update();
|
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::add_area_to_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
|
2015-03-17 03:45:25 +00:00
|
|
|
BodyKey bk(p_area, p_area_shape, p_self_shape);
|
|
|
|
monitored_areas[bk].inc();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!monitor_query_list.in_list()) {
|
2015-03-17 03:45:25 +00:00
|
|
|
_queue_monitor_update();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-03-17 03:45:25 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotArea2D::remove_area_from_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
|
2015-03-17 03:45:25 +00:00
|
|
|
BodyKey bk(p_area, p_area_shape, p_self_shape);
|
|
|
|
monitored_areas[bk].dec();
|
2023-12-15 19:51:44 +00:00
|
|
|
if (get_space() && !monitor_query_list.in_list()) {
|
2015-03-17 03:45:25 +00:00
|
|
|
_queue_monitor_update();
|
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-10-18 19:24:30 +00:00
|
|
|
#endif // GODOT_AREA_2D_H
|