2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* broad_phase_octree.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* 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
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "broad_phase_octree.h"
|
2020-03-27 19:21:27 +01:00
|
|
|
#include "collision_object_3d_sw.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
BroadPhase3DSW::ID BroadPhaseOctree::create(CollisionObject3DSW *p_object, int p_subindex) {
|
2017-11-17 03:09:00 +01:00
|
|
|
ID oid = octree.create(p_object, AABB(), p_subindex, false, 1 << p_object->get_type(), 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
return oid;
|
|
|
|
}
|
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
void BroadPhaseOctree::move(ID p_id, const AABB &p_aabb) {
|
2017-03-05 16:44:50 +01:00
|
|
|
octree.move(p_id, p_aabb);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhaseOctree::set_static(ID p_id, bool p_static) {
|
2020-03-27 19:21:27 +01:00
|
|
|
CollisionObject3DSW *it = octree.get(p_id);
|
2019-06-26 15:08:25 +02:00
|
|
|
octree.set_pairable(p_id, !p_static, 1 << it->get_type(), p_static ? 0 : 0xFFFFF); //pair everything, don't care 1?
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhaseOctree::remove(ID p_id) {
|
2014-02-10 02:10:30 +01:00
|
|
|
octree.erase(p_id);
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
CollisionObject3DSW *BroadPhaseOctree::get_object(ID p_id) const {
|
|
|
|
CollisionObject3DSW *it = octree.get(p_id);
|
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
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool BroadPhaseOctree::is_static(ID p_id) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return !octree.is_pairable(p_id);
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int BroadPhaseOctree::get_subindex(ID p_id) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return octree.get_subindex(p_id);
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
int BroadPhaseOctree::cull_point(const Vector3 &p_point, CollisionObject3DSW **p_results, int p_max_results, int *p_result_indices) {
|
2017-07-15 06:23:10 +02:00
|
|
|
return octree.cull_point(p_point, p_results, p_max_results, p_result_indices);
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
int BroadPhaseOctree::cull_segment(const Vector3 &p_from, const Vector3 &p_to, CollisionObject3DSW **p_results, int p_max_results, int *p_result_indices) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return octree.cull_segment(p_from, p_to, p_results, p_max_results, p_result_indices);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
int BroadPhaseOctree::cull_aabb(const AABB &p_aabb, CollisionObject3DSW **p_results, int p_max_results, int *p_result_indices) {
|
2017-08-16 17:00:07 +02:00
|
|
|
return octree.cull_aabb(p_aabb, p_results, p_max_results, p_result_indices);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void *BroadPhaseOctree::_pair_callback(void *self, OctreeElementID p_A, CollisionObject3DSW *p_object_A, int subindex_A, OctreeElementID p_B, CollisionObject3DSW *p_object_B, int subindex_B) {
|
2017-03-05 16:44:50 +01:00
|
|
|
BroadPhaseOctree *bpo = (BroadPhaseOctree *)(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
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void BroadPhaseOctree::_unpair_callback(void *self, OctreeElementID p_A, CollisionObject3DSW *p_object_A, int subindex_A, OctreeElementID p_B, CollisionObject3DSW *p_object_B, int subindex_B, void *pairdata) {
|
2017-03-05 16:44:50 +01:00
|
|
|
BroadPhaseOctree *bpo = (BroadPhaseOctree *)(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
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhaseOctree::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
|
|
|
|
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
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhaseOctree::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
|
|
|
|
unpair_callback = p_unpair_callback;
|
|
|
|
unpair_userdata = p_userdata;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BroadPhaseOctree::update() {
|
|
|
|
// does.. not?
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
BroadPhase3DSW *BroadPhaseOctree::_create() {
|
2017-03-05 16:44:50 +01:00
|
|
|
return memnew(BroadPhaseOctree);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BroadPhaseOctree::BroadPhaseOctree() {
|
2017-03-05 16:44:50 +01:00
|
|
|
octree.set_pair_callback(_pair_callback, this);
|
|
|
|
octree.set_unpair_callback(_unpair_callback, this);
|
2020-04-02 01:20:12 +02:00
|
|
|
pair_callback = nullptr;
|
|
|
|
pair_userdata = nullptr;
|
|
|
|
unpair_userdata = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|