| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "godot_area_3d.h" |
| 34 |
#include "godot_body_3d.h" |
| 35 |
#include "godot_broad_phase_3d.h" |
| 36 |
#include "godot_collision_object_3d.h" |
| 37 |
#include "godot_soft_body_3d.h" |
| 38 |
|
| 39 |
#include "core/typedefs.h" |
| 40 |
|
| 41 |
class GodotPhysicsDirectSpaceState3D : public PhysicsDirectSpaceState3D { |
| 42 |
GDCLASS(GodotPhysicsDirectSpaceState3D, PhysicsDirectSpaceState3D); |
| 43 |
|
| 44 |
public: |
| 45 |
GodotSpace3D *space = nullptr; |
| 46 |
|
| 47 |
virtual int intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) override; |
| 48 |
virtual bool intersect_ray(const RayParameters &p_parameters, RayResult &r_result) override; |
| 49 |
virtual int intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) override; |
| 50 |
virtual bool cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe, ShapeRestInfo *r_info = nullptr) override; |
| 51 |
virtual bool collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) override; |
| 52 |
virtual bool rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) override; |
| 53 |
virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const override; |
| 54 |
|
| 55 |
GodotPhysicsDirectSpaceState3D(); |
| 56 |
}; |
| 57 |
|
| 58 |
class GodotSpace3D { |
| 59 |
public: |
| 60 |
enum ElapsedTime { |
| 61 |
ELAPSED_TIME_INTEGRATE_FORCES, |
| 62 |
ELAPSED_TIME_GENERATE_ISLANDS, |
| 63 |
ELAPSED_TIME_SETUP_CONSTRAINTS, |
| 64 |
ELAPSED_TIME_SOLVE_CONSTRAINTS, |
| 65 |
ELAPSED_TIME_INTEGRATE_VELOCITIES, |
| 66 |
ELAPSED_TIME_MAX |
| 67 |
|
| 68 |
}; |
| 69 |
|
| 70 |
private: |
| 71 |
uint64_t elapsed_time[ELAPSED_TIME_MAX] = {}; |
| 72 |
|
| 73 |
GodotPhysicsDirectSpaceState3D *direct_access = nullptr; |
| 74 |
RID self; |
| 75 |
|
| 76 |
GodotBroadPhase3D *broadphase = nullptr; |
| 77 |
SelfList<GodotBody3D>::List active_list; |
| 78 |
SelfList<GodotBody3D>::List mass_properties_update_list; |
| 79 |
SelfList<GodotBody3D>::List state_query_list; |
| 80 |
SelfList<GodotArea3D>::List monitor_query_list; |
| 81 |
SelfList<GodotArea3D>::List area_moved_list; |
| 82 |
SelfList<GodotSoftBody3D>::List active_soft_body_list; |
| 83 |
|
| 84 |
static void *_broadphase_pair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_self); |
| 85 |
static void _broadphase_unpair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_data, void *p_self); |
| 86 |
|
| 87 |
HashSet<GodotCollisionObject3D *> objects; |
| 88 |
|
| 89 |
GodotArea3D *area = nullptr; |
| 90 |
|
| 91 |
int solver_iterations = 0; |
| 92 |
|
| 93 |
real_t contact_recycle_radius = 0.0; |
| 94 |
real_t contact_max_separation = 0.0; |
| 95 |
real_t contact_max_allowed_penetration = 0.0; |
| 96 |
real_t contact_bias = 0.0; |
| 97 |
|
| 98 |
enum { |
| 99 |
INTERSECTION_QUERY_MAX = 2048 |
| 100 |
}; |
| 101 |
|
| 102 |
GodotCollisionObject3D *intersection_query_results[INTERSECTION_QUERY_MAX]; |
| 103 |
int intersection_query_subindex_results[INTERSECTION_QUERY_MAX]; |
| 104 |
|
| 105 |
real_t body_linear_velocity_sleep_threshold = 0.0; |
| 106 |
real_t body_angular_velocity_sleep_threshold = 0.0; |
| 107 |
real_t body_time_to_sleep = 0.0; |
| 108 |
|
| 109 |
bool locked = false; |
| 110 |
|
| 111 |
real_t last_step = 0.001; |
| 112 |
|
| 113 |
int island_count = 0; |
| 114 |
int active_objects = 0; |
| 115 |
int collision_pairs = 0; |
| 116 |
|
| 117 |
RID static_global_body; |
| 118 |
|
| 119 |
Vector<Vector3> contact_debug; |
| 120 |
int contact_debug_count = 0; |
| 121 |
|
| 122 |
friend class GodotPhysicsDirectSpaceState3D; |
| 123 |
|
| 124 |
int _cull_aabb_for_body(GodotBody3D *p_body, const AABB &p_aabb); |
| 125 |
|
| 126 |
public: |
| 127 |
_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; } |
| 128 |
_FORCE_INLINE_ RID get_self() const { return self; } |
| 129 |
|
| 130 |
void set_default_area(GodotArea3D *p_area) { area = p_area; } |
| 131 |
GodotArea3D *get_default_area() const { return area; } |
| 132 |
|
| 133 |
const SelfList<GodotBody3D>::List &get_active_body_list() const; |
| 134 |
void body_add_to_active_list(SelfList<GodotBody3D> *p_body); |
| 135 |
void body_remove_from_active_list(SelfList<GodotBody3D> *p_body); |
| 136 |
void body_add_to_mass_properties_update_list(SelfList<GodotBody3D> *p_body); |
| 137 |
void body_remove_from_mass_properties_update_list(SelfList<GodotBody3D> *p_body); |
| 138 |
|
| 139 |
void body_add_to_state_query_list(SelfList<GodotBody3D> *p_body); |
| 140 |
void body_remove_from_state_query_list(SelfList<GodotBody3D> *p_body); |
| 141 |
|
| 142 |
void area_add_to_monitor_query_list(SelfList<GodotArea3D> *p_area); |
| 143 |
void area_remove_from_monitor_query_list(SelfList<GodotArea3D> *p_area); |
| 144 |
void area_add_to_moved_list(SelfList<GodotArea3D> *p_area); |
| 145 |
void area_remove_from_moved_list(SelfList<GodotArea3D> *p_area); |
| 146 |
const SelfList<GodotArea3D>::List &get_moved_area_list() const; |
| 147 |
|
| 148 |
const SelfList<GodotSoftBody3D>::List &get_active_soft_body_list() const; |
| 149 |
void soft_body_add_to_active_list(SelfList<GodotSoftBody3D> *p_soft_body); |
| 150 |
void soft_body_remove_from_active_list(SelfList<GodotSoftBody3D> *p_soft_body); |
| 151 |
|
| 152 |
GodotBroadPhase3D *get_broadphase(); |
| 153 |
|
| 154 |
void add_object(GodotCollisionObject3D *p_object); |
| 155 |
void remove_object(GodotCollisionObject3D *p_object); |
| 156 |
const HashSet<GodotCollisionObject3D *> &get_objects() const; |
| 157 |
|
| 158 |
_FORCE_INLINE_ int get_solver_iterations() const { return solver_iterations; } |
| 159 |
_FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; } |
| 160 |
_FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; } |
| 161 |
_FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; } |
| 162 |
_FORCE_INLINE_ real_t get_contact_bias() const { return contact_bias; } |
| 163 |
_FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; } |
| 164 |
_FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; } |
| 165 |
_FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; } |
| 166 |
|
| 167 |
void update(); |
| 168 |
void setup(); |
| 169 |
void call_queries(); |
| 170 |
|
| 171 |
bool is_locked() const; |
| 172 |
void lock(); |
| 173 |
void unlock(); |
| 174 |
|
| 175 |
real_t get_last_step() const { return last_step; } |
| 176 |
void set_last_step(real_t p_step) { last_step = p_step; } |
| 177 |
|
| 178 |
void set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value); |
| 179 |
real_t get_param(PhysicsServer3D::SpaceParameter p_param) const; |
| 180 |
|
| 181 |
void set_island_count(int p_island_count) { island_count = p_island_count; } |
| 182 |
int get_island_count() const { return island_count; } |
| 183 |
|
| 184 |
void set_active_objects(int p_active_objects) { active_objects = p_active_objects; } |
| 185 |
int get_active_objects() const { return active_objects; } |
| 186 |
|
| 187 |
int get_collision_pairs() const { return collision_pairs; } |
| 188 |
|
| 189 |
GodotPhysicsDirectSpaceState3D *get_direct_state(); |
| 190 |
|
| 191 |
void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); } |
| 192 |
_FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.is_empty(); } |
| 193 |
_FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) { |
| 194 |
if (contact_debug_count < contact_debug.size()) { |
| 195 |
contact_debug.write[contact_debug_count++] = p_contact; |
| 196 |
} |
| 197 |
} |
| 198 |
_FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contact_debug; } |
| 199 |
_FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; } |
| 200 |
|
| 201 |
void set_static_global_body(RID p_body) { static_global_body = p_body; } |
| 202 |
RID get_static_global_body() { return static_global_body; } |
| 203 |
|
| 204 |
void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; } |
| 205 |
uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; } |
| 206 |
|
| 207 |
bool test_body_motion(GodotBody3D *p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result); |
| 208 |
|
| 209 |
GodotSpace3D(); |
| 210 |
~GodotSpace3D(); |
| 211 |
}; |