Debian Codegraph @

modules/godot_physics_2d/godot_collision_object_2d.h

raw symbols SCIP
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) 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 #pragma once
32
33 #include "godot_broad_phase_2d.h"
34 #include "godot_shape_2d.h"
35
36 #include "core/templates/self_list.h"
37 #include "servers/physics_2d/physics_server_2d.h"
38
39 class GodotSpace2D;
40
41 class GodotCollisionObject2D : public GodotShapeOwner2D {
42 public:
43 enum Type {
44 TYPE_AREA,
45 TYPE_BODY
46 };
47
48 private:
49 Type type;
50 RID self;
51 ObjectID instance_id;
52 ObjectID canvas_instance_id;
53 bool pickable = true;
54
55 struct Shape {
56 Transform2D xform;
57 Transform2D xform_inv;
58 GodotBroadPhase2D::ID bpid = 0;
59 Rect2 aabb_cache; //for rayqueries
60 GodotShape2D *shape = nullptr;
61 bool disabled = false;
62 bool one_way_collision = false;
63 real_t one_way_collision_margin = 0.0;
64 };
65
66 Vector<Shape> shapes;
67 GodotSpace2D *space = nullptr;
68 Transform2D transform;
69 Transform2D inv_transform;
70 uint32_t collision_mask = 1;
71 uint32_t collision_layer = 1;
72 real_t collision_priority = 1.0;
73 bool _static = true;
74
75 SelfList<GodotCollisionObject2D> pending_shape_update_list;
76
77 void _update_shapes();
78
79 protected:
80 void _update_shapes_with_motion(const Vector2 &p_motion);
81 void _unregister_shapes();
82
83 _FORCE_INLINE_ void _set_transform(const Transform2D &p_transform, bool p_update_shapes = true) {
84 transform = p_transform;
85 if (p_update_shapes) {
86 _update_shapes();
87 }
88 }
89 _FORCE_INLINE_ void _set_inv_transform(const Transform2D &p_transform) { inv_transform = p_transform; }
90 void _set_static(bool p_static);
91
92 virtual void _shapes_changed() = 0;
93 void _set_space(GodotSpace2D *p_space);
94
95 GodotCollisionObject2D(Type p_type);
96
97 public:
98 _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
99 _FORCE_INLINE_ RID get_self() const { return self; }
100
101 _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
102 _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
103
104 _FORCE_INLINE_ void set_canvas_instance_id(const ObjectID &p_canvas_instance_id) { canvas_instance_id = p_canvas_instance_id; }
105 _FORCE_INLINE_ ObjectID get_canvas_instance_id() const { return canvas_instance_id; }
106
107 void _shape_changed() override;
108
109 _FORCE_INLINE_ Type get_type() const { return type; }
110 void add_shape(GodotShape2D *p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);
111 void set_shape(int p_index, GodotShape2D *p_shape);
112 void set_shape_transform(int p_index, const Transform2D &p_transform);
113
114 _FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }
115 _FORCE_INLINE_ GodotShape2D *get_shape(int p_index) const {
116 CRASH_BAD_INDEX(p_index, shapes.size());
117 return shapes[p_index].shape;
118 }
119 _FORCE_INLINE_ const Transform2D &get_shape_transform(int p_index) const {
120 CRASH_BAD_INDEX(p_index, shapes.size());
121 return shapes[p_index].xform;
122 }
123 _FORCE_INLINE_ const Transform2D &get_shape_inv_transform(int p_index) const {
124 CRASH_BAD_INDEX(p_index, shapes.size());
125 return shapes[p_index].xform_inv;
126 }
127 _FORCE_INLINE_ const Rect2 &get_shape_aabb(int p_index) const {
128 CRASH_BAD_INDEX(p_index, shapes.size());
129 return shapes[p_index].aabb_cache;
130 }
131
132 _FORCE_INLINE_ const Transform2D &get_transform() const { return transform; }
133 _FORCE_INLINE_ const Transform2D &get_inv_transform() const { return inv_transform; }
134 _FORCE_INLINE_ GodotSpace2D *get_space() const { return space; }
135
136 void set_shape_disabled(int p_idx, bool p_disabled);
137 _FORCE_INLINE_ bool is_shape_disabled(int p_idx) const {
138 ERR_FAIL_INDEX_V(p_idx, shapes.size(), false);
139 return shapes[p_idx].disabled;
140 }
141
142 _FORCE_INLINE_ void set_shape_as_one_way_collision(int p_idx, bool p_one_way_collision, real_t p_margin) {
143 CRASH_BAD_INDEX(p_idx, shapes.size());
144 shapes.write[p_idx].one_way_collision = p_one_way_collision;
145 shapes.write[p_idx].one_way_collision_margin = p_margin;
146 }
147 _FORCE_INLINE_ bool is_shape_set_as_one_way_collision(int p_idx) const {
148 CRASH_BAD_INDEX(p_idx, shapes.size());
149 return shapes[p_idx].one_way_collision;
150 }
151
152 _FORCE_INLINE_ real_t get_shape_one_way_collision_margin(int p_idx) const {
153 CRASH_BAD_INDEX(p_idx, shapes.size());
154 return shapes[p_idx].one_way_collision_margin;
155 }
156
157 void set_collision_mask(uint32_t p_mask) {
158 collision_mask = p_mask;
159 _shape_changed();
160 }
161 _FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }
162
163 void set_collision_layer(uint32_t p_layer) {
164 collision_layer = p_layer;
165 _shape_changed();
166 }
167 _FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }
168
169 _FORCE_INLINE_ void set_collision_priority(real_t p_priority) {
170 ERR_FAIL_COND_MSG(p_priority <= 0, "Priority must be greater than 0.");
171 collision_priority = p_priority;
172 _shape_changed();
173 }
174 _FORCE_INLINE_ real_t get_collision_priority() const { return collision_priority; }
175
176 void remove_shape(GodotShape2D *p_shape) override;
177 void remove_shape(int p_index);
178
179 virtual void set_space(GodotSpace2D *p_space) = 0;
180
181 _FORCE_INLINE_ bool is_static() const { return _static; }
182
183 void set_pickable(bool p_pickable) { pickable = p_pickable; }
184 _FORCE_INLINE_ bool is_pickable() const { return pickable; }
185
186 _FORCE_INLINE_ bool collides_with(GodotCollisionObject2D *p_other) const {
187 return p_other->collision_layer & collision_mask;
188 }
189
190 _FORCE_INLINE_ bool interacts_with(const GodotCollisionObject2D *p_other) const {
191 return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;
192 }
193
194 virtual ~GodotCollisionObject2D() {}
195 };