Path: blob/master/modules/jolt_physics/objects/jolt_object_3d.cpp
10278 views
/**************************************************************************/1/* jolt_object_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "jolt_object_3d.h"3132#include "../jolt_physics_server_3d.h"33#include "../jolt_project_settings.h"34#include "../spaces/jolt_layers.h"35#include "../spaces/jolt_space_3d.h"36#include "jolt_group_filter.h"3738void JoltObject3D::_remove_from_space() {39if (!in_space()) {40return;41}4243space->remove_object(jolt_body->GetID());44jolt_body = nullptr;45}4647void JoltObject3D::_reset_space() {48ERR_FAIL_NULL(space);4950_space_changing();51_remove_from_space();52_add_to_space();53_space_changed();54}5556void JoltObject3D::_update_object_layer() {57if (!in_space()) {58return;59}6061space->get_body_iface().SetObjectLayer(jolt_body->GetID(), _get_object_layer());62}6364void JoltObject3D::_collision_layer_changed() {65_update_object_layer();66}6768void JoltObject3D::_collision_mask_changed() {69_update_object_layer();70}7172JoltObject3D::JoltObject3D(ObjectType p_object_type) :73object_type(p_object_type) {74}7576JoltObject3D::~JoltObject3D() = default;7778Object *JoltObject3D::get_instance() const {79return ObjectDB::get_instance(instance_id);80}8182void JoltObject3D::set_space(JoltSpace3D *p_space) {83if (space == p_space) {84return;85}8687_space_changing();8889if (space != nullptr) {90_remove_from_space();91}9293space = p_space;9495if (space != nullptr) {96_add_to_space();97}9899_space_changed();100}101102void JoltObject3D::set_collision_layer(uint32_t p_layer) {103if (p_layer == collision_layer) {104return;105}106107collision_layer = p_layer;108109_collision_layer_changed();110}111112void JoltObject3D::set_collision_mask(uint32_t p_mask) {113if (p_mask == collision_mask) {114return;115}116117collision_mask = p_mask;118119_collision_mask_changed();120}121122bool JoltObject3D::can_collide_with(const JoltObject3D &p_other) const {123return (collision_mask & p_other.get_collision_layer()) != 0;124}125126bool JoltObject3D::can_interact_with(const JoltObject3D &p_other) const {127if (const JoltBody3D *other_body = p_other.as_body()) {128return can_interact_with(*other_body);129} else if (const JoltArea3D *other_area = p_other.as_area()) {130return can_interact_with(*other_area);131} else if (const JoltSoftBody3D *other_soft_body = p_other.as_soft_body()) {132return can_interact_with(*other_soft_body);133} else {134ERR_FAIL_V_MSG(false, vformat("Unhandled object type: '%d'. This should not happen. Please report this.", p_other.get_type()));135}136}137138String JoltObject3D::to_string() const {139static const String fallback_name = "<unknown>";140141if (JoltPhysicsServer3D::get_singleton()->is_on_separate_thread()) {142return fallback_name; // Calling `Object::to_string` is not thread-safe.143}144145Object *instance = get_instance();146return instance != nullptr ? instance->to_string() : fallback_name;147}148149150