Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/collision_object_2d.h
21344 views
1
/**************************************************************************/
2
/* 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 "scene/2d/node_2d.h"
34
#include "scene/main/viewport.h"
35
#include "scene/resources/2d/shape_2d.h"
36
#include "servers/physics_2d/physics_server_2d.h"
37
38
class CollisionObject2D : public Node2D {
39
GDCLASS(CollisionObject2D, Node2D);
40
41
public:
42
static constexpr AncestralClass static_ancestral_class = AncestralClass::COLLISION_OBJECT_2D;
43
44
enum DisableMode {
45
DISABLE_MODE_REMOVE,
46
DISABLE_MODE_MAKE_STATIC,
47
DISABLE_MODE_KEEP_ACTIVE,
48
};
49
50
private:
51
uint32_t collision_layer = 1;
52
uint32_t collision_mask = 1;
53
real_t collision_priority = 1.0;
54
55
bool area = false;
56
RID rid;
57
uint32_t callback_lock = 0;
58
bool pickable = false;
59
60
DisableMode disable_mode = DISABLE_MODE_REMOVE;
61
62
PhysicsServer2D::BodyMode body_mode = PhysicsServer2D::BODY_MODE_STATIC;
63
64
struct ShapeData {
65
ObjectID owner_id;
66
Transform2D xform;
67
struct Shape {
68
Ref<Shape2D> shape;
69
int index = 0;
70
};
71
72
Vector<Shape> shapes;
73
74
bool disabled = false;
75
bool one_way_collision = false;
76
real_t one_way_collision_margin = 0.0;
77
Vector2 one_way_collision_direction = Vector2(0.0, 1.0);
78
};
79
80
int total_subshapes = 0;
81
82
RBMap<uint32_t, ShapeData> shapes;
83
bool only_update_transform_changes = false; // This is used for sync to physics.
84
85
void _apply_disabled();
86
void _apply_enabled();
87
88
protected:
89
_FORCE_INLINE_ void lock_callback() { callback_lock++; }
90
_FORCE_INLINE_ void unlock_callback() {
91
ERR_FAIL_COND(callback_lock == 0);
92
callback_lock--;
93
}
94
95
CollisionObject2D(RID p_rid, bool p_area);
96
97
void _notification(int p_what);
98
static void _bind_methods();
99
100
void _update_pickable();
101
friend class Viewport;
102
void _input_event_call(Viewport *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape);
103
void _mouse_enter();
104
void _mouse_exit();
105
106
void _mouse_shape_enter(int p_shape);
107
void _mouse_shape_exit(int p_shape);
108
109
void set_only_update_transform_changes(bool p_enable);
110
bool is_only_update_transform_changes_enabled() const;
111
112
void set_body_mode(PhysicsServer2D::BodyMode p_mode);
113
114
virtual void _space_changed(const RID &p_new_space);
115
116
GDVIRTUAL3(_input_event, RequiredParam<Viewport>, RequiredParam<InputEvent>, int)
117
GDVIRTUAL0(_mouse_enter)
118
GDVIRTUAL0(_mouse_exit)
119
GDVIRTUAL1(_mouse_shape_enter, int)
120
GDVIRTUAL1(_mouse_shape_exit, int)
121
public:
122
void set_collision_layer(uint32_t p_layer);
123
uint32_t get_collision_layer() const;
124
125
void set_collision_mask(uint32_t p_mask);
126
uint32_t get_collision_mask() const;
127
128
void set_collision_layer_value(int p_layer_number, bool p_value);
129
bool get_collision_layer_value(int p_layer_number) const;
130
131
void set_collision_mask_value(int p_layer_number, bool p_value);
132
bool get_collision_mask_value(int p_layer_number) const;
133
134
void set_collision_priority(real_t p_priority);
135
real_t get_collision_priority() const;
136
137
void set_disable_mode(DisableMode p_mode);
138
DisableMode get_disable_mode() const;
139
140
uint32_t create_shape_owner(Object *p_owner);
141
void remove_shape_owner(uint32_t owner);
142
void get_shape_owners(List<uint32_t> *r_owners);
143
PackedInt32Array _get_shape_owners();
144
145
void shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform);
146
Transform2D shape_owner_get_transform(uint32_t p_owner) const;
147
Object *shape_owner_get_owner(uint32_t p_owner) const;
148
149
void shape_owner_set_disabled(uint32_t p_owner, bool p_disabled);
150
bool is_shape_owner_disabled(uint32_t p_owner) const;
151
152
void shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable);
153
bool is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const;
154
155
void shape_owner_set_one_way_collision_margin(uint32_t p_owner, real_t p_margin);
156
real_t get_shape_owner_one_way_collision_margin(uint32_t p_owner) const;
157
158
void shape_owner_set_one_way_collision_direction(uint32_t p_owner, const Vector2 &p_direction);
159
Vector2 get_shape_owner_one_way_collision_direction(uint32_t p_owner) const;
160
161
void shape_owner_add_shape(uint32_t p_owner, RequiredParam<Shape2D> rp_shape);
162
int shape_owner_get_shape_count(uint32_t p_owner) const;
163
Ref<Shape2D> shape_owner_get_shape(uint32_t p_owner, int p_shape) const;
164
int shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const;
165
166
void shape_owner_remove_shape(uint32_t p_owner, int p_shape);
167
void shape_owner_clear_shapes(uint32_t p_owner);
168
169
uint32_t shape_find_owner(int p_shape_index) const;
170
171
void set_pickable(bool p_enabled);
172
bool is_pickable() const;
173
174
PackedStringArray get_configuration_warnings() const override;
175
176
_FORCE_INLINE_ RID get_rid() const { return rid; }
177
178
CollisionObject2D();
179
~CollisionObject2D();
180
};
181
182
VARIANT_ENUM_CAST(CollisionObject2D::DisableMode);
183
184