2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-10-18 21:24:30 +02:00
|
|
|
/* godot_broad_phase_2d_bvh.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
#include "godot_broad_phase_2d_bvh.h"
|
|
|
|
#include "godot_collision_object_2d.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotBroadPhase2D::ID GodotBroadPhase2DBVH::create(GodotCollisionObject2D *p_object, int p_subindex, const Rect2 &p_aabb, bool p_static) {
|
2022-02-04 17:46:10 +01:00
|
|
|
uint32_t tree_id = p_static ? TREE_STATIC : TREE_DYNAMIC;
|
2022-03-06 15:03:19 +01:00
|
|
|
uint32_t tree_collision_mask = p_static ? TREE_FLAG_DYNAMIC : (TREE_FLAG_STATIC | TREE_FLAG_DYNAMIC);
|
2022-02-04 17:46:10 +01:00
|
|
|
ID oid = bvh.create(p_object, true, tree_id, tree_collision_mask, p_aabb, p_subindex); // Pair everything, don't care?
|
2021-05-10 23:43:13 +02:00
|
|
|
return oid + 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBroadPhase2DBVH::move(ID p_id, const Rect2 &p_aabb) {
|
2022-03-23 09:06:29 +01:00
|
|
|
ERR_FAIL_COND(!p_id);
|
2021-05-10 23:43:13 +02:00
|
|
|
bvh.move(p_id - 1, p_aabb);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBroadPhase2DBVH::set_static(ID p_id, bool p_static) {
|
2022-03-23 09:06:29 +01:00
|
|
|
ERR_FAIL_COND(!p_id);
|
2022-02-04 17:46:10 +01:00
|
|
|
uint32_t tree_id = p_static ? TREE_STATIC : TREE_DYNAMIC;
|
2022-03-06 15:03:19 +01:00
|
|
|
uint32_t tree_collision_mask = p_static ? TREE_FLAG_DYNAMIC : (TREE_FLAG_STATIC | TREE_FLAG_DYNAMIC);
|
2022-02-04 17:46:10 +01:00
|
|
|
bvh.set_tree(p_id - 1, tree_id, tree_collision_mask, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBroadPhase2DBVH::remove(ID p_id) {
|
2022-03-23 09:06:29 +01:00
|
|
|
ERR_FAIL_COND(!p_id);
|
2021-05-10 23:43:13 +02:00
|
|
|
bvh.erase(p_id - 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotCollisionObject2D *GodotBroadPhase2DBVH::get_object(ID p_id) const {
|
2022-03-23 09:06:29 +01:00
|
|
|
ERR_FAIL_COND_V(!p_id, nullptr);
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotCollisionObject2D *it = bvh.get(p_id - 1);
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(!it, nullptr);
|
2014-02-10 02:10:30 +01:00
|
|
|
return it;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
bool GodotBroadPhase2DBVH::is_static(ID p_id) const {
|
2022-03-23 09:06:29 +01:00
|
|
|
ERR_FAIL_COND_V(!p_id, false);
|
2022-02-04 17:46:10 +01:00
|
|
|
uint32_t tree_id = bvh.get_tree_id(p_id - 1);
|
|
|
|
return tree_id == 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
int GodotBroadPhase2DBVH::get_subindex(ID p_id) const {
|
2022-03-23 09:06:29 +01:00
|
|
|
ERR_FAIL_COND_V(!p_id, 0);
|
2021-05-10 23:43:13 +02:00
|
|
|
return bvh.get_subindex(p_id - 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
int GodotBroadPhase2DBVH::cull_segment(const Vector2 &p_from, const Vector2 &p_to, GodotCollisionObject2D **p_results, int p_max_results, int *p_result_indices) {
|
2022-02-04 17:46:10 +01:00
|
|
|
return bvh.cull_segment(p_from, p_to, p_results, p_max_results, nullptr, 0xFFFFFFFF, p_result_indices);
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
int GodotBroadPhase2DBVH::cull_aabb(const Rect2 &p_aabb, GodotCollisionObject2D **p_results, int p_max_results, int *p_result_indices) {
|
2022-02-04 17:46:10 +01:00
|
|
|
return bvh.cull_aabb(p_aabb, p_results, p_max_results, nullptr, 0xFFFFFFFF, p_result_indices);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void *GodotBroadPhase2DBVH::_pair_callback(void *self, uint32_t p_A, GodotCollisionObject2D *p_object_A, int subindex_A, uint32_t p_B, GodotCollisionObject2D *p_object_B, int subindex_B) {
|
2022-04-05 12:40:26 +02:00
|
|
|
GodotBroadPhase2DBVH *bpo = static_cast<GodotBroadPhase2DBVH *>(self);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!bpo->pair_callback) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return bpo->pair_callback(p_object_A, subindex_A, p_object_B, subindex_B, bpo->pair_userdata);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBroadPhase2DBVH::_unpair_callback(void *self, uint32_t p_A, GodotCollisionObject2D *p_object_A, int subindex_A, uint32_t p_B, GodotCollisionObject2D *p_object_B, int subindex_B, void *pairdata) {
|
2022-04-05 12:40:26 +02:00
|
|
|
GodotBroadPhase2DBVH *bpo = static_cast<GodotBroadPhase2DBVH *>(self);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!bpo->unpair_callback) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bpo->unpair_callback(p_object_A, subindex_A, p_object_B, subindex_B, pairdata, bpo->unpair_userdata);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBroadPhase2DBVH::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
|
2017-03-05 16:44:50 +01:00
|
|
|
pair_callback = p_pair_callback;
|
|
|
|
pair_userdata = p_userdata;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBroadPhase2DBVH::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
|
2017-03-05 16:44:50 +01:00
|
|
|
unpair_callback = p_unpair_callback;
|
|
|
|
unpair_userdata = p_userdata;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBroadPhase2DBVH::update() {
|
2021-05-10 23:43:13 +02:00
|
|
|
bvh.update();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotBroadPhase2D *GodotBroadPhase2DBVH::_create() {
|
|
|
|
return memnew(GodotBroadPhase2DBVH);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotBroadPhase2DBVH::GodotBroadPhase2DBVH() {
|
2021-05-10 23:43:13 +02:00
|
|
|
bvh.set_pair_callback(_pair_callback, this);
|
|
|
|
bvh.set_unpair_callback(_unpair_callback, this);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|