Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/jolt_physics/objects/jolt_object_3d.cpp
10278 views
1
/**************************************************************************/
2
/* jolt_object_3d.cpp */
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
#include "jolt_object_3d.h"
32
33
#include "../jolt_physics_server_3d.h"
34
#include "../jolt_project_settings.h"
35
#include "../spaces/jolt_layers.h"
36
#include "../spaces/jolt_space_3d.h"
37
#include "jolt_group_filter.h"
38
39
void JoltObject3D::_remove_from_space() {
40
if (!in_space()) {
41
return;
42
}
43
44
space->remove_object(jolt_body->GetID());
45
jolt_body = nullptr;
46
}
47
48
void JoltObject3D::_reset_space() {
49
ERR_FAIL_NULL(space);
50
51
_space_changing();
52
_remove_from_space();
53
_add_to_space();
54
_space_changed();
55
}
56
57
void JoltObject3D::_update_object_layer() {
58
if (!in_space()) {
59
return;
60
}
61
62
space->get_body_iface().SetObjectLayer(jolt_body->GetID(), _get_object_layer());
63
}
64
65
void JoltObject3D::_collision_layer_changed() {
66
_update_object_layer();
67
}
68
69
void JoltObject3D::_collision_mask_changed() {
70
_update_object_layer();
71
}
72
73
JoltObject3D::JoltObject3D(ObjectType p_object_type) :
74
object_type(p_object_type) {
75
}
76
77
JoltObject3D::~JoltObject3D() = default;
78
79
Object *JoltObject3D::get_instance() const {
80
return ObjectDB::get_instance(instance_id);
81
}
82
83
void JoltObject3D::set_space(JoltSpace3D *p_space) {
84
if (space == p_space) {
85
return;
86
}
87
88
_space_changing();
89
90
if (space != nullptr) {
91
_remove_from_space();
92
}
93
94
space = p_space;
95
96
if (space != nullptr) {
97
_add_to_space();
98
}
99
100
_space_changed();
101
}
102
103
void JoltObject3D::set_collision_layer(uint32_t p_layer) {
104
if (p_layer == collision_layer) {
105
return;
106
}
107
108
collision_layer = p_layer;
109
110
_collision_layer_changed();
111
}
112
113
void JoltObject3D::set_collision_mask(uint32_t p_mask) {
114
if (p_mask == collision_mask) {
115
return;
116
}
117
118
collision_mask = p_mask;
119
120
_collision_mask_changed();
121
}
122
123
bool JoltObject3D::can_collide_with(const JoltObject3D &p_other) const {
124
return (collision_mask & p_other.get_collision_layer()) != 0;
125
}
126
127
bool JoltObject3D::can_interact_with(const JoltObject3D &p_other) const {
128
if (const JoltBody3D *other_body = p_other.as_body()) {
129
return can_interact_with(*other_body);
130
} else if (const JoltArea3D *other_area = p_other.as_area()) {
131
return can_interact_with(*other_area);
132
} else if (const JoltSoftBody3D *other_soft_body = p_other.as_soft_body()) {
133
return can_interact_with(*other_soft_body);
134
} else {
135
ERR_FAIL_V_MSG(false, vformat("Unhandled object type: '%d'. This should not happen. Please report this.", p_other.get_type()));
136
}
137
}
138
139
String JoltObject3D::to_string() const {
140
static const String fallback_name = "<unknown>";
141
142
if (JoltPhysicsServer3D::get_singleton()->is_on_separate_thread()) {
143
return fallback_name; // Calling `Object::to_string` is not thread-safe.
144
}
145
146
Object *instance = get_instance();
147
return instance != nullptr ? instance->to_string() : fallback_name;
148
}
149
150