world_2d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*************************************************************************/
  2. /* world_2d.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 "world_2d.h"
  30. #include "servers/visual_server.h"
  31. #include "servers/physics_2d_server.h"
  32. #include "servers/spatial_sound_2d_server.h"
  33. #include "globals.h"
  34. #include "scene/2d/visibility_notifier_2d.h"
  35. #include "scene/main/viewport.h"
  36. #include "scene/2d/camera_2d.h"
  37. #include "globals.h"
  38. struct SpatialIndexer2D {
  39. struct CellRef {
  40. int ref;
  41. _FORCE_INLINE_ int inc() {
  42. ref++;
  43. return ref;
  44. }
  45. _FORCE_INLINE_ int dec() {
  46. ref--;
  47. return ref;
  48. }
  49. _FORCE_INLINE_ CellRef() {
  50. ref=0;
  51. }
  52. };
  53. struct CellKey {
  54. union {
  55. struct {
  56. int32_t x;
  57. int32_t y;
  58. };
  59. uint64_t key;
  60. };
  61. bool operator==(const CellKey& p_key) const { return key==p_key.key; }
  62. _FORCE_INLINE_ bool operator<(const CellKey& p_key) const {
  63. return key < p_key.key;
  64. }
  65. };
  66. struct CellData {
  67. Map<VisibilityNotifier2D*,CellRef> notifiers;
  68. };
  69. Map<CellKey,CellData> cells;
  70. int cell_size;
  71. Map<VisibilityNotifier2D*,Rect2> notifiers;
  72. struct ViewportData {
  73. Map<VisibilityNotifier2D*,uint64_t> notifiers;
  74. Rect2 rect;
  75. };
  76. Map<Viewport*,ViewportData> viewports;
  77. bool changed;
  78. uint64_t pass;
  79. void _notifier_update_cells(VisibilityNotifier2D *p_notifier,const Rect2& p_rect,bool p_add) {
  80. Point2i begin = p_rect.pos;
  81. begin/=cell_size;
  82. Point2i end = p_rect.pos+p_rect.size;
  83. end/=cell_size;
  84. for(int i=begin.x;i<=end.x;i++) {
  85. for(int j=begin.y;j<=end.y;j++) {
  86. CellKey ck;
  87. ck.x=i;
  88. ck.y=j;
  89. Map<CellKey,CellData>::Element *E=cells.find(ck);
  90. if (p_add) {
  91. if (!E)
  92. E=cells.insert(ck,CellData());
  93. E->get().notifiers[p_notifier].inc();
  94. } else {
  95. ERR_CONTINUE(!E);
  96. if (E->get().notifiers[p_notifier].dec()==0) {
  97. E->get().notifiers.erase(p_notifier);
  98. if (E->get().notifiers.empty()) {
  99. cells.erase(E);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. void _notifier_add(VisibilityNotifier2D* p_notifier,const Rect2& p_rect) {
  107. ERR_FAIL_COND(notifiers.has(p_notifier));
  108. notifiers[p_notifier]=p_rect;
  109. _notifier_update_cells(p_notifier,p_rect,true);
  110. changed=true;
  111. }
  112. void _notifier_update(VisibilityNotifier2D* p_notifier,const Rect2& p_rect) {
  113. Map<VisibilityNotifier2D*,Rect2>::Element *E=notifiers.find(p_notifier);
  114. ERR_FAIL_COND(!E);
  115. if (E->get()==p_rect)
  116. return;
  117. _notifier_update_cells(p_notifier,p_rect,true);
  118. _notifier_update_cells(p_notifier,E->get(),false);
  119. E->get()=p_rect;
  120. changed=true;
  121. }
  122. void _notifier_remove(VisibilityNotifier2D* p_notifier) {
  123. Map<VisibilityNotifier2D*,Rect2>::Element *E=notifiers.find(p_notifier);
  124. ERR_FAIL_COND(!E);
  125. _notifier_update_cells(p_notifier,E->get(),false);
  126. notifiers.erase(p_notifier);
  127. List<Viewport*> removed;
  128. for (Map<Viewport*,ViewportData>::Element*F=viewports.front();F;F=F->next()) {
  129. Map<VisibilityNotifier2D*,uint64_t>::Element*G=F->get().notifiers.find(p_notifier);
  130. if (G) {
  131. F->get().notifiers.erase(G);
  132. removed.push_back(F->key());
  133. }
  134. }
  135. while(!removed.empty()) {
  136. p_notifier->_exit_viewport(removed.front()->get());
  137. removed.pop_front();
  138. }
  139. changed=true;
  140. }
  141. void _add_viewport(Viewport* p_viewport,const Rect2& p_rect) {
  142. ERR_FAIL_COND(viewports.has(p_viewport));
  143. ViewportData vd;
  144. vd.rect=p_rect;
  145. viewports[p_viewport]=vd;
  146. changed=true;
  147. }
  148. void _update_viewport(Viewport* p_viewport, const Rect2& p_rect) {
  149. Map<Viewport*,ViewportData>::Element *E= viewports.find(p_viewport);
  150. ERR_FAIL_COND(!E);
  151. if (E->get().rect==p_rect)
  152. return;
  153. E->get().rect=p_rect;
  154. changed=true;
  155. }
  156. void _remove_viewport(Viewport* p_viewport) {
  157. ERR_FAIL_COND(!viewports.has(p_viewport));
  158. List<VisibilityNotifier2D*> removed;
  159. for(Map<VisibilityNotifier2D*,uint64_t>::Element *E=viewports[p_viewport].notifiers.front();E;E=E->next()) {
  160. removed.push_back(E->key());
  161. }
  162. while(!removed.empty()) {
  163. removed.front()->get()->_exit_viewport(p_viewport);
  164. removed.pop_front();
  165. }
  166. viewports.erase(p_viewport);
  167. }
  168. void _update() {
  169. if (!changed)
  170. return;
  171. for (Map<Viewport*,ViewportData>::Element *E=viewports.front();E;E=E->next()) {
  172. Point2i begin = E->get().rect.pos;
  173. begin/=cell_size;
  174. Point2i end = E->get().rect.pos+E->get().rect.size;
  175. end/=cell_size;
  176. pass++;
  177. List<VisibilityNotifier2D*> added;
  178. List<VisibilityNotifier2D*> removed;
  179. for(int i=begin.x;i<=end.x;i++) {
  180. for(int j=begin.y;j<=end.y;j++) {
  181. CellKey ck;
  182. ck.x=i;
  183. ck.y=j;
  184. Map<CellKey,CellData>::Element *F=cells.find(ck);
  185. if (!F) {
  186. continue;
  187. }
  188. //notifiers in cell
  189. for (Map<VisibilityNotifier2D*,CellRef>::Element *G=F->get().notifiers.front();G;G=G->next()) {
  190. Map<VisibilityNotifier2D*,uint64_t>::Element *H=E->get().notifiers.find(G->key());
  191. if (!H) {
  192. H=E->get().notifiers.insert(G->key(),pass);
  193. added.push_back(G->key());
  194. } else {
  195. H->get()=pass;
  196. }
  197. }
  198. }
  199. }
  200. for (Map<VisibilityNotifier2D*,uint64_t>::Element *F=E->get().notifiers.front();F;F=F->next()) {
  201. if (F->get()!=pass)
  202. removed.push_back(F->key());
  203. }
  204. while(!added.empty()) {
  205. added.front()->get()->_enter_viewport(E->key());
  206. added.pop_front();
  207. }
  208. while(!removed.empty()) {
  209. E->get().notifiers.erase(removed.front()->get());
  210. removed.front()->get()->_exit_viewport(E->key());
  211. removed.pop_front();
  212. }
  213. }
  214. changed=false;
  215. }
  216. SpatialIndexer2D() {
  217. pass=0;
  218. changed=false;
  219. cell_size=100; //should be configurable with GLOBAL_DEF("") i guess
  220. }
  221. };
  222. void World2D::_register_viewport(Viewport* p_viewport,const Rect2& p_rect) {
  223. indexer->_add_viewport(p_viewport,p_rect);
  224. }
  225. void World2D::_update_viewport(Viewport* p_viewport,const Rect2& p_rect){
  226. indexer->_update_viewport(p_viewport,p_rect);
  227. }
  228. void World2D::_remove_viewport(Viewport* p_viewport){
  229. indexer->_remove_viewport(p_viewport);
  230. }
  231. void World2D::_register_notifier(VisibilityNotifier2D* p_notifier, const Rect2 &p_rect){
  232. indexer->_notifier_add(p_notifier,p_rect);
  233. }
  234. void World2D::_update_notifier(VisibilityNotifier2D* p_notifier,const Rect2& p_rect){
  235. indexer->_notifier_update(p_notifier,p_rect);
  236. }
  237. void World2D::_remove_notifier(VisibilityNotifier2D* p_notifier){
  238. indexer->_notifier_remove(p_notifier);
  239. }
  240. void World2D::_update() {
  241. indexer->_update();
  242. }
  243. RID World2D::get_canvas() {
  244. return canvas;
  245. }
  246. RID World2D::get_space() {
  247. return space;
  248. }
  249. RID World2D::get_sound_space() {
  250. return sound_space;
  251. }
  252. void World2D::_bind_methods() {
  253. ObjectTypeDB::bind_method(_MD("get_canvas"),&World2D::get_canvas);
  254. ObjectTypeDB::bind_method(_MD("get_space"),&World2D::get_space);
  255. ObjectTypeDB::bind_method(_MD("get_sound_space"),&World2D::get_sound_space);
  256. }
  257. World2D::World2D() {
  258. canvas = VisualServer::get_singleton()->canvas_create();
  259. space = Physics2DServer::get_singleton()->space_create();
  260. sound_space = SpatialSound2DServer::get_singleton()->space_create();
  261. //set space2D to be more friendly with pixels than meters, by adjusting some constants
  262. Physics2DServer::get_singleton()->space_set_active(space,true);
  263. Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_GRAVITY,GLOBAL_DEF("physics_2d/default_gravity",98));
  264. Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_GRAVITY_VECTOR,GLOBAL_DEF("physics_2d/default_gravity_vector",Vector2(0,1)));
  265. Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_DENSITY,GLOBAL_DEF("physics_2d/default_density",0.1));
  266. Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS,1.0);
  267. Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_CONTACT_MAX_SEPARATION,1.5);
  268. Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION,0.3);
  269. Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_TRESHOLD,2);
  270. Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO,20);
  271. Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,0.2);
  272. indexer = memnew( SpatialIndexer2D );
  273. }
  274. World2D::~World2D() {
  275. VisualServer::get_singleton()->free(canvas);
  276. Physics2DServer::get_singleton()->free(space);
  277. SpatialSound2DServer::get_singleton()->free(sound_space);
  278. memdelete(indexer);
  279. }