ray_shape_2d.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*************************************************************************/
  2. /* ray_shape_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "ray_shape_2d.h"
  31. #include "servers/physics_server_2d.h"
  32. #include "servers/rendering_server.h"
  33. void RayShape2D::_update_shape() {
  34. Dictionary d;
  35. d["length"] = length;
  36. d["slips_on_slope"] = slips_on_slope;
  37. PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), d);
  38. emit_changed();
  39. }
  40. void RayShape2D::draw(const RID &p_to_rid, const Color &p_color) {
  41. const Vector2 target_position = Vector2(0, get_length());
  42. const float max_arrow_size = 6;
  43. const float line_width = 1.4;
  44. bool no_line = target_position.length() < line_width;
  45. float arrow_size = CLAMP(target_position.length() * 2 / 3, line_width, max_arrow_size);
  46. if (no_line) {
  47. arrow_size = target_position.length();
  48. } else {
  49. RS::get_singleton()->canvas_item_add_line(p_to_rid, Vector2(), target_position - target_position.normalized() * arrow_size, p_color, line_width);
  50. }
  51. Transform2D xf;
  52. xf.rotate(target_position.angle());
  53. xf.translate(Vector2(no_line ? 0 : target_position.length() - arrow_size, 0));
  54. Vector<Vector2> pts;
  55. pts.push_back(xf.xform(Vector2(arrow_size, 0)));
  56. pts.push_back(xf.xform(Vector2(0, 0.5 * arrow_size)));
  57. pts.push_back(xf.xform(Vector2(0, -0.5 * arrow_size)));
  58. Vector<Color> cols;
  59. for (int i = 0; i < 3; i++) {
  60. cols.push_back(p_color);
  61. }
  62. RS::get_singleton()->canvas_item_add_primitive(p_to_rid, pts, cols, Vector<Point2>(), RID());
  63. }
  64. Rect2 RayShape2D::get_rect() const {
  65. Rect2 rect;
  66. rect.position = Vector2();
  67. rect.expand_to(Vector2(0, length));
  68. rect = rect.grow(Math_SQRT12 * 4);
  69. return rect;
  70. }
  71. real_t RayShape2D::get_enclosing_radius() const {
  72. return length;
  73. }
  74. void RayShape2D::_bind_methods() {
  75. ClassDB::bind_method(D_METHOD("set_length", "length"), &RayShape2D::set_length);
  76. ClassDB::bind_method(D_METHOD("get_length"), &RayShape2D::get_length);
  77. ClassDB::bind_method(D_METHOD("set_slips_on_slope", "active"), &RayShape2D::set_slips_on_slope);
  78. ClassDB::bind_method(D_METHOD("get_slips_on_slope"), &RayShape2D::get_slips_on_slope);
  79. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length"), "set_length", "get_length");
  80. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slips_on_slope"), "set_slips_on_slope", "get_slips_on_slope");
  81. }
  82. void RayShape2D::set_length(real_t p_length) {
  83. length = p_length;
  84. _update_shape();
  85. }
  86. real_t RayShape2D::get_length() const {
  87. return length;
  88. }
  89. void RayShape2D::set_slips_on_slope(bool p_active) {
  90. slips_on_slope = p_active;
  91. _update_shape();
  92. }
  93. bool RayShape2D::get_slips_on_slope() const {
  94. return slips_on_slope;
  95. }
  96. RayShape2D::RayShape2D() :
  97. Shape2D(PhysicsServer2D::get_singleton()->ray_shape_create()) {
  98. _update_shape();
  99. }