collision_object_sw.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**************************************************************************/
  2. /* collision_object_sw.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 COLLISION_OBJECT_SW_H
  31. #define COLLISION_OBJECT_SW_H
  32. #include "broad_phase_sw.h"
  33. #include "core/self_list.h"
  34. #include "servers/physics_server.h"
  35. #include "shape_sw.h"
  36. #ifdef DEBUG_ENABLED
  37. #define MAX_OBJECT_DISTANCE 3.1622776601683791e+18
  38. #define MAX_OBJECT_DISTANCE_X2 (MAX_OBJECT_DISTANCE * MAX_OBJECT_DISTANCE)
  39. #endif
  40. class SpaceSW;
  41. class CollisionObjectSW : public ShapeOwnerSW {
  42. public:
  43. enum Type {
  44. TYPE_AREA,
  45. TYPE_BODY
  46. };
  47. private:
  48. Type type;
  49. RID self;
  50. ObjectID instance_id;
  51. uint32_t collision_layer;
  52. uint32_t collision_mask;
  53. struct Shape {
  54. Transform xform;
  55. Transform xform_inv;
  56. BroadPhaseSW::ID bpid;
  57. AABB aabb_cache; //for rayqueries
  58. real_t area_cache;
  59. ShapeSW *shape;
  60. bool disabled;
  61. Shape() { disabled = false; }
  62. };
  63. Vector<Shape> shapes;
  64. SpaceSW *space;
  65. Transform transform;
  66. Transform inv_transform;
  67. bool _static;
  68. SelfList<CollisionObjectSW> pending_shape_update_list;
  69. void _update_shapes();
  70. void _recheck_shapes();
  71. protected:
  72. void _update_shapes_with_motion(const Vector3 &p_motion);
  73. void _unregister_shapes();
  74. _FORCE_INLINE_ void _set_transform(const Transform &p_transform, bool p_update_shapes = true) {
  75. #ifdef DEBUG_ENABLED
  76. ERR_FAIL_COND_MSG(p_transform.origin.length_squared() > MAX_OBJECT_DISTANCE_X2, "Object went too far away (more than '" + itos(MAX_OBJECT_DISTANCE) + "' units from origin).");
  77. #endif
  78. transform = p_transform;
  79. if (p_update_shapes) {
  80. _update_shapes();
  81. }
  82. }
  83. _FORCE_INLINE_ void _set_inv_transform(const Transform &p_transform) { inv_transform = p_transform; }
  84. void _set_static(bool p_static);
  85. virtual void _shapes_changed() = 0;
  86. void _set_space(SpaceSW *p_space);
  87. bool ray_pickable;
  88. CollisionObjectSW(Type p_type);
  89. public:
  90. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  91. _FORCE_INLINE_ RID get_self() const { return self; }
  92. _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
  93. _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
  94. void _shape_changed();
  95. _FORCE_INLINE_ Type get_type() const { return type; }
  96. void add_shape(ShapeSW *p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
  97. void set_shape(int p_index, ShapeSW *p_shape);
  98. void set_shape_transform(int p_index, const Transform &p_transform);
  99. _FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }
  100. _FORCE_INLINE_ ShapeSW *get_shape(int p_index) const {
  101. CRASH_BAD_INDEX(p_index, shapes.size());
  102. return shapes[p_index].shape;
  103. }
  104. _FORCE_INLINE_ const Transform &get_shape_transform(int p_index) const {
  105. CRASH_BAD_INDEX(p_index, shapes.size());
  106. return shapes[p_index].xform;
  107. }
  108. _FORCE_INLINE_ const Transform &get_shape_inv_transform(int p_index) const {
  109. CRASH_BAD_INDEX(p_index, shapes.size());
  110. return shapes[p_index].xform_inv;
  111. }
  112. _FORCE_INLINE_ const AABB &get_shape_aabb(int p_index) const {
  113. CRASH_BAD_INDEX(p_index, shapes.size());
  114. return shapes[p_index].aabb_cache;
  115. }
  116. _FORCE_INLINE_ real_t get_shape_area(int p_index) const {
  117. CRASH_BAD_INDEX(p_index, shapes.size());
  118. return shapes[p_index].area_cache;
  119. }
  120. _FORCE_INLINE_ Transform get_transform() const { return transform; }
  121. _FORCE_INLINE_ Transform get_inv_transform() const { return inv_transform; }
  122. _FORCE_INLINE_ SpaceSW *get_space() const { return space; }
  123. _FORCE_INLINE_ void set_ray_pickable(bool p_enable) { ray_pickable = p_enable; }
  124. _FORCE_INLINE_ bool is_ray_pickable() const { return ray_pickable; }
  125. void set_shape_disabled(int p_idx, bool p_disabled);
  126. _FORCE_INLINE_ bool is_shape_disabled(int p_idx) const {
  127. ERR_FAIL_INDEX_V(p_idx, shapes.size(), false);
  128. return shapes[p_idx].disabled;
  129. }
  130. _FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) {
  131. collision_layer = p_layer;
  132. _recheck_shapes();
  133. _shapes_changed();
  134. }
  135. _FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }
  136. _FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) {
  137. collision_mask = p_mask;
  138. _recheck_shapes();
  139. _shapes_changed();
  140. }
  141. _FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }
  142. _FORCE_INLINE_ bool test_collision_mask(const CollisionObjectSW *p_other) const {
  143. return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;
  144. }
  145. void remove_shape(ShapeSW *p_shape);
  146. void remove_shape(int p_index);
  147. virtual void set_space(SpaceSW *p_space) = 0;
  148. _FORCE_INLINE_ bool is_static() const { return _static; }
  149. virtual ~CollisionObjectSW() {}
  150. };
  151. #endif // COLLISION_OBJECT_SW_H