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