shape_cast_2d.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**************************************************************************/
  2. /* shape_cast_2d.h */
  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. #ifndef SHAPE_CAST_2D_H
  31. #define SHAPE_CAST_2D_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/resources/2d/shape_2d.h"
  34. #include "servers/physics_server_2d.h"
  35. class CollisionObject2D;
  36. class ShapeCast2D : public Node2D {
  37. GDCLASS(ShapeCast2D, Node2D);
  38. bool enabled = true;
  39. Ref<Shape2D> shape;
  40. RID shape_rid;
  41. Vector2 target_position = Vector2(0, 50);
  42. HashSet<RID> exclude;
  43. real_t margin = 0.0;
  44. uint32_t collision_mask = 1;
  45. bool exclude_parent_body = true;
  46. bool collide_with_areas = false;
  47. bool collide_with_bodies = true;
  48. // Result
  49. int max_results = 32;
  50. Vector<PhysicsDirectSpaceState2D::ShapeRestInfo> result;
  51. bool collided = false;
  52. real_t collision_safe_fraction = 1.0;
  53. real_t collision_unsafe_fraction = 1.0;
  54. void _shape_changed();
  55. protected:
  56. void _notification(int p_what);
  57. void _update_shapecast_state();
  58. static void _bind_methods();
  59. public:
  60. void set_collide_with_areas(bool p_clip);
  61. bool is_collide_with_areas_enabled() const;
  62. void set_collide_with_bodies(bool p_clip);
  63. bool is_collide_with_bodies_enabled() const;
  64. void set_enabled(bool p_enabled);
  65. bool is_enabled() const;
  66. void set_shape(const Ref<Shape2D> &p_shape);
  67. Ref<Shape2D> get_shape() const;
  68. void set_target_position(const Vector2 &p_point);
  69. Vector2 get_target_position() const;
  70. void set_margin(real_t p_margin);
  71. real_t get_margin() const;
  72. void set_max_results(int p_max_results);
  73. int get_max_results() const;
  74. void set_collision_mask(uint32_t p_mask);
  75. uint32_t get_collision_mask() const;
  76. void set_collision_mask_value(int p_layer_number, bool p_value);
  77. bool get_collision_mask_value(int p_layer_number) const;
  78. void set_exclude_parent_body(bool p_exclude_parent_body);
  79. bool get_exclude_parent_body() const;
  80. void force_shapecast_update();
  81. bool is_colliding() const;
  82. Array get_collision_result() const;
  83. int get_collision_count() const;
  84. Object *get_collider(int p_idx) const;
  85. RID get_collider_rid(int p_idx) const;
  86. int get_collider_shape(int p_idx) const;
  87. Vector2 get_collision_point(int p_idx) const;
  88. Vector2 get_collision_normal(int p_idx) const;
  89. real_t get_closest_collision_safe_fraction() const;
  90. real_t get_closest_collision_unsafe_fraction() const;
  91. void add_exception_rid(const RID &p_rid);
  92. void add_exception(const CollisionObject2D *p_node);
  93. void remove_exception_rid(const RID &p_rid);
  94. void remove_exception(const CollisionObject2D *p_node);
  95. void clear_exceptions();
  96. PackedStringArray get_configuration_warnings() const override;
  97. ShapeCast2D();
  98. };
  99. #endif // SHAPE_CAST_2D_H