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