1 /*************************************************************************/ 2 /* space_sw.h */ 3 /*************************************************************************/ 4 /* This file is part of: */ 5 /* GODOT ENGINE */ 6 /* https://godotengine.org */ 7 /*************************************************************************/ 8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ 9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ 10 /* */ 11 /* Permission is hereby granted, free of charge, to any person obtaining */ 12 /* a copy of this software and associated documentation files (the */ 13 /* "Software"), to deal in the Software without restriction, including */ 14 /* without limitation the rights to use, copy, modify, merge, publish, */ 15 /* distribute, sublicense, and/or sell copies of the Software, and to */ 16 /* permit persons to whom the Software is furnished to do so, subject to */ 17 /* the following conditions: */ 18 /* */ 19 /* The above copyright notice and this permission notice shall be */ 20 /* included in all copies or substantial portions of the Software. */ 21 /* */ 22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ 25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ 26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ 27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ 28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 29 /*************************************************************************/ 30 31 #ifndef SPACE_SW_H 32 #define SPACE_SW_H 33 34 #include "area_pair_sw.h" 35 #include "area_sw.h" 36 #include "body_pair_sw.h" 37 #include "body_sw.h" 38 #include "broad_phase_sw.h" 39 #include "collision_object_sw.h" 40 #include "core/hash_map.h" 41 #include "core/project_settings.h" 42 #include "core/typedefs.h" 43 44 class PhysicsDirectSpaceStateSW : public PhysicsDirectSpaceState { 45 46 GDCLASS(PhysicsDirectSpaceStateSW, PhysicsDirectSpaceState); 47 48 public: 49 SpaceSW *space; 50 51 virtual int intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false); 52 virtual bool intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_ray = false); 53 virtual int intersect_shape(const RID &p_shape, const Transform &p_xform, real_t p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false); 54 virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, real_t p_margin, real_t &p_closest_safe, real_t &p_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, ShapeRestInfo *r_info = NULL); 55 virtual bool collide_shape(RID p_shape, const Transform &p_shape_xform, real_t p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false); 56 virtual bool rest_info(RID p_shape, const Transform &p_shape_xform, real_t p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false); 57 virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const; 58 59 PhysicsDirectSpaceStateSW(); 60 }; 61 62 class SpaceSW : public RID_Data { 63 64 public: 65 enum ElapsedTime { 66 ELAPSED_TIME_INTEGRATE_FORCES, 67 ELAPSED_TIME_GENERATE_ISLANDS, 68 ELAPSED_TIME_SETUP_CONSTRAINTS, 69 ELAPSED_TIME_SOLVE_CONSTRAINTS, 70 ELAPSED_TIME_INTEGRATE_VELOCITIES, 71 ELAPSED_TIME_MAX 72 73 }; 74 75 private: 76 uint64_t elapsed_time[ELAPSED_TIME_MAX]; 77 78 PhysicsDirectSpaceStateSW *direct_access; 79 RID self; 80 81 BroadPhaseSW *broadphase; 82 SelfList<BodySW>::List active_list; 83 SelfList<BodySW>::List inertia_update_list; 84 SelfList<BodySW>::List state_query_list; 85 SelfList<AreaSW>::List monitor_query_list; 86 SelfList<AreaSW>::List area_moved_list; 87 88 static void *_broadphase_pair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_self); 89 static void _broadphase_unpair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_data, void *p_self); 90 91 Set<CollisionObjectSW *> objects; 92 93 AreaSW *area; 94 95 real_t contact_recycle_radius; 96 real_t contact_max_separation; 97 real_t contact_max_allowed_penetration; 98 real_t constraint_bias; 99 real_t test_motion_min_contact_depth; 100 101 enum { 102 103 INTERSECTION_QUERY_MAX = 2048 104 }; 105 106 CollisionObjectSW *intersection_query_results[INTERSECTION_QUERY_MAX]; 107 int intersection_query_subindex_results[INTERSECTION_QUERY_MAX]; 108 109 real_t body_linear_velocity_sleep_threshold; 110 real_t body_angular_velocity_sleep_threshold; 111 real_t body_time_to_sleep; 112 real_t body_angular_velocity_damp_ratio; 113 114 bool locked; 115 116 int island_count; 117 int active_objects; 118 int collision_pairs; 119 120 RID static_global_body; 121 122 Vector<Vector3> contact_debug; 123 int contact_debug_count; 124 125 friend class PhysicsDirectSpaceStateSW; 126 127 int _cull_aabb_for_body(BodySW *p_body, const AABB &p_aabb); 128 129 public: set_self(const RID & p_self)130 _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; } get_self()131 _FORCE_INLINE_ RID get_self() const { return self; } 132 set_default_area(AreaSW * p_area)133 void set_default_area(AreaSW *p_area) { area = p_area; } get_default_area()134 AreaSW *get_default_area() const { return area; } 135 136 const SelfList<BodySW>::List &get_active_body_list() const; 137 void body_add_to_active_list(SelfList<BodySW> *p_body); 138 void body_remove_from_active_list(SelfList<BodySW> *p_body); 139 void body_add_to_inertia_update_list(SelfList<BodySW> *p_body); 140 void body_remove_from_inertia_update_list(SelfList<BodySW> *p_body); 141 142 void body_add_to_state_query_list(SelfList<BodySW> *p_body); 143 void body_remove_from_state_query_list(SelfList<BodySW> *p_body); 144 145 void area_add_to_monitor_query_list(SelfList<AreaSW> *p_area); 146 void area_remove_from_monitor_query_list(SelfList<AreaSW> *p_area); 147 void area_add_to_moved_list(SelfList<AreaSW> *p_area); 148 void area_remove_from_moved_list(SelfList<AreaSW> *p_area); 149 const SelfList<AreaSW>::List &get_moved_area_list() const; 150 151 BroadPhaseSW *get_broadphase(); 152 153 void add_object(CollisionObjectSW *p_object); 154 void remove_object(CollisionObjectSW *p_object); 155 const Set<CollisionObjectSW *> &get_objects() const; 156 get_contact_recycle_radius()157 _FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; } get_contact_max_separation()158 _FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; } get_contact_max_allowed_penetration()159 _FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; } get_constraint_bias()160 _FORCE_INLINE_ real_t get_constraint_bias() const { return constraint_bias; } get_body_linear_velocity_sleep_threshold()161 _FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; } get_body_angular_velocity_sleep_threshold()162 _FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; } get_body_time_to_sleep()163 _FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; } get_body_angular_velocity_damp_ratio()164 _FORCE_INLINE_ real_t get_body_angular_velocity_damp_ratio() const { return body_angular_velocity_damp_ratio; } 165 166 void update(); 167 void setup(); 168 void call_queries(); 169 170 bool is_locked() const; 171 void lock(); 172 void unlock(); 173 174 void set_param(PhysicsServer::SpaceParameter p_param, real_t p_value); 175 real_t get_param(PhysicsServer::SpaceParameter p_param) const; 176 set_island_count(int p_island_count)177 void set_island_count(int p_island_count) { island_count = p_island_count; } get_island_count()178 int get_island_count() const { return island_count; } 179 set_active_objects(int p_active_objects)180 void set_active_objects(int p_active_objects) { active_objects = p_active_objects; } get_active_objects()181 int get_active_objects() const { return active_objects; } 182 get_collision_pairs()183 int get_collision_pairs() const { return collision_pairs; } 184 185 PhysicsDirectSpaceStateSW *get_direct_state(); 186 set_debug_contacts(int p_amount)187 void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); } is_debugging_contacts()188 _FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.empty(); } add_debug_contact(const Vector3 & p_contact)189 _FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) { 190 if (contact_debug_count < contact_debug.size()) contact_debug.write[contact_debug_count++] = p_contact; 191 } get_debug_contacts()192 _FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contact_debug; } get_debug_contact_count()193 _FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; } 194 set_static_global_body(RID p_body)195 void set_static_global_body(RID p_body) { static_global_body = p_body; } get_static_global_body()196 RID get_static_global_body() { return static_global_body; } 197 set_elapsed_time(ElapsedTime p_time,uint64_t p_msec)198 void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; } get_elapsed_time(ElapsedTime p_time)199 uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; } 200 201 int test_body_ray_separation(BodySW *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer::SeparationResult *r_results, int p_result_max, real_t p_margin); 202 bool test_body_motion(BodySW *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, real_t p_margin, PhysicsServer::MotionResult *r_result, bool p_exclude_raycast_shapes); 203 204 SpaceSW(); 205 ~SpaceSW(); 206 }; 207 208 #endif // SPACE__SW_H 209