space_sw.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*************************************************************************/
  2. /* space_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  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. /*************************************************************************/
  29. #include "globals.h"
  30. #include "space_sw.h"
  31. #include "collision_solver_sw.h"
  32. #include "physics_server_sw.h"
  33. bool PhysicsDirectSpaceStateSW::intersect_ray(const Vector3& p_from, const Vector3& p_to,RayResult &r_result,const Set<RID>& p_exclude,uint32_t p_user_mask) {
  34. ERR_FAIL_COND_V(space->locked,false);
  35. Vector3 begin,end;
  36. Vector3 normal;
  37. begin=p_from;
  38. end=p_to;
  39. normal=(end-begin).normalized();
  40. int amount = space->broadphase->cull_segment(begin,end,space->intersection_query_results,SpaceSW::INTERSECTION_QUERY_MAX,space->intersection_query_subindex_results);
  41. //todo, create another array tha references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision
  42. bool collided=false;
  43. Vector3 res_point,res_normal;
  44. int res_shape;
  45. const CollisionObjectSW *res_obj;
  46. real_t min_d=1e10;
  47. for(int i=0;i<amount;i++) {
  48. if (space->intersection_query_results[i]->get_type()==CollisionObjectSW::TYPE_AREA)
  49. continue; //ignore area
  50. if (p_exclude.has( space->intersection_query_results[i]->get_self()))
  51. continue;
  52. const CollisionObjectSW *col_obj=space->intersection_query_results[i];
  53. int shape_idx=space->intersection_query_subindex_results[i];
  54. Transform inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
  55. Vector3 local_from = inv_xform.xform(begin);
  56. Vector3 local_to = inv_xform.xform(end);
  57. const ShapeSW *shape = col_obj->get_shape(shape_idx);
  58. Vector3 shape_point,shape_normal;
  59. if (shape->intersect_segment(local_from,local_to,shape_point,shape_normal)) {
  60. Transform xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  61. shape_point=xform.xform(shape_point);
  62. real_t ld = normal.dot(shape_point);
  63. if (ld<min_d) {
  64. min_d=ld;
  65. res_point=shape_point;
  66. res_normal=inv_xform.basis.xform_inv(shape_normal).normalized();
  67. res_shape=shape_idx;
  68. res_obj=col_obj;
  69. collided=true;
  70. }
  71. }
  72. }
  73. if (!collided)
  74. return false;
  75. r_result.collider_id=res_obj->get_instance_id();
  76. if (r_result.collider_id!=0)
  77. r_result.collider=ObjectDB::get_instance(r_result.collider_id);
  78. r_result.normal=res_normal;
  79. r_result.position=res_point;
  80. r_result.rid=res_obj->get_self();
  81. r_result.shape=res_shape;
  82. return true;
  83. }
  84. int PhysicsDirectSpaceStateSW::intersect_shape(const RID& p_shape, const Transform& p_xform,ShapeResult *r_results,int p_result_max,const Set<RID>& p_exclude,uint32_t p_user_mask) {
  85. if (p_result_max<=0)
  86. return 0;
  87. ShapeSW *shape = static_cast<PhysicsServerSW*>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
  88. ERR_FAIL_COND_V(!shape,0);
  89. AABB aabb = p_xform.xform(shape->get_aabb());
  90. int amount = space->broadphase->cull_aabb(aabb,space->intersection_query_results,SpaceSW::INTERSECTION_QUERY_MAX,space->intersection_query_subindex_results);
  91. bool collided=false;
  92. int cc=0;
  93. //Transform ai = p_xform.affine_inverse();
  94. for(int i=0;i<amount;i++) {
  95. if (cc>=p_result_max)
  96. break;
  97. if (space->intersection_query_results[i]->get_type()==CollisionObjectSW::TYPE_AREA)
  98. continue; //ignore area
  99. if (p_exclude.has( space->intersection_query_results[i]->get_self()))
  100. continue;
  101. const CollisionObjectSW *col_obj=space->intersection_query_results[i];
  102. int shape_idx=space->intersection_query_subindex_results[i];
  103. if (!CollisionSolverSW::solve_static(shape,p_xform,col_obj->get_shape(shape_idx),col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), NULL,NULL,NULL))
  104. continue;
  105. r_results[cc].collider_id=col_obj->get_instance_id();
  106. if (r_results[cc].collider_id!=0)
  107. r_results[cc].collider=ObjectDB::get_instance(r_results[cc].collider_id);
  108. r_results[cc].rid=col_obj->get_self();
  109. r_results[cc].shape=shape_idx;
  110. cc++;
  111. }
  112. return cc;
  113. }
  114. PhysicsDirectSpaceStateSW::PhysicsDirectSpaceStateSW() {
  115. space=NULL;
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. void* SpaceSW::_broadphase_pair(CollisionObjectSW *A,int p_subindex_A,CollisionObjectSW *B,int p_subindex_B,void *p_self) {
  119. CollisionObjectSW::Type type_A=A->get_type();
  120. CollisionObjectSW::Type type_B=B->get_type();
  121. if (type_A>type_B) {
  122. SWAP(A,B);
  123. SWAP(p_subindex_A,p_subindex_B);
  124. SWAP(type_A,type_B);
  125. }
  126. SpaceSW *self = (SpaceSW*)p_self;
  127. if (type_A==CollisionObjectSW::TYPE_AREA) {
  128. ERR_FAIL_COND_V(type_B!=CollisionObjectSW::TYPE_BODY,NULL);
  129. AreaSW *area=static_cast<AreaSW*>(A);
  130. BodySW *body=static_cast<BodySW*>(B);
  131. AreaPairSW *area_pair = memnew(AreaPairSW(body,p_subindex_B,area,p_subindex_A) );
  132. return area_pair;
  133. } else {
  134. BodyPairSW *b = memnew( BodyPairSW((BodySW*)A,p_subindex_A,(BodySW*)B,p_subindex_B) );
  135. return b;
  136. }
  137. return NULL;
  138. }
  139. void SpaceSW::_broadphase_unpair(CollisionObjectSW *A,int p_subindex_A,CollisionObjectSW *B,int p_subindex_B,void *p_data,void *p_self) {
  140. SpaceSW *self = (SpaceSW*)p_self;
  141. ConstraintSW *c = (ConstraintSW*)p_data;
  142. memdelete(c);
  143. }
  144. const SelfList<BodySW>::List& SpaceSW::get_active_body_list() const {
  145. return active_list;
  146. }
  147. void SpaceSW::body_add_to_active_list(SelfList<BodySW>* p_body) {
  148. active_list.add(p_body);
  149. }
  150. void SpaceSW::body_remove_from_active_list(SelfList<BodySW>* p_body) {
  151. active_list.remove(p_body);
  152. }
  153. void SpaceSW::body_add_to_inertia_update_list(SelfList<BodySW>* p_body) {
  154. inertia_update_list.add(p_body);
  155. }
  156. void SpaceSW::body_remove_from_inertia_update_list(SelfList<BodySW>* p_body) {
  157. inertia_update_list.remove(p_body);
  158. }
  159. BroadPhaseSW *SpaceSW::get_broadphase() {
  160. return broadphase;
  161. }
  162. void SpaceSW::add_object(CollisionObjectSW *p_object) {
  163. ERR_FAIL_COND( objects.has(p_object) );
  164. objects.insert(p_object);
  165. }
  166. void SpaceSW::remove_object(CollisionObjectSW *p_object) {
  167. ERR_FAIL_COND( !objects.has(p_object) );
  168. objects.erase(p_object);
  169. }
  170. const Set<CollisionObjectSW*> &SpaceSW::get_objects() const {
  171. return objects;
  172. }
  173. void SpaceSW::body_add_to_state_query_list(SelfList<BodySW>* p_body) {
  174. state_query_list.add(p_body);
  175. }
  176. void SpaceSW::body_remove_from_state_query_list(SelfList<BodySW>* p_body) {
  177. state_query_list.remove(p_body);
  178. }
  179. void SpaceSW::area_add_to_monitor_query_list(SelfList<AreaSW>* p_area) {
  180. monitor_query_list.add(p_area);
  181. }
  182. void SpaceSW::area_remove_from_monitor_query_list(SelfList<AreaSW>* p_area) {
  183. monitor_query_list.remove(p_area);
  184. }
  185. void SpaceSW::area_add_to_moved_list(SelfList<AreaSW>* p_area) {
  186. area_moved_list.add(p_area);
  187. }
  188. void SpaceSW::area_remove_from_moved_list(SelfList<AreaSW>* p_area) {
  189. area_moved_list.remove(p_area);
  190. }
  191. const SelfList<AreaSW>::List& SpaceSW::get_moved_area_list() const {
  192. return area_moved_list;
  193. }
  194. void SpaceSW::call_queries() {
  195. while(state_query_list.first()) {
  196. BodySW * b = state_query_list.first()->self();
  197. b->call_queries();
  198. state_query_list.remove(state_query_list.first());
  199. }
  200. while(monitor_query_list.first()) {
  201. AreaSW * a = monitor_query_list.first()->self();
  202. a->call_queries();
  203. monitor_query_list.remove(monitor_query_list.first());
  204. }
  205. }
  206. void SpaceSW::setup() {
  207. while(inertia_update_list.first()) {
  208. inertia_update_list.first()->self()->update_inertias();
  209. inertia_update_list.remove(inertia_update_list.first());
  210. }
  211. }
  212. void SpaceSW::update() {
  213. broadphase->update();
  214. }
  215. void SpaceSW::set_param(PhysicsServer::SpaceParameter p_param, real_t p_value) {
  216. switch(p_param) {
  217. case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: contact_recycle_radius=p_value; break;
  218. case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: contact_max_separation=p_value; break;
  219. case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: contact_max_allowed_penetration=p_value; break;
  220. case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_TRESHOLD: body_linear_velocity_sleep_threshold=p_value; break;
  221. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_TRESHOLD: body_angular_velocity_sleep_threshold=p_value; break;
  222. case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: body_time_to_sleep=p_value; break;
  223. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO: body_angular_velocity_damp_ratio=p_value; break;
  224. case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: constraint_bias=p_value; break;
  225. }
  226. }
  227. real_t SpaceSW::get_param(PhysicsServer::SpaceParameter p_param) const {
  228. switch(p_param) {
  229. case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: return contact_recycle_radius;
  230. case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: return contact_max_separation;
  231. case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: return contact_max_allowed_penetration;
  232. case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_TRESHOLD: return body_linear_velocity_sleep_threshold;
  233. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_TRESHOLD: return body_angular_velocity_sleep_threshold;
  234. case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: return body_time_to_sleep;
  235. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO: return body_angular_velocity_damp_ratio;
  236. case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: return constraint_bias;
  237. }
  238. return 0;
  239. }
  240. void SpaceSW::lock() {
  241. locked=true;
  242. }
  243. void SpaceSW::unlock() {
  244. locked=false;
  245. }
  246. bool SpaceSW::is_locked() const {
  247. return locked;
  248. }
  249. PhysicsDirectSpaceStateSW *SpaceSW::get_direct_state() {
  250. return direct_access;
  251. }
  252. SpaceSW::SpaceSW() {
  253. locked=false;
  254. contact_recycle_radius=0.01;
  255. contact_max_separation=0.05;
  256. contact_max_allowed_penetration= 0.01;
  257. constraint_bias = 0.01;
  258. body_linear_velocity_sleep_threshold=GLOBAL_DEF("physics/sleep_threshold_linear",0.1);
  259. body_angular_velocity_sleep_threshold=GLOBAL_DEF("physics/sleep_threshold_angular", (8.0 / 180.0 * Math_PI) );
  260. body_time_to_sleep=0.5;
  261. body_angular_velocity_damp_ratio=10;
  262. broadphase = BroadPhaseSW::create_func();
  263. broadphase->set_pair_callback(_broadphase_pair,this);
  264. broadphase->set_unpair_callback(_broadphase_unpair,this);
  265. area=NULL;
  266. direct_access = memnew( PhysicsDirectSpaceStateSW );
  267. direct_access->space=this;
  268. }
  269. SpaceSW::~SpaceSW() {
  270. memdelete(broadphase);
  271. memdelete( direct_access );
  272. }