static_body_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**************************************************************************/
  2. /* static_body_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "static_body_2d.h"
  31. #ifndef NAVIGATION_2D_DISABLED
  32. #include "scene/resources/2d/capsule_shape_2d.h"
  33. #include "scene/resources/2d/circle_shape_2d.h"
  34. #include "scene/resources/2d/concave_polygon_shape_2d.h"
  35. #include "scene/resources/2d/convex_polygon_shape_2d.h"
  36. #include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h"
  37. #include "scene/resources/2d/navigation_polygon.h"
  38. #include "scene/resources/2d/rectangle_shape_2d.h"
  39. #include "servers/navigation_server_2d.h"
  40. #endif // NAVIGATION_2D_DISABLED
  41. Callable StaticBody2D::_navmesh_source_geometry_parsing_callback;
  42. RID StaticBody2D::_navmesh_source_geometry_parser;
  43. void StaticBody2D::set_constant_linear_velocity(const Vector2 &p_vel) {
  44. constant_linear_velocity = p_vel;
  45. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
  46. }
  47. void StaticBody2D::set_constant_angular_velocity(real_t p_vel) {
  48. constant_angular_velocity = p_vel;
  49. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
  50. }
  51. Vector2 StaticBody2D::get_constant_linear_velocity() const {
  52. return constant_linear_velocity;
  53. }
  54. real_t StaticBody2D::get_constant_angular_velocity() const {
  55. return constant_angular_velocity;
  56. }
  57. void StaticBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  58. if (physics_material_override.is_valid()) {
  59. physics_material_override->disconnect_changed(callable_mp(this, &StaticBody2D::_reload_physics_characteristics));
  60. }
  61. physics_material_override = p_physics_material_override;
  62. if (physics_material_override.is_valid()) {
  63. physics_material_override->connect_changed(callable_mp(this, &StaticBody2D::_reload_physics_characteristics));
  64. }
  65. _reload_physics_characteristics();
  66. }
  67. Ref<PhysicsMaterial> StaticBody2D::get_physics_material_override() const {
  68. return physics_material_override;
  69. }
  70. void StaticBody2D::_reload_physics_characteristics() {
  71. if (physics_material_override.is_null()) {
  72. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  73. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  74. } else {
  75. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  76. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  77. }
  78. }
  79. #ifndef NAVIGATION_2D_DISABLED
  80. void StaticBody2D::navmesh_parse_init() {
  81. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  82. if (!_navmesh_source_geometry_parser.is_valid()) {
  83. _navmesh_source_geometry_parsing_callback = callable_mp_static(&StaticBody2D::navmesh_parse_source_geometry);
  84. _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
  85. NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  86. }
  87. }
  88. void StaticBody2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
  89. StaticBody2D *static_body = Object::cast_to<StaticBody2D>(p_node);
  90. if (static_body == nullptr) {
  91. return;
  92. }
  93. NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  94. if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_STATIC_COLLIDERS || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {
  95. return;
  96. }
  97. uint32_t parsed_collision_mask = p_navigation_mesh->get_parsed_collision_mask();
  98. if (!(static_body->get_collision_layer() & parsed_collision_mask)) {
  99. return;
  100. }
  101. List<uint32_t> shape_owners;
  102. static_body->get_shape_owners(&shape_owners);
  103. for (uint32_t shape_owner : shape_owners) {
  104. if (static_body->is_shape_owner_disabled(shape_owner)) {
  105. continue;
  106. }
  107. const int shape_count = static_body->shape_owner_get_shape_count(shape_owner);
  108. for (int shape_index = 0; shape_index < shape_count; shape_index++) {
  109. Ref<Shape2D> s = static_body->shape_owner_get_shape(shape_owner, shape_index);
  110. if (s.is_null()) {
  111. continue;
  112. }
  113. const Transform2D static_body_xform = p_source_geometry_data->root_node_transform * static_body->get_global_transform() * static_body->shape_owner_get_transform(shape_owner);
  114. RectangleShape2D *rectangle_shape = Object::cast_to<RectangleShape2D>(*s);
  115. if (rectangle_shape) {
  116. Vector<Vector2> shape_outline;
  117. const Vector2 &rectangle_size = rectangle_shape->get_size();
  118. shape_outline.resize(5);
  119. shape_outline.write[0] = static_body_xform.xform(-rectangle_size * 0.5);
  120. shape_outline.write[1] = static_body_xform.xform(Vector2(rectangle_size.x, -rectangle_size.y) * 0.5);
  121. shape_outline.write[2] = static_body_xform.xform(rectangle_size * 0.5);
  122. shape_outline.write[3] = static_body_xform.xform(Vector2(-rectangle_size.x, rectangle_size.y) * 0.5);
  123. shape_outline.write[4] = static_body_xform.xform(-rectangle_size * 0.5);
  124. p_source_geometry_data->add_obstruction_outline(shape_outline);
  125. }
  126. CapsuleShape2D *capsule_shape = Object::cast_to<CapsuleShape2D>(*s);
  127. if (capsule_shape) {
  128. const real_t capsule_height = capsule_shape->get_height();
  129. const real_t capsule_radius = capsule_shape->get_radius();
  130. Vector<Vector2> shape_outline;
  131. const real_t turn_step = Math::TAU / 12.0;
  132. shape_outline.resize(14);
  133. int shape_outline_inx = 0;
  134. for (int i = 0; i < 12; i++) {
  135. Vector2 ofs = Vector2(0, (i > 3 && i <= 9) ? capsule_height * 0.5 - capsule_radius : -capsule_height * 0.5 + capsule_radius);
  136. shape_outline.write[shape_outline_inx] = static_body_xform.xform(Vector2(Math::sin(i * turn_step), -Math::cos(i * turn_step)) * capsule_radius + ofs);
  137. shape_outline_inx += 1;
  138. if (i == 3 || i == 9) {
  139. shape_outline.write[shape_outline_inx] = static_body_xform.xform(Vector2(Math::sin(i * turn_step), -Math::cos(i * turn_step)) * capsule_radius - ofs);
  140. shape_outline_inx += 1;
  141. }
  142. }
  143. p_source_geometry_data->add_obstruction_outline(shape_outline);
  144. }
  145. CircleShape2D *circle_shape = Object::cast_to<CircleShape2D>(*s);
  146. if (circle_shape) {
  147. const real_t circle_radius = circle_shape->get_radius();
  148. Vector<Vector2> shape_outline;
  149. int circle_edge_count = 12;
  150. shape_outline.resize(circle_edge_count);
  151. const real_t turn_step = Math::TAU / real_t(circle_edge_count);
  152. for (int i = 0; i < circle_edge_count; i++) {
  153. shape_outline.write[i] = static_body_xform.xform(Vector2(Math::cos(i * turn_step), Math::sin(i * turn_step)) * circle_radius);
  154. }
  155. p_source_geometry_data->add_obstruction_outline(shape_outline);
  156. }
  157. ConcavePolygonShape2D *concave_polygon_shape = Object::cast_to<ConcavePolygonShape2D>(*s);
  158. if (concave_polygon_shape) {
  159. Vector<Vector2> shape_outline = concave_polygon_shape->get_segments();
  160. for (int i = 0; i < shape_outline.size(); i++) {
  161. shape_outline.write[i] = static_body_xform.xform(shape_outline[i]);
  162. }
  163. p_source_geometry_data->add_obstruction_outline(shape_outline);
  164. }
  165. ConvexPolygonShape2D *convex_polygon_shape = Object::cast_to<ConvexPolygonShape2D>(*s);
  166. if (convex_polygon_shape) {
  167. Vector<Vector2> shape_outline = convex_polygon_shape->get_points();
  168. for (int i = 0; i < shape_outline.size(); i++) {
  169. shape_outline.write[i] = static_body_xform.xform(shape_outline[i]);
  170. }
  171. p_source_geometry_data->add_obstruction_outline(shape_outline);
  172. }
  173. }
  174. }
  175. }
  176. #endif // NAVIGATION_2D_DISABLED
  177. void StaticBody2D::_bind_methods() {
  178. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody2D::set_constant_linear_velocity);
  179. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody2D::set_constant_angular_velocity);
  180. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody2D::get_constant_linear_velocity);
  181. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody2D::get_constant_angular_velocity);
  182. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody2D::set_physics_material_override);
  183. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody2D::get_physics_material_override);
  184. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  185. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant_linear_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_constant_linear_velocity", "get_constant_linear_velocity");
  186. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "constant_angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_constant_angular_velocity", "get_constant_angular_velocity");
  187. }
  188. StaticBody2D::StaticBody2D(PhysicsServer2D::BodyMode p_mode) :
  189. PhysicsBody2D(p_mode) {
  190. }