godot_collision_object_2d.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*************************************************************************/
  2. /* godot_collision_object_2d.h */
  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. #ifndef GODOT_COLLISION_OBJECT_2D_H
  31. #define GODOT_COLLISION_OBJECT_2D_H
  32. #include "godot_broad_phase_2d.h"
  33. #include "godot_shape_2d.h"
  34. #include "core/templates/self_list.h"
  35. #include "servers/physics_server_2d.h"
  36. class GodotSpace2D;
  37. class GodotCollisionObject2D : public GodotShapeOwner2D {
  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 = true;
  49. struct Shape {
  50. Transform2D xform;
  51. Transform2D xform_inv;
  52. GodotBroadPhase2D::ID bpid = 0;
  53. Rect2 aabb_cache; //for rayqueries
  54. GodotShape2D *shape = nullptr;
  55. bool disabled = false;
  56. bool one_way_collision = false;
  57. real_t one_way_collision_margin = 0.0;
  58. };
  59. Vector<Shape> shapes;
  60. GodotSpace2D *space = nullptr;
  61. Transform2D transform;
  62. Transform2D inv_transform;
  63. uint32_t collision_mask = 1;
  64. uint32_t collision_layer = 1;
  65. bool _static = true;
  66. SelfList<GodotCollisionObject2D> pending_shape_update_list;
  67. void _update_shapes();
  68. protected:
  69. void _update_shapes_with_motion(const Vector2 &p_motion);
  70. void _unregister_shapes();
  71. _FORCE_INLINE_ void _set_transform(const Transform2D &p_transform, bool p_update_shapes = true) {
  72. transform = p_transform;
  73. if (p_update_shapes) {
  74. _update_shapes();
  75. }
  76. }
  77. _FORCE_INLINE_ void _set_inv_transform(const Transform2D &p_transform) { inv_transform = p_transform; }
  78. void _set_static(bool p_static);
  79. virtual void _shapes_changed() = 0;
  80. void _set_space(GodotSpace2D *p_space);
  81. GodotCollisionObject2D(Type p_type);
  82. public:
  83. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  84. _FORCE_INLINE_ RID get_self() const { return self; }
  85. _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
  86. _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
  87. _FORCE_INLINE_ void set_canvas_instance_id(const ObjectID &p_canvas_instance_id) { canvas_instance_id = p_canvas_instance_id; }
  88. _FORCE_INLINE_ ObjectID get_canvas_instance_id() const { return canvas_instance_id; }
  89. void _shape_changed();
  90. _FORCE_INLINE_ Type get_type() const { return type; }
  91. void add_shape(GodotShape2D *p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);
  92. void set_shape(int p_index, GodotShape2D *p_shape);
  93. void set_shape_transform(int p_index, const Transform2D &p_transform);
  94. _FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }
  95. _FORCE_INLINE_ GodotShape2D *get_shape(int p_index) const {
  96. CRASH_BAD_INDEX(p_index, shapes.size());
  97. return shapes[p_index].shape;
  98. }
  99. _FORCE_INLINE_ const Transform2D &get_shape_transform(int p_index) const {
  100. CRASH_BAD_INDEX(p_index, shapes.size());
  101. return shapes[p_index].xform;
  102. }
  103. _FORCE_INLINE_ const Transform2D &get_shape_inv_transform(int p_index) const {
  104. CRASH_BAD_INDEX(p_index, shapes.size());
  105. return shapes[p_index].xform_inv;
  106. }
  107. _FORCE_INLINE_ const Rect2 &get_shape_aabb(int p_index) const {
  108. CRASH_BAD_INDEX(p_index, shapes.size());
  109. return shapes[p_index].aabb_cache;
  110. }
  111. _FORCE_INLINE_ const Transform2D &get_transform() const { return transform; }
  112. _FORCE_INLINE_ const Transform2D &get_inv_transform() const { return inv_transform; }
  113. _FORCE_INLINE_ GodotSpace2D *get_space() const { return space; }
  114. void set_shape_disabled(int p_idx, bool p_disabled);
  115. _FORCE_INLINE_ bool is_shape_disabled(int p_idx) const {
  116. ERR_FAIL_INDEX_V(p_idx, shapes.size(), false);
  117. return shapes[p_idx].disabled;
  118. }
  119. _FORCE_INLINE_ void set_shape_as_one_way_collision(int p_idx, bool p_one_way_collision, real_t p_margin) {
  120. CRASH_BAD_INDEX(p_idx, shapes.size());
  121. shapes.write[p_idx].one_way_collision = p_one_way_collision;
  122. shapes.write[p_idx].one_way_collision_margin = p_margin;
  123. }
  124. _FORCE_INLINE_ bool is_shape_set_as_one_way_collision(int p_idx) const {
  125. CRASH_BAD_INDEX(p_idx, shapes.size());
  126. return shapes[p_idx].one_way_collision;
  127. }
  128. _FORCE_INLINE_ real_t get_shape_one_way_collision_margin(int p_idx) const {
  129. CRASH_BAD_INDEX(p_idx, shapes.size());
  130. return shapes[p_idx].one_way_collision_margin;
  131. }
  132. void set_collision_mask(uint32_t p_mask) {
  133. collision_mask = p_mask;
  134. _shape_changed();
  135. }
  136. _FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }
  137. void set_collision_layer(uint32_t p_layer) {
  138. collision_layer = p_layer;
  139. _shape_changed();
  140. }
  141. _FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }
  142. void remove_shape(GodotShape2D *p_shape);
  143. void remove_shape(int p_index);
  144. virtual void set_space(GodotSpace2D *p_space) = 0;
  145. _FORCE_INLINE_ bool is_static() const { return _static; }
  146. void set_pickable(bool p_pickable) { pickable = p_pickable; }
  147. _FORCE_INLINE_ bool is_pickable() const { return pickable; }
  148. _FORCE_INLINE_ bool collides_with(GodotCollisionObject2D *p_other) const {
  149. return p_other->collision_layer & collision_mask;
  150. }
  151. _FORCE_INLINE_ bool interacts_with(GodotCollisionObject2D *p_other) const {
  152. return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;
  153. }
  154. virtual ~GodotCollisionObject2D() {}
  155. };
  156. #endif // GODOT_COLLISION_OBJECT_2D_H