2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-03-28 12:19:05 +00:00
|
|
|
/* area_3d_sw.h */
|
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
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifndef AREA_SW_H
|
|
|
|
#define AREA_SW_H
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
#include "collision_object_3d_sw.h"
|
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_3d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
class Space3DSW;
|
|
|
|
class Body3DSW;
|
2021-07-20 04:23:32 +00:00
|
|
|
class SoftBody3DSW;
|
2020-03-27 18:21:27 +00:00
|
|
|
class Constraint3DSW;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
class Area3DSW : public CollisionObject3DSW {
|
2021-09-14 09:01:49 +00:00
|
|
|
PhysicsServer3D::AreaSpaceOverrideMode space_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
|
|
|
|
real_t gravity = 9.80665;
|
|
|
|
Vector3 gravity_vector = Vector3(0, -1, 0);
|
|
|
|
bool gravity_is_point = false;
|
|
|
|
real_t gravity_distance_scale = 0.0;
|
|
|
|
real_t point_attenuation = 1.0;
|
|
|
|
real_t linear_damp = 0.1;
|
|
|
|
real_t angular_damp = 0.1;
|
2021-07-20 04:23:32 +00:00
|
|
|
real_t wind_force_magnitude = 0.0;
|
|
|
|
real_t wind_attenuation_factor = 0.0;
|
|
|
|
Vector3 wind_source;
|
|
|
|
Vector3 wind_direction;
|
2021-09-14 09:01:49 +00:00
|
|
|
int priority = 0;
|
|
|
|
bool monitorable = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ObjectID monitor_callback_id;
|
|
|
|
StringName monitor_callback_method;
|
|
|
|
|
2015-06-12 18:52:21 +00:00
|
|
|
ObjectID area_monitor_callback_id;
|
|
|
|
StringName area_monitor_callback_method;
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
SelfList<Area3DSW> monitor_query_list;
|
|
|
|
SelfList<Area3DSW> 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
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ bool operator<(const BodyKey &p_key) const {
|
|
|
|
if (rid == p_key.rid) {
|
|
|
|
if (body_shape == p_key.body_shape) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return area_shape < p_key.area_shape;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2014-09-03 02:13:40 +00:00
|
|
|
return body_shape < p_key.body_shape;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
return rid < p_key.rid;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ BodyKey() {}
|
2021-07-20 04:23:32 +00:00
|
|
|
BodyKey(SoftBody3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
2020-03-27 18:21:27 +00:00
|
|
|
BodyKey(Body3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
|
|
|
BodyKey(Area3DSW *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--; }
|
|
|
|
};
|
|
|
|
|
2021-07-20 04:23:32 +00:00
|
|
|
Map<BodyKey, BodyState> monitored_soft_bodies;
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<BodyKey, BodyState> monitored_bodies;
|
|
|
|
Map<BodyKey, BodyState> monitored_areas;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
Set<Constraint3DSW *> constraints;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
virtual void _shapes_changed();
|
|
|
|
void _queue_monitor_update();
|
|
|
|
|
|
|
|
public:
|
2017-03-05 15:44:50 +00:00
|
|
|
void set_monitor_callback(ObjectID p_id, const StringName &p_method);
|
2020-02-12 17:24:06 +00:00
|
|
|
_FORCE_INLINE_ bool has_monitor_callback() const { return monitor_callback_id.is_valid(); }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void set_area_monitor_callback(ObjectID p_id, const StringName &p_method);
|
2020-02-12 17:24:06 +00:00
|
|
|
_FORCE_INLINE_ bool has_area_monitor_callback() const { return area_monitor_callback_id.is_valid(); }
|
2015-06-12 18:52:21 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
_FORCE_INLINE_ void add_body_to_query(Body3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
|
|
|
_FORCE_INLINE_ void remove_body_from_query(Body3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-07-20 04:23:32 +00:00
|
|
|
_FORCE_INLINE_ void add_soft_body_to_query(SoftBody3DSW *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape);
|
|
|
|
_FORCE_INLINE_ void remove_soft_body_from_query(SoftBody3DSW *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape);
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
_FORCE_INLINE_ void add_area_to_query(Area3DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
|
|
|
|
_FORCE_INLINE_ void remove_area_from_query(Area3DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
|
2015-06-12 18:52:21 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value);
|
|
|
|
Variant get_param(PhysicsServer3D::AreaParameter p_param) const;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_space_override_mode(PhysicsServer3D::AreaSpaceOverrideMode p_mode);
|
|
|
|
PhysicsServer3D::AreaSpaceOverrideMode get_space_override_mode() const { return space_override_mode; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; }
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ real_t get_gravity() const { return gravity; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_gravity_vector(const Vector3 &p_gravity) { gravity_vector = p_gravity; }
|
2014-02-10 01:10:30 +00:00
|
|
|
_FORCE_INLINE_ Vector3 get_gravity_vector() const { return gravity_vector; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; }
|
2014-02-10 01:10:30 +00:00
|
|
|
_FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_gravity_distance_scale(real_t scale) { gravity_distance_scale = scale; }
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ real_t get_gravity_distance_scale() const { return gravity_distance_scale; }
|
2015-03-19 14:10:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_point_attenuation(real_t p_point_attenuation) { point_attenuation = p_point_attenuation; }
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ real_t get_point_attenuation() const { return point_attenuation; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; }
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
|
2015-08-30 21:57:17 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; }
|
2017-02-13 23:25:05 +00:00
|
|
|
_FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; }
|
2014-02-10 01:10:30 +00:00
|
|
|
_FORCE_INLINE_ int get_priority() const { return priority; }
|
|
|
|
|
2021-07-20 04:23:32 +00:00
|
|
|
_FORCE_INLINE_ void set_wind_force_magnitude(real_t p_wind_force_magnitude) { wind_force_magnitude = p_wind_force_magnitude; }
|
|
|
|
_FORCE_INLINE_ real_t get_wind_force_magnitude() const { return wind_force_magnitude; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_wind_attenuation_factor(real_t p_wind_attenuation_factor) { wind_attenuation_factor = p_wind_attenuation_factor; }
|
|
|
|
_FORCE_INLINE_ real_t get_wind_attenuation_factor() const { return wind_attenuation_factor; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_wind_source(const Vector3 &p_wind_source) { wind_source = p_wind_source; }
|
|
|
|
_FORCE_INLINE_ const Vector3 &get_wind_source() const { return wind_source; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_wind_direction(const Vector3 &p_wind_direction) { wind_direction = p_wind_direction; }
|
|
|
|
_FORCE_INLINE_ const Vector3 &get_wind_direction() const { return wind_direction; }
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
_FORCE_INLINE_ void add_constraint(Constraint3DSW *p_constraint) { constraints.insert(p_constraint); }
|
|
|
|
_FORCE_INLINE_ void remove_constraint(Constraint3DSW *p_constraint) { constraints.erase(p_constraint); }
|
|
|
|
_FORCE_INLINE_ const Set<Constraint3DSW *> &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-06-12 18:52:21 +00:00
|
|
|
void set_monitorable(bool p_monitorable);
|
|
|
|
_FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
void set_transform(const Transform3D &p_transform);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_space(Space3DSW *p_space);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void call_queries();
|
|
|
|
|
2021-08-24 16:30:31 +00:00
|
|
|
void compute_gravity(const Vector3 &p_position, Vector3 &r_gravity) const;
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
Area3DSW();
|
|
|
|
~Area3DSW();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2021-07-20 04:23:32 +00:00
|
|
|
void Area3DSW::add_soft_body_to_query(SoftBody3DSW *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape) {
|
|
|
|
BodyKey bk(p_soft_body, p_soft_body_shape, p_area_shape);
|
|
|
|
monitored_soft_bodies[bk].inc();
|
|
|
|
if (!monitor_query_list.in_list()) {
|
|
|
|
_queue_monitor_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area3DSW::remove_soft_body_from_query(SoftBody3DSW *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape) {
|
|
|
|
BodyKey bk(p_soft_body, p_soft_body_shape, p_area_shape);
|
|
|
|
monitored_soft_bodies[bk].dec();
|
|
|
|
if (!monitor_query_list.in_list()) {
|
|
|
|
_queue_monitor_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void Area3DSW::add_body_to_query(Body3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
2017-03-05 15:44:50 +00:00
|
|
|
BodyKey bk(p_body, p_body_shape, p_area_shape);
|
2014-02-10 01:10:30 +00:00
|
|
|
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
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void Area3DSW::remove_body_from_query(Body3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
|
2017-03-05 15:44:50 +00:00
|
|
|
BodyKey bk(p_body, p_body_shape, p_area_shape);
|
2014-02-10 01:10:30 +00:00
|
|
|
monitored_bodies[bk].dec();
|
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-03-27 18:21:27 +00:00
|
|
|
void Area3DSW::add_area_to_query(Area3DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
|
2017-03-05 15:44:50 +00:00
|
|
|
BodyKey bk(p_area, p_area_shape, p_self_shape);
|
2015-06-12 18:52:21 +00:00
|
|
|
monitored_areas[bk].inc();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!monitor_query_list.in_list()) {
|
2015-06-12 18:52:21 +00:00
|
|
|
_queue_monitor_update();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-06-12 18:52:21 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void Area3DSW::remove_area_from_query(Area3DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
|
2017-03-05 15:44:50 +00:00
|
|
|
BodyKey bk(p_area, p_area_shape, p_self_shape);
|
2015-06-12 18:52:21 +00:00
|
|
|
monitored_areas[bk].dec();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!monitor_query_list.in_list()) {
|
2015-06-12 18:52:21 +00:00
|
|
|
_queue_monitor_update();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-06-12 18:52:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 04:23:32 +00:00
|
|
|
struct AreaCMP {
|
2021-09-14 09:01:49 +00:00
|
|
|
Area3DSW *area = nullptr;
|
|
|
|
int refCount = 0;
|
2021-07-20 04:23:32 +00:00
|
|
|
_FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
|
|
|
|
_FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }
|
|
|
|
_FORCE_INLINE_ AreaCMP() {}
|
|
|
|
_FORCE_INLINE_ AreaCMP(Area3DSW *p_area) {
|
|
|
|
area = p_area;
|
|
|
|
refCount = 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif // AREA__SW_H
|