virtualx-engine/servers/physics_2d/broad_phase_2d_basic.cpp

175 lines
5.9 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* broad_phase_2d_basic.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 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. */
/*************************************************************************/
2014-02-10 02:10:30 +01:00
#include "broad_phase_2d_basic.h"
BroadPhase2DBasic::ID BroadPhase2DBasic::create(CollisionObject2DSW *p_object_, int p_subindex) {
2014-02-10 02:10:30 +01:00
current++;
Element e;
e.owner = p_object_;
e._static = false;
e.subindex = p_subindex;
2014-02-10 02:10:30 +01:00
element_map[current] = e;
2014-02-10 02:10:30 +01:00
return current;
}
void BroadPhase2DBasic::move(ID p_id, const Rect2 &p_aabb) {
Map<ID, Element>::Element *E = element_map.find(p_id);
2014-02-10 02:10:30 +01:00
ERR_FAIL_COND(!E);
E->get().aabb = p_aabb;
2014-02-10 02:10:30 +01:00
}
2014-02-10 02:10:30 +01:00
void BroadPhase2DBasic::set_static(ID p_id, bool p_static) {
Map<ID, Element>::Element *E = element_map.find(p_id);
2014-02-10 02:10:30 +01:00
ERR_FAIL_COND(!E);
E->get()._static = p_static;
2014-02-10 02:10:30 +01:00
}
2014-02-10 02:10:30 +01:00
void BroadPhase2DBasic::remove(ID p_id) {
Map<ID, Element>::Element *E = element_map.find(p_id);
2014-02-10 02:10:30 +01:00
ERR_FAIL_COND(!E);
element_map.erase(E);
}
CollisionObject2DSW *BroadPhase2DBasic::get_object(ID p_id) const {
const Map<ID, Element>::Element *E = element_map.find(p_id);
2020-04-02 01:20:12 +02:00
ERR_FAIL_COND_V(!E, nullptr);
2014-02-10 02:10:30 +01:00
return E->get().owner;
}
2014-02-10 02:10:30 +01:00
bool BroadPhase2DBasic::is_static(ID p_id) const {
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, false);
2014-02-10 02:10:30 +01:00
return E->get()._static;
}
2014-02-10 02:10:30 +01:00
int BroadPhase2DBasic::get_subindex(ID p_id) const {
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, -1);
2014-02-10 02:10:30 +01:00
return E->get().subindex;
}
int BroadPhase2DBasic::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
int rc = 0;
2014-02-10 02:10:30 +01:00
for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
const Rect2 aabb = E->get().aabb;
if (aabb.intersects_segment(p_from, p_to)) {
p_results[rc] = E->get().owner;
p_result_indices[rc] = E->get().subindex;
2014-02-10 02:10:30 +01:00
rc++;
if (rc >= p_max_results) {
2014-02-10 02:10:30 +01:00
break;
}
2014-02-10 02:10:30 +01:00
}
}
return rc;
}
int BroadPhase2DBasic::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
int rc = 0;
2014-02-10 02:10:30 +01:00
for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
const Rect2 aabb = E->get().aabb;
2014-02-10 02:10:30 +01:00
if (aabb.intersects(p_aabb)) {
p_results[rc] = E->get().owner;
p_result_indices[rc] = E->get().subindex;
2014-02-10 02:10:30 +01:00
rc++;
if (rc >= p_max_results) {
2014-02-10 02:10:30 +01:00
break;
}
2014-02-10 02:10:30 +01:00
}
}
return rc;
}
void BroadPhase2DBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
pair_userdata = p_userdata;
pair_callback = p_pair_callback;
2014-02-10 02:10:30 +01:00
}
void BroadPhase2DBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
unpair_userdata = p_userdata;
unpair_callback = p_unpair_callback;
2014-02-10 02:10:30 +01:00
}
void BroadPhase2DBasic::update() {
// recompute pairs
for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
Element *elem_A = &I->get();
Element *elem_B = &J->get();
2014-02-10 02:10:30 +01:00
if (elem_A->owner == elem_B->owner) {
2014-02-10 02:10:30 +01:00
continue;
}
2014-02-10 02:10:30 +01:00
bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
2014-02-10 02:10:30 +01:00
PairKey key(I->key(), J->key());
2014-02-10 02:10:30 +01:00
Map<PairKey, void *>::Element *E = pair_map.find(key);
2014-02-10 02:10:30 +01:00
if (!pair_ok && E) {
if (unpair_callback) {
unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
}
2014-02-10 02:10:30 +01:00
pair_map.erase(key);
}
if (pair_ok && !E) {
2020-04-02 01:20:12 +02:00
void *data = nullptr;
if (pair_callback) {
data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, unpair_userdata);
if (data) {
pair_map.insert(key, data);
}
}
2014-02-10 02:10:30 +01:00
}
}
}
}
BroadPhase2DSW *BroadPhase2DBasic::_create() {
return memnew(BroadPhase2DBasic);
2014-02-10 02:10:30 +01:00
}
BroadPhase2DBasic::BroadPhase2DBasic() {
current = 1;
2020-04-02 01:20:12 +02:00
unpair_callback = nullptr;
unpair_userdata = nullptr;
pair_callback = nullptr;
pair_userdata = nullptr;
2014-02-10 02:10:30 +01:00
}