world_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*************************************************************************/
  2. /* world_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "world_2d.h"
  31. #include "servers/physics_2d_server.h"
  32. #include "servers/visual_server.h"
  33. //#include "servers/spatial_sound_2d_server.h"
  34. #include "project_settings.h"
  35. #include "scene/2d/camera_2d.h"
  36. #include "scene/2d/visibility_notifier_2d.h"
  37. #include "scene/main/viewport.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.position;
  81. begin /= cell_size;
  82. Point2i end = p_rect.position + 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.position;
  173. begin /= cell_size;
  174. Point2i end = E->get().rect.position + E->get().rect.size;
  175. end /= cell_size;
  176. pass++;
  177. List<VisibilityNotifier2D *> added;
  178. List<VisibilityNotifier2D *> removed;
  179. int visible_cells = (end.x - begin.x) * (end.y - begin.y);
  180. if (visible_cells > 10000) {
  181. //well you zoomed out a lot, it's your problem. To avoid freezing in the for loops below, we'll manually check cell by cell
  182. for (Map<CellKey, CellData>::Element *F = cells.front(); F; F = F->next()) {
  183. const CellKey &ck = F->key();
  184. if (ck.x < begin.x || ck.x > end.x)
  185. continue;
  186. if (ck.y < begin.y || ck.y > end.y)
  187. continue;
  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. } else {
  200. //check cells in grid fashion
  201. for (int i = begin.x; i <= end.x; i++) {
  202. for (int j = begin.y; j <= end.y; j++) {
  203. CellKey ck;
  204. ck.x = i;
  205. ck.y = j;
  206. Map<CellKey, CellData>::Element *F = cells.find(ck);
  207. if (!F) {
  208. continue;
  209. }
  210. //notifiers in cell
  211. for (Map<VisibilityNotifier2D *, CellRef>::Element *G = F->get().notifiers.front(); G; G = G->next()) {
  212. Map<VisibilityNotifier2D *, uint64_t>::Element *H = E->get().notifiers.find(G->key());
  213. if (!H) {
  214. H = E->get().notifiers.insert(G->key(), pass);
  215. added.push_back(G->key());
  216. } else {
  217. H->get() = pass;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. for (Map<VisibilityNotifier2D *, uint64_t>::Element *F = E->get().notifiers.front(); F; F = F->next()) {
  224. if (F->get() != pass)
  225. removed.push_back(F->key());
  226. }
  227. while (!added.empty()) {
  228. added.front()->get()->_enter_viewport(E->key());
  229. added.pop_front();
  230. }
  231. while (!removed.empty()) {
  232. E->get().notifiers.erase(removed.front()->get());
  233. removed.front()->get()->_exit_viewport(E->key());
  234. removed.pop_front();
  235. }
  236. }
  237. changed = false;
  238. }
  239. SpatialIndexer2D() {
  240. pass = 0;
  241. changed = false;
  242. cell_size = 100; //should be configurable with GLOBAL_DEF("") i guess
  243. }
  244. };
  245. void World2D::_register_viewport(Viewport *p_viewport, const Rect2 &p_rect) {
  246. indexer->_add_viewport(p_viewport, p_rect);
  247. }
  248. void World2D::_update_viewport(Viewport *p_viewport, const Rect2 &p_rect) {
  249. indexer->_update_viewport(p_viewport, p_rect);
  250. }
  251. void World2D::_remove_viewport(Viewport *p_viewport) {
  252. indexer->_remove_viewport(p_viewport);
  253. }
  254. void World2D::_register_notifier(VisibilityNotifier2D *p_notifier, const Rect2 &p_rect) {
  255. indexer->_notifier_add(p_notifier, p_rect);
  256. }
  257. void World2D::_update_notifier(VisibilityNotifier2D *p_notifier, const Rect2 &p_rect) {
  258. indexer->_notifier_update(p_notifier, p_rect);
  259. }
  260. void World2D::_remove_notifier(VisibilityNotifier2D *p_notifier) {
  261. indexer->_notifier_remove(p_notifier);
  262. }
  263. void World2D::_update() {
  264. indexer->_update();
  265. }
  266. RID World2D::get_canvas() {
  267. return canvas;
  268. }
  269. RID World2D::get_space() {
  270. return space;
  271. }
  272. void World2D::get_viewport_list(List<Viewport *> *r_viewports) {
  273. for (Map<Viewport *, SpatialIndexer2D::ViewportData>::Element *E = indexer->viewports.front(); E; E = E->next()) {
  274. r_viewports->push_back(E->key());
  275. }
  276. }
  277. void World2D::_bind_methods() {
  278. ClassDB::bind_method(D_METHOD("get_canvas"), &World2D::get_canvas);
  279. ClassDB::bind_method(D_METHOD("get_space"), &World2D::get_space);
  280. ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World2D::get_direct_space_state);
  281. }
  282. Physics2DDirectSpaceState *World2D::get_direct_space_state() {
  283. return Physics2DServer::get_singleton()->space_get_direct_state(space);
  284. }
  285. World2D::World2D() {
  286. canvas = VisualServer::get_singleton()->canvas_create();
  287. space = Physics2DServer::get_singleton()->space_create();
  288. //set space2D to be more friendly with pixels than meters, by adjusting some constants
  289. Physics2DServer::get_singleton()->space_set_active(space, true);
  290. Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_GRAVITY, GLOBAL_DEF("physics/2d/default_gravity", 98));
  291. Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_DEF("physics/2d/default_gravity_vector", Vector2(0, 1)));
  292. Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_LINEAR_DAMP, GLOBAL_DEF("physics/2d/default_linear_damp", 0.1));
  293. Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF("physics/2d/default_angular_damp", 1));
  294. indexer = memnew(SpatialIndexer2D);
  295. }
  296. World2D::~World2D() {
  297. VisualServer::get_singleton()->free(canvas);
  298. Physics2DServer::get_singleton()->free(space);
  299. memdelete(indexer);
  300. }