world_2d.cpp 11 KB

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