world_2d.cpp 5.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*************************************************************************/
  2. /* world_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/config/project_settings.h"
  32. #include "scene/2d/camera_2d.h"
  33. #include "scene/2d/visible_on_screen_notifier_2d.h"
  34. #include "scene/main/window.h"
  35. #include "servers/navigation_server_2d.h"
  36. #include "servers/physics_server_2d.h"
  37. #include "servers/rendering_server.h"
  38. RID World2D::get_canvas() const {
  39. return canvas;
  40. }
  41. RID World2D::get_space() const {
  42. return space;
  43. }
  44. RID World2D::get_navigation_map() const {
  45. return navigation_map;
  46. }
  47. void World2D::_bind_methods() {
  48. ClassDB::bind_method(D_METHOD("get_canvas"), &World2D::get_canvas);
  49. ClassDB::bind_method(D_METHOD("get_space"), &World2D::get_space);
  50. ClassDB::bind_method(D_METHOD("get_navigation_map"), &World2D::get_navigation_map);
  51. ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World2D::get_direct_space_state);
  52. ADD_PROPERTY(PropertyInfo(Variant::RID, "canvas", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_canvas");
  53. ADD_PROPERTY(PropertyInfo(Variant::RID, "space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_space");
  54. ADD_PROPERTY(PropertyInfo(Variant::RID, "navigation_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_navigation_map");
  55. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState2D", PROPERTY_USAGE_NONE), "", "get_direct_space_state");
  56. }
  57. PhysicsDirectSpaceState2D *World2D::get_direct_space_state() {
  58. return PhysicsServer2D::get_singleton()->space_get_direct_state(space);
  59. }
  60. World2D::World2D() {
  61. canvas = RenderingServer::get_singleton()->canvas_create();
  62. // Create and configure space2D to be more friendly with pixels than meters
  63. space = PhysicsServer2D::get_singleton()->space_create();
  64. PhysicsServer2D::get_singleton()->space_set_active(space, true);
  65. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY, GLOBAL_DEF_BASIC("physics/2d/default_gravity", 980.0));
  66. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_DEF_BASIC("physics/2d/default_gravity_vector", Vector2(0, 1)));
  67. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_LINEAR_DAMP, GLOBAL_DEF("physics/2d/default_linear_damp", 0.1));
  68. ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/default_linear_damp", PropertyInfo(Variant::FLOAT, "physics/2d/default_linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"));
  69. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF("physics/2d/default_angular_damp", 1.0));
  70. ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/default_angular_damp", PropertyInfo(Variant::FLOAT, "physics/2d/default_angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"));
  71. // Create and configure the navigation_map to be more friendly with pixels than meters.
  72. navigation_map = NavigationServer2D::get_singleton()->map_create();
  73. NavigationServer2D::get_singleton()->map_set_active(navigation_map, true);
  74. NavigationServer2D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_DEF("navigation/2d/default_cell_size", 1));
  75. NavigationServer2D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_DEF("navigation/2d/default_edge_connection_margin", 1));
  76. }
  77. World2D::~World2D() {
  78. RenderingServer::get_singleton()->free(canvas);
  79. PhysicsServer2D::get_singleton()->free(space);
  80. NavigationServer2D::get_singleton()->free(navigation_map);
  81. }