collision_object_3d.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**************************************************************************/
  2. /* collision_object_3d.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_3D_H
  31. #define COLLISION_OBJECT_3D_H
  32. #include "scene/3d/camera_3d.h"
  33. #include "scene/3d/node_3d.h"
  34. class CollisionObject3D : public Node3D {
  35. GDCLASS(CollisionObject3D, Node3D);
  36. public:
  37. enum DisableMode {
  38. DISABLE_MODE_REMOVE,
  39. DISABLE_MODE_MAKE_STATIC,
  40. DISABLE_MODE_KEEP_ACTIVE,
  41. };
  42. private:
  43. uint32_t collision_layer = 1;
  44. uint32_t collision_mask = 1;
  45. real_t collision_priority = 1.0;
  46. bool area = false;
  47. RID rid;
  48. uint32_t callback_lock = 0;
  49. DisableMode disable_mode = DISABLE_MODE_REMOVE;
  50. PhysicsServer3D::BodyMode body_mode = PhysicsServer3D::BODY_MODE_STATIC;
  51. struct ShapeData {
  52. ObjectID owner_id;
  53. Transform3D xform;
  54. struct ShapeBase {
  55. RID debug_shape;
  56. Ref<Shape3D> shape;
  57. int index = 0;
  58. };
  59. Vector<ShapeBase> shapes;
  60. bool disabled = false;
  61. };
  62. int total_subshapes = 0;
  63. RBMap<uint32_t, ShapeData> shapes;
  64. bool only_update_transform_changes = false; // This is used for sync to physics.
  65. bool capture_input_on_drag = false;
  66. bool ray_pickable = true;
  67. HashSet<uint32_t> debug_shapes_to_update;
  68. int debug_shapes_count = 0;
  69. Transform3D debug_shape_old_transform;
  70. void _update_pickable();
  71. bool _are_collision_shapes_visible();
  72. void _update_shape_data(uint32_t p_owner);
  73. void _shape_changed(const Ref<Shape3D> &p_shape);
  74. void _update_debug_shapes();
  75. void _clear_debug_shapes();
  76. void _apply_disabled();
  77. void _apply_enabled();
  78. protected:
  79. CollisionObject3D(RID p_rid, bool p_area);
  80. _FORCE_INLINE_ void lock_callback() { callback_lock++; }
  81. _FORCE_INLINE_ void unlock_callback() {
  82. ERR_FAIL_COND(callback_lock == 0);
  83. callback_lock--;
  84. }
  85. void _notification(int p_what);
  86. static void _bind_methods();
  87. void _on_transform_changed();
  88. friend class Viewport;
  89. virtual void _input_event_call(Camera3D *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape);
  90. virtual void _mouse_enter();
  91. virtual void _mouse_exit();
  92. void set_body_mode(PhysicsServer3D::BodyMode p_mode);
  93. virtual void _space_changed(const RID &p_new_space);
  94. void set_only_update_transform_changes(bool p_enable);
  95. bool is_only_update_transform_changes_enabled() const;
  96. GDVIRTUAL5(_input_event, Camera3D *, Ref<InputEvent>, Vector3, Vector3, int)
  97. GDVIRTUAL0(_mouse_enter)
  98. GDVIRTUAL0(_mouse_exit)
  99. public:
  100. void set_collision_layer(uint32_t p_layer);
  101. uint32_t get_collision_layer() const;
  102. void set_collision_mask(uint32_t p_mask);
  103. uint32_t get_collision_mask() const;
  104. void set_collision_layer_value(int p_layer_number, bool p_value);
  105. bool get_collision_layer_value(int p_layer_number) const;
  106. void set_collision_mask_value(int p_layer_number, bool p_value);
  107. bool get_collision_mask_value(int p_layer_number) const;
  108. void set_collision_priority(real_t p_priority);
  109. real_t get_collision_priority() const;
  110. void set_disable_mode(DisableMode p_mode);
  111. DisableMode get_disable_mode() const;
  112. uint32_t create_shape_owner(Object *p_owner);
  113. void remove_shape_owner(uint32_t owner);
  114. void get_shape_owners(List<uint32_t> *r_owners);
  115. PackedInt32Array _get_shape_owners();
  116. void shape_owner_set_transform(uint32_t p_owner, const Transform3D &p_transform);
  117. Transform3D shape_owner_get_transform(uint32_t p_owner) const;
  118. Object *shape_owner_get_owner(uint32_t p_owner) const;
  119. void shape_owner_set_disabled(uint32_t p_owner, bool p_disabled);
  120. bool is_shape_owner_disabled(uint32_t p_owner) const;
  121. void shape_owner_add_shape(uint32_t p_owner, const Ref<Shape3D> &p_shape);
  122. int shape_owner_get_shape_count(uint32_t p_owner) const;
  123. Ref<Shape3D> shape_owner_get_shape(uint32_t p_owner, int p_shape) const;
  124. int shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const;
  125. void shape_owner_remove_shape(uint32_t p_owner, int p_shape);
  126. void shape_owner_clear_shapes(uint32_t p_owner);
  127. uint32_t shape_find_owner(int p_shape_index) const;
  128. void set_ray_pickable(bool p_ray_pickable);
  129. bool is_ray_pickable() const;
  130. void set_capture_input_on_drag(bool p_capture);
  131. bool get_capture_input_on_drag() const;
  132. _FORCE_INLINE_ RID get_rid() const { return rid; }
  133. PackedStringArray get_configuration_warnings() const override;
  134. CollisionObject3D();
  135. ~CollisionObject3D();
  136. };
  137. VARIANT_ENUM_CAST(CollisionObject3D::DisableMode);
  138. #endif // COLLISION_OBJECT_3D_H