collision_shape_2d.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*************************************************************************/
  2. /* collision_shape_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "collision_shape_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "engine.h"
  33. #include "scene/resources/capsule_shape_2d.h"
  34. #include "scene/resources/circle_shape_2d.h"
  35. #include "scene/resources/concave_polygon_shape_2d.h"
  36. #include "scene/resources/convex_polygon_shape_2d.h"
  37. #include "scene/resources/rectangle_shape_2d.h"
  38. #include "scene/resources/segment_shape_2d.h"
  39. #include "scene/resources/shape_line_2d.h"
  40. void CollisionShape2D::_shape_changed() {
  41. update();
  42. }
  43. void CollisionShape2D::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_PARENTED: {
  46. parent = Object::cast_to<CollisionObject2D>(get_parent());
  47. if (parent) {
  48. owner_id = parent->create_shape_owner(this);
  49. if (shape.is_valid()) {
  50. parent->shape_owner_add_shape(owner_id, shape);
  51. }
  52. parent->shape_owner_set_transform(owner_id, get_transform());
  53. parent->shape_owner_set_disabled(owner_id, disabled);
  54. parent->shape_owner_set_one_way_collision(owner_id, one_way_collision);
  55. }
  56. /*if (Engine::get_singleton()->is_editor_hint()) {
  57. //display above all else
  58. set_z_as_relative(false);
  59. set_z(VS::CANVAS_ITEM_Z_MAX - 1);
  60. }*/
  61. } break;
  62. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  63. if (parent) {
  64. parent->shape_owner_set_transform(owner_id, get_transform());
  65. }
  66. } break;
  67. case NOTIFICATION_UNPARENTED: {
  68. if (parent) {
  69. parent->remove_shape_owner(owner_id);
  70. }
  71. owner_id = 0;
  72. parent = NULL;
  73. } break;
  74. /*
  75. case NOTIFICATION_TRANSFORM_CHANGED: {
  76. if (!is_inside_scene())
  77. break;
  78. _update_parent();
  79. } break;*/
  80. case NOTIFICATION_DRAW: {
  81. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  82. break;
  83. }
  84. if (!shape.is_valid()) {
  85. break;
  86. }
  87. rect = Rect2();
  88. Color draw_col = get_tree()->get_debug_collisions_color();
  89. if (disabled) {
  90. float g = draw_col.gray();
  91. draw_col.r = g;
  92. draw_col.g = g;
  93. draw_col.b = g;
  94. }
  95. shape->draw(get_canvas_item(), draw_col);
  96. rect = shape->get_rect();
  97. rect = rect.grow(3);
  98. if (one_way_collision) {
  99. Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
  100. dcol.a = 1.0;
  101. Vector2 line_to(0, 20);
  102. draw_line(Vector2(), line_to, dcol, 3);
  103. Vector<Vector2> pts;
  104. float tsize = 8;
  105. pts.push_back(line_to + (Vector2(0, tsize)));
  106. pts.push_back(line_to + (Vector2(0.707 * tsize, 0)));
  107. pts.push_back(line_to + (Vector2(-0.707 * tsize, 0)));
  108. Vector<Color> cols;
  109. for (int i = 0; i < 3; i++)
  110. cols.push_back(dcol);
  111. draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
  112. }
  113. } break;
  114. }
  115. }
  116. void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) {
  117. if (shape.is_valid())
  118. shape->disconnect("changed", this, "_shape_changed");
  119. shape = p_shape;
  120. update();
  121. if (parent) {
  122. parent->shape_owner_clear_shapes(owner_id);
  123. if (shape.is_valid()) {
  124. parent->shape_owner_add_shape(owner_id, shape);
  125. }
  126. }
  127. if (shape.is_valid())
  128. shape->connect("changed", this, "_shape_changed");
  129. update_configuration_warning();
  130. }
  131. Ref<Shape2D> CollisionShape2D::get_shape() const {
  132. return shape;
  133. }
  134. Rect2 CollisionShape2D::get_item_rect() const {
  135. return rect;
  136. }
  137. String CollisionShape2D::get_configuration_warning() const {
  138. if (!Object::cast_to<CollisionObject2D>(get_parent())) {
  139. return TTR("CollisionShape2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, KinematicBody2D, etc. to give them a shape.");
  140. }
  141. if (!shape.is_valid()) {
  142. return TTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!");
  143. }
  144. return String();
  145. }
  146. void CollisionShape2D::set_disabled(bool p_disabled) {
  147. disabled = p_disabled;
  148. update();
  149. if (parent) {
  150. parent->shape_owner_set_disabled(owner_id, p_disabled);
  151. }
  152. }
  153. bool CollisionShape2D::is_disabled() const {
  154. return disabled;
  155. }
  156. void CollisionShape2D::set_one_way_collision(bool p_enable) {
  157. one_way_collision = p_enable;
  158. update();
  159. if (parent) {
  160. parent->shape_owner_set_one_way_collision(owner_id, p_enable);
  161. }
  162. }
  163. bool CollisionShape2D::is_one_way_collision_enabled() const {
  164. return one_way_collision;
  165. }
  166. void CollisionShape2D::_bind_methods() {
  167. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
  168. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
  169. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionShape2D::set_disabled);
  170. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape2D::is_disabled);
  171. ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionShape2D::set_one_way_collision);
  172. ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
  173. ClassDB::bind_method(D_METHOD("_shape_changed"), &CollisionShape2D::_shape_changed);
  174. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  175. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  176. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
  177. }
  178. CollisionShape2D::CollisionShape2D() {
  179. rect = Rect2(-Point2(10, 10), Point2(20, 20));
  180. set_notify_local_transform(true);
  181. owner_id = 0;
  182. parent = NULL;
  183. disabled = false;
  184. one_way_collision = false;
  185. }