world_boundary_shape_2d.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**************************************************************************/
  2. /* world_boundary_shape_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 "world_boundary_shape_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "servers/physics_server_2d.h"
  33. #include "servers/rendering_server.h"
  34. bool WorldBoundaryShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  35. const Vector2 shape_center = distance * normal;
  36. // Orthogonal part of the shape editor gizmo (the flat line).
  37. const Vector2 ortho_segment_a = shape_center - normal.orthogonal() * 100;
  38. const Vector2 ortho_segment_b = shape_center + normal.orthogonal() * 100;
  39. const Vector2 ortho_closest = Geometry2D::get_closest_point_to_segment(p_point, ortho_segment_a, ortho_segment_b);
  40. if (p_point.distance_to(ortho_closest) < p_tolerance) {
  41. return true;
  42. }
  43. // Normal part of the shape editor gizmo (the arrow).
  44. const Vector2 normal_segment_a = shape_center;
  45. const Vector2 normal_segment_b = shape_center + normal * 30;
  46. const Vector2 normal_closest = Geometry2D::get_closest_point_to_segment(p_point, normal_segment_a, normal_segment_b);
  47. if (p_point.distance_to(normal_closest) < p_tolerance) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. void WorldBoundaryShape2D::_update_shape() {
  53. Array arr = { normal, distance };
  54. PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), arr);
  55. emit_changed();
  56. }
  57. void WorldBoundaryShape2D::set_normal(const Vector2 &p_normal) {
  58. // Can be non-unit but prevent zero.
  59. ERR_FAIL_COND(p_normal.is_zero_approx());
  60. if (normal == p_normal) {
  61. return;
  62. }
  63. normal = p_normal;
  64. _update_shape();
  65. }
  66. void WorldBoundaryShape2D::set_distance(real_t p_distance) {
  67. if (distance == p_distance) {
  68. return;
  69. }
  70. distance = p_distance;
  71. _update_shape();
  72. }
  73. Vector2 WorldBoundaryShape2D::get_normal() const {
  74. return normal;
  75. }
  76. real_t WorldBoundaryShape2D::get_distance() const {
  77. return distance;
  78. }
  79. void WorldBoundaryShape2D::draw(const RID &p_to_rid, const Color &p_color) {
  80. Vector2 point = distance * normal;
  81. real_t line_width = 3.0;
  82. // Draw collision shape line.
  83. PackedVector2Array line_points = {
  84. point - normal.orthogonal() * 100,
  85. point - normal.orthogonal() * 60,
  86. point + normal.orthogonal() * 60,
  87. point + normal.orthogonal() * 100
  88. };
  89. Color transparent_color = Color(p_color, 0);
  90. PackedColorArray line_colors = {
  91. transparent_color,
  92. p_color,
  93. p_color,
  94. transparent_color
  95. };
  96. RS::get_singleton()->canvas_item_add_polyline(p_to_rid, line_points, line_colors, line_width);
  97. // Draw arrow.
  98. Color arrow_color = p_color.inverted();
  99. Transform2D xf;
  100. xf.rotate(normal.angle());
  101. Vector<Vector2> arrow_points = {
  102. xf.xform(Vector2(distance + line_width / 2, -2.5)),
  103. xf.xform(Vector2(distance + 20, -2.5)),
  104. xf.xform(Vector2(distance + 20, -10)),
  105. xf.xform(Vector2(distance + 40, 0)),
  106. xf.xform(Vector2(distance + 20, 10)),
  107. xf.xform(Vector2(distance + 20, 2.5)),
  108. xf.xform(Vector2(distance + line_width / 2, 2.5)),
  109. };
  110. RS::get_singleton()->canvas_item_add_polyline(p_to_rid, arrow_points, { arrow_color }, line_width / 2);
  111. }
  112. Rect2 WorldBoundaryShape2D::get_rect() const {
  113. Vector2 point = distance * normal;
  114. Vector2 l1[2] = { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 };
  115. Vector2 l2[2] = { point, point + normal * 30 };
  116. Rect2 rect;
  117. rect.position = l1[0];
  118. rect.expand_to(l1[1]);
  119. rect.expand_to(l2[0]);
  120. rect.expand_to(l2[1]);
  121. return rect;
  122. }
  123. real_t WorldBoundaryShape2D::get_enclosing_radius() const {
  124. return distance;
  125. }
  126. void WorldBoundaryShape2D::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_normal", "normal"), &WorldBoundaryShape2D::set_normal);
  128. ClassDB::bind_method(D_METHOD("get_normal"), &WorldBoundaryShape2D::get_normal);
  129. ClassDB::bind_method(D_METHOD("set_distance", "distance"), &WorldBoundaryShape2D::set_distance);
  130. ClassDB::bind_method(D_METHOD("get_distance"), &WorldBoundaryShape2D::get_distance);
  131. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "set_normal", "get_normal");
  132. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01,or_greater,or_less,suffix:px"), "set_distance", "get_distance");
  133. }
  134. WorldBoundaryShape2D::WorldBoundaryShape2D() :
  135. Shape2D(PhysicsServer2D::get_singleton()->world_boundary_shape_create()) {
  136. _update_shape();
  137. }