static_body_2d.cpp 11 KB

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