collision_object_2d_sw.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*************************************************************************/
  2. /* collision_object_2d_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef COLLISION_OBJECT_2D_SW_H
  31. #define COLLISION_OBJECT_2D_SW_H
  32. #include "broad_phase_2d_sw.h"
  33. #include "core/self_list.h"
  34. #include "servers/physics_2d_server.h"
  35. #include "shape_2d_sw.h"
  36. class Space2DSW;
  37. class CollisionObject2DSW : public ShapeOwner2DSW {
  38. public:
  39. enum Type {
  40. TYPE_AREA,
  41. TYPE_BODY
  42. };
  43. private:
  44. Type type;
  45. RID self;
  46. ObjectID instance_id;
  47. ObjectID canvas_instance_id;
  48. bool pickable;
  49. struct Shape {
  50. Transform2D xform;
  51. Transform2D xform_inv;
  52. BroadPhase2DSW::ID bpid;
  53. Rect2 aabb_cache; //for rayqueries
  54. Shape2DSW *shape;
  55. Variant metadata;
  56. bool disabled;
  57. bool one_way_collision;
  58. float one_way_collision_margin;
  59. Shape() {
  60. disabled = false;
  61. one_way_collision = false;
  62. one_way_collision_margin = 0;
  63. }
  64. };
  65. Vector<Shape> shapes;
  66. Space2DSW *space;
  67. Transform2D transform;
  68. Transform2D inv_transform;
  69. uint32_t collision_mask;
  70. uint32_t collision_layer;
  71. bool _static;
  72. SelfList<CollisionObject2DSW> pending_shape_update_list;
  73. void _update_shapes();
  74. protected:
  75. void _update_shapes_with_motion(const Vector2 &p_motion);
  76. void _unregister_shapes();
  77. _FORCE_INLINE_ void _set_transform(const Transform2D &p_transform, bool p_update_shapes = true) {
  78. transform = p_transform;
  79. if (p_update_shapes) {
  80. _update_shapes();
  81. }
  82. }
  83. _FORCE_INLINE_ void _set_inv_transform(const Transform2D &p_transform) { inv_transform = p_transform; }
  84. void _set_static(bool p_static);
  85. virtual void _shapes_changed() = 0;
  86. void _set_space(Space2DSW *p_space);
  87. CollisionObject2DSW(Type p_type);
  88. public:
  89. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  90. _FORCE_INLINE_ RID get_self() const { return self; }
  91. _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
  92. _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
  93. _FORCE_INLINE_ void set_canvas_instance_id(const ObjectID &p_canvas_instance_id) { canvas_instance_id = p_canvas_instance_id; }
  94. _FORCE_INLINE_ ObjectID get_canvas_instance_id() const { return canvas_instance_id; }
  95. void _shape_changed();
  96. _FORCE_INLINE_ Type get_type() const { return type; }
  97. void add_shape(Shape2DSW *p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);
  98. void set_shape(int p_index, Shape2DSW *p_shape);
  99. void set_shape_transform(int p_index, const Transform2D &p_transform);
  100. void set_shape_metadata(int p_index, const Variant &p_metadata);
  101. _FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }
  102. _FORCE_INLINE_ bool is_shape_disabled(int p_index) const {
  103. CRASH_BAD_INDEX(p_index, shapes.size());
  104. return shapes[p_index].disabled;
  105. }
  106. _FORCE_INLINE_ Shape2DSW *get_shape(int p_index) const {
  107. CRASH_BAD_INDEX(p_index, shapes.size());
  108. return shapes[p_index].shape;
  109. }
  110. _FORCE_INLINE_ const Transform2D &get_shape_transform(int p_index) const {
  111. CRASH_BAD_INDEX(p_index, shapes.size());
  112. return shapes[p_index].xform;
  113. }
  114. _FORCE_INLINE_ const Transform2D &get_shape_inv_transform(int p_index) const {
  115. CRASH_BAD_INDEX(p_index, shapes.size());
  116. return shapes[p_index].xform_inv;
  117. }
  118. _FORCE_INLINE_ const Rect2 &get_shape_aabb(int p_index) const {
  119. CRASH_BAD_INDEX(p_index, shapes.size());
  120. return shapes[p_index].aabb_cache;
  121. }
  122. _FORCE_INLINE_ const Variant &get_shape_metadata(int p_index) const {
  123. CRASH_BAD_INDEX(p_index, shapes.size());
  124. return shapes[p_index].metadata;
  125. }
  126. _FORCE_INLINE_ Transform2D get_transform() const { return transform; }
  127. _FORCE_INLINE_ Transform2D get_inv_transform() const { return inv_transform; }
  128. _FORCE_INLINE_ Space2DSW *get_space() const { return space; }
  129. void set_shape_as_disabled(int p_idx, bool p_disabled);
  130. _FORCE_INLINE_ bool is_shape_set_as_disabled(int p_idx) const {
  131. CRASH_BAD_INDEX(p_idx, shapes.size());
  132. return shapes[p_idx].disabled;
  133. }
  134. _FORCE_INLINE_ void set_shape_as_one_way_collision(int p_idx, bool p_one_way_collision, float p_margin) {
  135. CRASH_BAD_INDEX(p_idx, shapes.size());
  136. shapes.write[p_idx].one_way_collision = p_one_way_collision;
  137. shapes.write[p_idx].one_way_collision_margin = p_margin;
  138. }
  139. _FORCE_INLINE_ bool is_shape_set_as_one_way_collision(int p_idx) const {
  140. CRASH_BAD_INDEX(p_idx, shapes.size());
  141. return shapes[p_idx].one_way_collision;
  142. }
  143. _FORCE_INLINE_ float get_shape_one_way_collision_margin(int p_idx) const {
  144. CRASH_BAD_INDEX(p_idx, shapes.size());
  145. return shapes[p_idx].one_way_collision_margin;
  146. }
  147. void set_collision_mask(uint32_t p_mask) {
  148. collision_mask = p_mask;
  149. _shape_changed();
  150. }
  151. _FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }
  152. void set_collision_layer(uint32_t p_layer) {
  153. collision_layer = p_layer;
  154. _shape_changed();
  155. }
  156. _FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }
  157. void remove_shape(Shape2DSW *p_shape);
  158. void remove_shape(int p_index);
  159. virtual void set_space(Space2DSW *p_space) = 0;
  160. _FORCE_INLINE_ bool is_static() const { return _static; }
  161. void set_pickable(bool p_pickable) { pickable = p_pickable; }
  162. _FORCE_INLINE_ bool is_pickable() const { return pickable; }
  163. _FORCE_INLINE_ bool test_collision_mask(CollisionObject2DSW *p_other) const {
  164. return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;
  165. }
  166. virtual ~CollisionObject2DSW() {}
  167. };
  168. #endif // COLLISION_OBJECT_2D_SW_H