world_boundary_shape_2d.cpp 5.8 KB

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