2020-10-21 14:13:20 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* broad_phase_bvh.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-13 16:54:31 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-10-21 14:13:20 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "broad_phase_bvh.h"
|
|
|
|
#include "collision_object_sw.h"
|
|
|
|
#include "core/project_settings.h"
|
|
|
|
|
2021-04-30 20:45:41 +02:00
|
|
|
BroadPhaseSW::ID BroadPhaseBVH::create(CollisionObjectSW *p_object, int p_subindex, const AABB &p_aabb, bool p_static) {
|
|
|
|
ID oid = bvh.create(p_object, true, p_aabb, p_subindex, !p_static, 1 << p_object->get_type(), p_static ? 0 : 0xFFFFF); // Pair everything, don't care?
|
2021-01-14 13:56:28 +01:00
|
|
|
return oid + 1;
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BroadPhaseBVH::move(ID p_id, const AABB &p_aabb) {
|
2021-01-14 13:56:28 +01:00
|
|
|
bvh.move(p_id - 1, p_aabb);
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 21:06:09 +02:00
|
|
|
void BroadPhaseBVH::recheck_pairs(ID p_id) {
|
|
|
|
bvh.recheck_pairs(p_id - 1);
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
void BroadPhaseBVH::set_static(ID p_id, bool p_static) {
|
2021-01-14 13:56:28 +01:00
|
|
|
CollisionObjectSW *it = bvh.get(p_id - 1);
|
2021-04-30 20:45:41 +02:00
|
|
|
bvh.set_pairable(p_id - 1, !p_static, 1 << it->get_type(), p_static ? 0 : 0xFFFFF, false); // Pair everything, don't care?
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
2021-10-21 21:06:09 +02:00
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
void BroadPhaseBVH::remove(ID p_id) {
|
2021-01-14 13:56:28 +01:00
|
|
|
bvh.erase(p_id - 1);
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CollisionObjectSW *BroadPhaseBVH::get_object(ID p_id) const {
|
2021-01-14 13:56:28 +01:00
|
|
|
CollisionObjectSW *it = bvh.get(p_id - 1);
|
2021-05-04 16:00:45 +02:00
|
|
|
ERR_FAIL_COND_V(!it, nullptr);
|
2020-10-21 14:13:20 +02:00
|
|
|
return it;
|
|
|
|
}
|
2021-10-21 21:06:09 +02:00
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
bool BroadPhaseBVH::is_static(ID p_id) const {
|
2021-01-14 13:56:28 +01:00
|
|
|
return !bvh.is_pairable(p_id - 1);
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
2021-10-21 21:06:09 +02:00
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
int BroadPhaseBVH::get_subindex(ID p_id) const {
|
2021-01-14 13:56:28 +01:00
|
|
|
return bvh.get_subindex(p_id - 1);
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int BroadPhaseBVH::cull_point(const Vector3 &p_point, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
|
|
|
|
return bvh.cull_point(p_point, p_results, p_max_results, p_result_indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
int BroadPhaseBVH::cull_segment(const Vector3 &p_from, const Vector3 &p_to, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
|
|
|
|
return bvh.cull_segment(p_from, p_to, p_results, p_max_results, p_result_indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
int BroadPhaseBVH::cull_aabb(const AABB &p_aabb, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
|
|
|
|
return bvh.cull_aabb(p_aabb, p_results, p_max_results, p_result_indices);
|
|
|
|
}
|
|
|
|
|
2021-10-21 21:06:09 +02:00
|
|
|
void *BroadPhaseBVH::_pair_callback(void *p_self, uint32_t p_id_A, CollisionObjectSW *p_object_A, int p_subindex_A, uint32_t p_id_B, CollisionObjectSW *p_object_B, int p_subindex_B) {
|
|
|
|
BroadPhaseBVH *bpo = (BroadPhaseBVH *)(p_self);
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!bpo->pair_callback) {
|
2021-05-04 16:00:45 +02:00
|
|
|
return nullptr;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2020-10-21 14:13:20 +02:00
|
|
|
|
2021-10-21 21:06:09 +02:00
|
|
|
return bpo->pair_callback(p_object_A, p_subindex_A, p_object_B, p_subindex_B, nullptr, bpo->pair_userdata);
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 21:06:09 +02:00
|
|
|
void BroadPhaseBVH::_unpair_callback(void *p_self, uint32_t p_id_A, CollisionObjectSW *p_object_A, int p_subindex_A, uint32_t p_id_B, CollisionObjectSW *p_object_B, int p_subindex_B, void *p_pair_data) {
|
|
|
|
BroadPhaseBVH *bpo = (BroadPhaseBVH *)(p_self);
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!bpo->unpair_callback) {
|
2020-10-21 14:13:20 +02:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2020-10-21 14:13:20 +02:00
|
|
|
|
2021-10-21 21:06:09 +02:00
|
|
|
bpo->unpair_callback(p_object_A, p_subindex_A, p_object_B, p_subindex_B, p_pair_data, bpo->unpair_userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *BroadPhaseBVH::_check_pair_callback(void *p_self, uint32_t p_id_A, CollisionObjectSW *p_object_A, int p_subindex_A, uint32_t p_id_B, CollisionObjectSW *p_object_B, int p_subindex_B, void *p_pair_data) {
|
|
|
|
BroadPhaseBVH *bpo = (BroadPhaseBVH *)(p_self);
|
|
|
|
if (!bpo->pair_callback) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bpo->pair_callback(p_object_A, p_subindex_A, p_object_B, p_subindex_B, p_pair_data, bpo->pair_userdata);
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BroadPhaseBVH::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
|
|
|
|
pair_callback = p_pair_callback;
|
|
|
|
pair_userdata = p_userdata;
|
|
|
|
}
|
2021-10-21 21:06:09 +02:00
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
void BroadPhaseBVH::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
|
|
|
|
unpair_callback = p_unpair_callback;
|
|
|
|
unpair_userdata = p_userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BroadPhaseBVH::update() {
|
|
|
|
bvh.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
BroadPhaseSW *BroadPhaseBVH::_create() {
|
|
|
|
return memnew(BroadPhaseBVH);
|
|
|
|
}
|
|
|
|
|
|
|
|
BroadPhaseBVH::BroadPhaseBVH() {
|
2021-05-18 21:36:25 +02:00
|
|
|
bvh.params_set_thread_safe(GLOBAL_GET("rendering/threads/thread_safe_bvh"));
|
2021-11-18 15:32:33 +01:00
|
|
|
bvh.params_set_pairing_expansion(GLOBAL_GET("physics/3d/godot_physics/bvh_collision_margin"));
|
2020-10-21 14:13:20 +02:00
|
|
|
bvh.set_pair_callback(_pair_callback, this);
|
|
|
|
bvh.set_unpair_callback(_unpair_callback, this);
|
2021-10-21 21:06:09 +02:00
|
|
|
bvh.set_check_pair_callback(_check_pair_callback, this);
|
2021-05-04 16:00:45 +02:00
|
|
|
pair_callback = nullptr;
|
|
|
|
pair_userdata = nullptr;
|
|
|
|
unpair_userdata = nullptr;
|
2020-10-21 14:13:20 +02:00
|
|
|
}
|