Path: blob/master/modules/godot_physics_3d/godot_broad_phase_3d_bvh.cpp
10277 views
/**************************************************************************/1/* godot_broad_phase_3d_bvh.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 "godot_broad_phase_3d_bvh.h"3132#include "godot_collision_object_3d.h"3334GodotBroadPhase3DBVH::ID GodotBroadPhase3DBVH::create(GodotCollisionObject3D *p_object, int p_subindex, const AABB &p_aabb, bool p_static) {35uint32_t tree_id = p_static ? TREE_STATIC : TREE_DYNAMIC;36uint32_t tree_collision_mask = p_static ? TREE_FLAG_DYNAMIC : (TREE_FLAG_STATIC | TREE_FLAG_DYNAMIC);37ID oid = bvh.create(p_object, true, tree_id, tree_collision_mask, p_aabb, p_subindex); // Pair everything, don't care?38return oid + 1;39}4041void GodotBroadPhase3DBVH::move(ID p_id, const AABB &p_aabb) {42ERR_FAIL_COND(!p_id);43bvh.move(p_id - 1, p_aabb);44}4546void GodotBroadPhase3DBVH::set_static(ID p_id, bool p_static) {47ERR_FAIL_COND(!p_id);48uint32_t tree_id = p_static ? TREE_STATIC : TREE_DYNAMIC;49uint32_t tree_collision_mask = p_static ? TREE_FLAG_DYNAMIC : (TREE_FLAG_STATIC | TREE_FLAG_DYNAMIC);50bvh.set_tree(p_id - 1, tree_id, tree_collision_mask, false);51}5253void GodotBroadPhase3DBVH::remove(ID p_id) {54ERR_FAIL_COND(!p_id);55bvh.erase(p_id - 1);56}5758GodotCollisionObject3D *GodotBroadPhase3DBVH::get_object(ID p_id) const {59ERR_FAIL_COND_V(!p_id, nullptr);60GodotCollisionObject3D *it = bvh.get(p_id - 1);61ERR_FAIL_NULL_V(it, nullptr);62return it;63}6465bool GodotBroadPhase3DBVH::is_static(ID p_id) const {66ERR_FAIL_COND_V(!p_id, false);67uint32_t tree_id = bvh.get_tree_id(p_id - 1);68return tree_id == 0;69}7071int GodotBroadPhase3DBVH::get_subindex(ID p_id) const {72ERR_FAIL_COND_V(!p_id, 0);73return bvh.get_subindex(p_id - 1);74}7576int GodotBroadPhase3DBVH::cull_point(const Vector3 &p_point, GodotCollisionObject3D **p_results, int p_max_results, int *p_result_indices) {77return bvh.cull_point(p_point, p_results, p_max_results, nullptr, 0xFFFFFFFF, p_result_indices);78}7980int GodotBroadPhase3DBVH::cull_segment(const Vector3 &p_from, const Vector3 &p_to, GodotCollisionObject3D **p_results, int p_max_results, int *p_result_indices) {81return bvh.cull_segment(p_from, p_to, p_results, p_max_results, nullptr, 0xFFFFFFFF, p_result_indices);82}8384int GodotBroadPhase3DBVH::cull_aabb(const AABB &p_aabb, GodotCollisionObject3D **p_results, int p_max_results, int *p_result_indices) {85return bvh.cull_aabb(p_aabb, p_results, p_max_results, nullptr, 0xFFFFFFFF, p_result_indices);86}8788void *GodotBroadPhase3DBVH::_pair_callback(void *self, uint32_t p_A, GodotCollisionObject3D *p_object_A, int subindex_A, uint32_t p_B, GodotCollisionObject3D *p_object_B, int subindex_B) {89GodotBroadPhase3DBVH *bpo = static_cast<GodotBroadPhase3DBVH *>(self);90if (!bpo->pair_callback) {91return nullptr;92}9394return bpo->pair_callback(p_object_A, subindex_A, p_object_B, subindex_B, bpo->pair_userdata);95}9697void GodotBroadPhase3DBVH::_unpair_callback(void *self, uint32_t p_A, GodotCollisionObject3D *p_object_A, int subindex_A, uint32_t p_B, GodotCollisionObject3D *p_object_B, int subindex_B, void *pairdata) {98GodotBroadPhase3DBVH *bpo = static_cast<GodotBroadPhase3DBVH *>(self);99if (!bpo->unpair_callback) {100return;101}102103bpo->unpair_callback(p_object_A, subindex_A, p_object_B, subindex_B, pairdata, bpo->unpair_userdata);104}105106void GodotBroadPhase3DBVH::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {107pair_callback = p_pair_callback;108pair_userdata = p_userdata;109}110111void GodotBroadPhase3DBVH::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {112unpair_callback = p_unpair_callback;113unpair_userdata = p_userdata;114}115116void GodotBroadPhase3DBVH::update() {117bvh.update();118}119120GodotBroadPhase3D *GodotBroadPhase3DBVH::_create() {121return memnew(GodotBroadPhase3DBVH);122}123124GodotBroadPhase3DBVH::GodotBroadPhase3DBVH() {125bvh.set_pair_callback(_pair_callback, this);126bvh.set_unpair_callback(_unpair_callback, this);127}128129130