2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* broad_phase_2d_hash_grid.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
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "broad_phase_2d_hash_grid.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/project_settings.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 22:20:57 +02:00
|
|
|
#define LARGE_ELEMENT_FI 1.01239812
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhase2DHashGrid::_pair_attempt(Element *p_elem, Element *p_with) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<Element *, PairData *>::Element *E = p_elem->paired.find(p_with);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(p_elem->_static && p_with->_static);
|
|
|
|
|
|
|
|
if (!E) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PairData *pd = memnew(PairData);
|
|
|
|
p_elem->paired[p_with] = pd;
|
|
|
|
p_with->paired[p_elem] = pd;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
E->get()->rc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhase2DHashGrid::_unpair_attempt(Element *p_elem, Element *p_with) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<Element *, PairData *>::Element *E = p_elem->paired.find(p_with);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!E); //this should really be paired..
|
|
|
|
|
|
|
|
E->get()->rc--;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->get()->rc == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (E->get()->colliding) {
|
|
|
|
//uncollide
|
|
|
|
if (unpair_callback) {
|
2017-03-05 16:44:50 +01:00
|
|
|
unpair_callback(p_elem->owner, p_elem->subindex, p_with->owner, p_with->subindex, E->get()->ud, unpair_userdata);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(E->get());
|
|
|
|
p_elem->paired.erase(E);
|
|
|
|
p_with->paired.erase(p_elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BroadPhase2DHashGrid::_check_motion(Element *p_elem) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, PairData *>::Element *E = p_elem->paired.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool pairing = p_elem->aabb.intersects(E->key()->aabb);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pairing != E->get()->colliding) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (pairing) {
|
|
|
|
|
|
|
|
if (pair_callback) {
|
2017-03-05 16:44:50 +01:00
|
|
|
E->get()->ud = pair_callback(p_elem->owner, p_elem->subindex, E->key()->owner, E->key()->subindex, pair_userdata);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (unpair_callback) {
|
2017-03-05 16:44:50 +01:00
|
|
|
unpair_callback(p_elem->owner, p_elem->subindex, E->key()->owner, E->key()->subindex, E->get()->ud, unpair_userdata);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
E->get()->colliding = pairing;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhase2DHashGrid::_enter_grid(Element *p_elem, const Rect2 &p_rect, bool p_static) {
|
2016-06-18 22:20:57 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 sz = (p_rect.size / cell_size * LARGE_ELEMENT_FI); //use magic number to avoid floating point issues
|
|
|
|
if (sz.width * sz.height > large_object_min_surface) {
|
2016-06-18 22:20:57 +02:00
|
|
|
//large object, do not use grid, must check against all elements
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
|
|
|
|
if (E->key() == p_elem->self)
|
2016-06-18 22:20:57 +02:00
|
|
|
continue; // do not pair against itself
|
|
|
|
if (E->get().owner == p_elem->owner)
|
|
|
|
continue;
|
|
|
|
if (E->get()._static && p_static)
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_pair_attempt(p_elem, &E->get());
|
2016-06-18 22:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
large_elements[p_elem].inc();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-04 00:25:13 +02:00
|
|
|
Point2i from = (p_rect.position / cell_size).floor();
|
|
|
|
Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = from.x; i <= to.x; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = from.y; j <= to.y; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
PosKey pk;
|
2017-03-05 16:44:50 +01:00
|
|
|
pk.x = i;
|
|
|
|
pk.y = j;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint32_t idx = pk.hash() % hash_table_size;
|
|
|
|
PosBin *pb = hash_table[idx];
|
|
|
|
|
|
|
|
while (pb) {
|
|
|
|
|
|
|
|
if (pb->key == pk) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pb = pb->next;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool entered = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!pb) {
|
|
|
|
//does not exist, create!
|
2017-03-05 16:44:50 +01:00
|
|
|
pb = memnew(PosBin);
|
|
|
|
pb->key = pk;
|
|
|
|
pb->next = hash_table[idx];
|
|
|
|
hash_table[idx] = pb;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pb->static_object_set[p_elem].inc() == 1) {
|
|
|
|
entered = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pb->object_set[p_elem].inc() == 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
entered = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entered) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->owner == p_elem->owner)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2017-03-05 16:44:50 +01:00
|
|
|
_pair_attempt(p_elem, E->key());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!p_static) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->owner == p_elem->owner)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2017-03-05 16:44:50 +01:00
|
|
|
_pair_attempt(p_elem, E->key());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 22:20:57 +02:00
|
|
|
//pair separatedly with large elements
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
|
2016-06-18 22:20:57 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key() == p_elem)
|
2016-06-18 22:20:57 +02:00
|
|
|
continue; // do not pair against itself
|
|
|
|
if (E->key()->owner == p_elem->owner)
|
|
|
|
continue;
|
|
|
|
if (E->key()->_static && p_static)
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_pair_attempt(E->key(), p_elem);
|
2016-06-18 22:20:57 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhase2DHashGrid::_exit_grid(Element *p_elem, const Rect2 &p_rect, bool p_static) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 sz = (p_rect.size / cell_size * LARGE_ELEMENT_FI);
|
|
|
|
if (sz.width * sz.height > large_object_min_surface) {
|
2016-06-18 22:20:57 +02:00
|
|
|
|
|
|
|
//unpair all elements, instead of checking all, just check what is already paired, so we at least save from checking static vs static
|
2017-07-25 04:10:29 +02:00
|
|
|
Map<Element *, PairData *>::Element *E = p_elem->paired.front();
|
|
|
|
while (E) {
|
|
|
|
Map<Element *, PairData *>::Element *next = E->next();
|
2017-03-05 16:44:50 +01:00
|
|
|
_unpair_attempt(p_elem, E->key());
|
2017-07-25 04:10:29 +02:00
|
|
|
E = next;
|
2016-06-18 22:20:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (large_elements[p_elem].dec() == 0) {
|
2016-06-18 22:20:57 +02:00
|
|
|
large_elements.erase(p_elem);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-04 00:25:13 +02:00
|
|
|
Point2i from = (p_rect.position / cell_size).floor();
|
|
|
|
Point2i to = ((p_rect.position + p_rect.size) / cell_size).floor();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = from.x; i <= to.x; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = from.y; j <= to.y; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
PosKey pk;
|
2017-03-05 16:44:50 +01:00
|
|
|
pk.x = i;
|
|
|
|
pk.y = j;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint32_t idx = pk.hash() % hash_table_size;
|
|
|
|
PosBin *pb = hash_table[idx];
|
|
|
|
|
|
|
|
while (pb) {
|
|
|
|
|
|
|
|
if (pb->key == pk) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pb = pb->next;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_CONTINUE(!pb); //should exist!!
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool exited = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (p_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pb->static_object_set[p_elem].dec() == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
pb->static_object_set.erase(p_elem);
|
2017-03-05 16:44:50 +01:00
|
|
|
exited = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pb->object_set[p_elem].dec() == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
pb->object_set.erase(p_elem);
|
2017-03-05 16:44:50 +01:00
|
|
|
exited = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exited) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->owner == p_elem->owner)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2017-03-05 16:44:50 +01:00
|
|
|
_unpair_attempt(p_elem, E->key());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!p_static) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->owner == p_elem->owner)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2017-03-05 16:44:50 +01:00
|
|
|
_unpair_attempt(p_elem, E->key());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pb->object_set.empty() && pb->static_object_set.empty()) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (hash_table[idx] == pb) {
|
|
|
|
hash_table[idx] = pb->next;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
PosBin *px = hash_table[idx];
|
|
|
|
|
|
|
|
while (px) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (px->next == pb) {
|
|
|
|
px->next = pb->next;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
px = px->next;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_CONTINUE(!px);
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(pb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
|
|
|
|
if (E->key() == p_elem)
|
2016-06-18 22:20:57 +02:00
|
|
|
continue; // do not pair against itself
|
|
|
|
if (E->key()->owner == p_elem->owner)
|
|
|
|
continue;
|
|
|
|
if (E->key()->_static && p_static)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//unpair from large elements
|
2017-03-05 16:44:50 +01:00
|
|
|
_unpair_attempt(p_elem, E->key());
|
2016-06-18 22:20:57 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BroadPhase2DHashGrid::ID BroadPhase2DHashGrid::create(CollisionObject2DSW *p_object, int p_subindex) {
|
|
|
|
|
|
|
|
current++;
|
|
|
|
|
|
|
|
Element e;
|
2017-03-05 16:44:50 +01:00
|
|
|
e.owner = p_object;
|
|
|
|
e._static = false;
|
|
|
|
e.subindex = p_subindex;
|
|
|
|
e.self = current;
|
|
|
|
e.pass = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
element_map[current] = e;
|
2014-02-10 02:10:30 +01:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhase2DHashGrid::move(ID p_id, const Rect2 &p_aabb) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<ID, Element>::Element *E = element_map.find(p_id);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(!E);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element &e = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_aabb == e.aabb)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_aabb != Rect2()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_enter_grid(&e, p_aabb, e._static);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (e.aabb != Rect2()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_exit_grid(&e, e.aabb, e._static);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
e.aabb = p_aabb;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_check_motion(&e);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
e.aabb = p_aabb;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
void BroadPhase2DHashGrid::set_static(ID p_id, bool p_static) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<ID, Element>::Element *E = element_map.find(p_id);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(!E);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element &e = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (e._static == p_static)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (e.aabb != Rect2())
|
|
|
|
_exit_grid(&e, e.aabb, e._static);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
e._static = p_static;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (e.aabb != Rect2()) {
|
|
|
|
_enter_grid(&e, e.aabb, e._static);
|
2014-02-10 02:10:30 +01:00
|
|
|
_check_motion(&e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void BroadPhase2DHashGrid::remove(ID p_id) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<ID, Element>::Element *E = element_map.find(p_id);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(!E);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Element &e = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (e.aabb != Rect2())
|
|
|
|
_exit_grid(&e, e.aabb, e._static);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
element_map.erase(p_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
CollisionObject2DSW *BroadPhase2DHashGrid::get_object(ID p_id) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
bool BroadPhase2DHashGrid::is_static(ID p_id) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
int BroadPhase2DHashGrid::get_subindex(ID p_id) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool use_aabb, bool use_segment>
|
|
|
|
void BroadPhase2DHashGrid::_cull(const Point2i p_cell, const Rect2 &p_aabb, const Point2 &p_from, const Point2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices, int &index) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
PosKey pk;
|
2017-03-05 16:44:50 +01:00
|
|
|
pk.x = p_cell.x;
|
|
|
|
pk.y = p_cell.y;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint32_t idx = pk.hash() % hash_table_size;
|
|
|
|
PosBin *pb = hash_table[idx];
|
|
|
|
|
|
|
|
while (pb) {
|
|
|
|
|
|
|
|
if (pb->key == pk) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pb = pb->next;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pb)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = pb->object_set.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (index >= p_max_results)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->pass == pass)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
E->key()->pass = pass;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (use_aabb && !p_aabb.intersects(E->key()->aabb))
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (use_segment && !E->key()->aabb.intersects_segment(p_from, p_to))
|
2014-02-19 15:57:14 +01:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_results[index] = E->key()->owner;
|
|
|
|
p_result_indices[index] = E->key()->subindex;
|
2014-02-10 02:10:30 +01:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = pb->static_object_set.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (index >= p_max_results)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->pass == pass)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
|
2014-02-23 00:28:19 +01:00
|
|
|
if (use_aabb && !p_aabb.intersects(E->key()->aabb)) {
|
2014-02-19 15:57:14 +01:00
|
|
|
continue;
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (use_segment && !E->key()->aabb.intersects_segment(p_from, p_to))
|
2014-02-19 15:57:14 +01:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
E->key()->pass = pass;
|
|
|
|
p_results[index] = E->key()->owner;
|
|
|
|
p_result_indices[index] = E->key()->subindex;
|
2014-02-10 02:10:30 +01:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int BroadPhase2DHashGrid::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
pass++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 dir = (p_to - p_from);
|
|
|
|
if (dir == Vector2())
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
//avoid divisions by zero
|
|
|
|
dir.normalize();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (dir.x == 0.0)
|
|
|
|
dir.x = 0.000001;
|
|
|
|
if (dir.y == 0.0)
|
|
|
|
dir.y = 0.000001;
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector2 delta = dir.abs();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
delta.x = cell_size / delta.x;
|
|
|
|
delta.y = cell_size / delta.y;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point2i pos = (p_from / cell_size).floor();
|
|
|
|
Point2i end = (p_to / cell_size).floor();
|
2015-04-25 03:41:31 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point2i step = Vector2(SGN(dir.x), SGN(dir.y));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector2 max;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (dir.x < 0)
|
|
|
|
max.x = (Math::floor((double)pos.x) * cell_size - p_from.x) / dir.x;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
max.x = (Math::floor((double)pos.x + 1) * cell_size - p_from.x) / dir.x;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (dir.y < 0)
|
|
|
|
max.y = (Math::floor((double)pos.y) * cell_size - p_from.y) / dir.y;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
max.y = (Math::floor((double)pos.y + 1) * cell_size - p_from.y) / dir.y;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int cullcount = 0;
|
|
|
|
_cull<false, true>(pos, Rect2(), p_from, p_to, p_results, p_max_results, p_result_indices, cullcount);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool reached_x = false;
|
|
|
|
bool reached_y = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (max.x < max.y) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
max.x += delta.x;
|
|
|
|
pos.x += step.x;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
max.y += delta.y;
|
|
|
|
pos.y += step.y;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (step.x > 0) {
|
|
|
|
if (pos.x >= end.x)
|
|
|
|
reached_x = true;
|
|
|
|
} else if (pos.x <= end.x) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
reached_x = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (step.y > 0) {
|
|
|
|
if (pos.y >= end.y)
|
|
|
|
reached_y = true;
|
|
|
|
} else if (pos.y <= end.y) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
reached_y = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_cull<false, true>(pos, Rect2(), p_from, p_to, p_results, p_max_results, p_result_indices, cullcount);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (reached_x && reached_y)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
|
2016-06-18 22:20:57 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (cullcount >= p_max_results)
|
2016-06-18 22:20:57 +02:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->pass == pass)
|
2016-06-18 22:20:57 +02:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
E->key()->pass = pass;
|
2016-06-18 22:20:57 +02:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
if (use_aabb && !p_aabb.intersects(E->key()->aabb))
|
|
|
|
continue;
|
|
|
|
*/
|
2016-06-18 22:20:57 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!E->key()->aabb.intersects_segment(p_from, p_to))
|
2016-06-18 22:20:57 +02:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_results[cullcount] = E->key()->owner;
|
|
|
|
p_result_indices[cullcount] = E->key()->subindex;
|
2016-06-18 22:20:57 +02:00
|
|
|
cullcount++;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return cullcount;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int BroadPhase2DHashGrid::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
pass++;
|
|
|
|
|
2017-06-04 00:25:13 +02:00
|
|
|
Point2i from = (p_aabb.position / cell_size).floor();
|
|
|
|
Point2i to = ((p_aabb.position + p_aabb.size) / cell_size).floor();
|
2017-03-05 16:44:50 +01:00
|
|
|
int cullcount = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = from.x; i <= to.x; i++) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = from.y; j <= to.y; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_cull<true, false>(Point2i(i, j), p_aabb, Point2(), Point2(), p_results, p_max_results, p_result_indices, cullcount);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Element *, RC>::Element *E = large_elements.front(); E; E = E->next()) {
|
2016-06-18 22:20:57 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (cullcount >= p_max_results)
|
2016-06-18 22:20:57 +02:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (E->key()->pass == pass)
|
2016-06-18 22:20:57 +02:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
E->key()->pass = pass;
|
2016-06-18 22:20:57 +02:00
|
|
|
|
|
|
|
if (!p_aabb.intersects(E->key()->aabb))
|
|
|
|
continue;
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
if (!E->key()->aabb.intersects_segment(p_from,p_to))
|
|
|
|
continue;
|
|
|
|
*/
|
2016-06-18 22:20:57 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_results[cullcount] = E->key()->owner;
|
|
|
|
p_result_indices[cullcount] = E->key()->subindex;
|
2016-06-18 22:20:57 +02:00
|
|
|
cullcount++;
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
return cullcount;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhase2DHashGrid::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
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
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
void BroadPhase2DHashGrid::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void BroadPhase2DHashGrid::update() {
|
|
|
|
}
|
|
|
|
|
|
|
|
BroadPhase2DSW *BroadPhase2DHashGrid::_create() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return memnew(BroadPhase2DHashGrid);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BroadPhase2DHashGrid::BroadPhase2DHashGrid() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hash_table_size = GLOBAL_DEF("physics/2d/bp_hash_table_size", 4096);
|
2018-10-05 18:43:53 +02:00
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/bp_hash_table_size", PropertyInfo(Variant::INT, "physics/2d/bp_hash_table_size", PROPERTY_HINT_RANGE, "0,8192,1,or_greater"));
|
2014-02-10 02:10:30 +01:00
|
|
|
hash_table_size = Math::larger_prime(hash_table_size);
|
2017-03-05 16:44:50 +01:00
|
|
|
hash_table = memnew_arr(PosBin *, hash_table_size);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
cell_size = GLOBAL_DEF("physics/2d/cell_size", 128);
|
2018-10-05 18:43:53 +02:00
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/cell_size", PropertyInfo(Variant::INT, "physics/2d/cell_size", PROPERTY_HINT_RANGE, "0,512,1,or_greater"));
|
|
|
|
|
2017-07-08 17:12:18 +02:00
|
|
|
large_object_min_surface = GLOBAL_DEF("physics/2d/large_object_surface_threshold_in_cells", 512);
|
2018-10-05 18:43:53 +02:00
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/large_object_surface_threshold_in_cells", PropertyInfo(Variant::INT, "physics/2d/large_object_surface_threshold_in_cells", PROPERTY_HINT_RANGE, "0,1024,1,or_greater"));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-31 23:30:35 +02:00
|
|
|
for (uint32_t i = 0; i < hash_table_size; i++)
|
2020-04-02 01:20:12 +02:00
|
|
|
hash_table[i] = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
pass = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
current = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BroadPhase2DHashGrid::~BroadPhase2DHashGrid() {
|
|
|
|
|
2017-08-31 23:30:35 +02:00
|
|
|
for (uint32_t i = 0; i < hash_table_size; i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (hash_table[i]) {
|
|
|
|
PosBin *pb = hash_table[i];
|
|
|
|
hash_table[i] = pb->next;
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(pb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete_arr(hash_table);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 3D version of voxel traversal:
|
|
|
|
|
|
|
|
public IEnumerable<Point3D> GetCellsOnRay(Ray ray, int maxDepth)
|
|
|
|
{
|
|
|
|
// Implementation is based on:
|
|
|
|
// "A Fast Voxel Traversal Algorithm for Ray Tracing"
|
|
|
|
// John Amanatides, Andrew Woo
|
|
|
|
// http://www.cse.yorku.ca/~amana/research/grid.pdf
|
2019-07-24 02:06:12 +02:00
|
|
|
// https://web.archive.org/web/20100616193049/http://www.devmaster.net/articles/raytracing_series/A%20faster%20voxel%20traversal%20algorithm%20for%20ray%20tracing.pdf
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// NOTES:
|
|
|
|
// * This code assumes that the ray's position and direction are in 'cell coordinates', which means
|
|
|
|
// that one unit equals one cell in all directions.
|
|
|
|
// * When the ray doesn't start within the voxel grid, calculate the first position at which the
|
|
|
|
// ray could enter the grid. If it never enters the grid, there is nothing more to do here.
|
|
|
|
// * Also, it is important to test when the ray exits the voxel grid when the grid isn't infinite.
|
|
|
|
// * The Point3D structure is a simple structure having three integer fields (X, Y and Z).
|
|
|
|
|
|
|
|
// The cell in which the ray starts.
|
|
|
|
Point3D start = GetCellAt(ray.Position); // Rounds the position's X, Y and Z down to the nearest integer values.
|
|
|
|
int x = start.X;
|
|
|
|
int y = start.Y;
|
|
|
|
int z = start.Z;
|
|
|
|
|
|
|
|
// Determine which way we go.
|
|
|
|
int stepX = Math.Sign(ray.Direction.X);
|
|
|
|
int stepY = Math.Sign(ray.Direction.Y);
|
|
|
|
int stepZ = Math.Sign(ray.Direction.Z);
|
|
|
|
|
|
|
|
// Calculate cell boundaries. When the step (i.e. direction sign) is positive,
|
|
|
|
// the next boundary is AFTER our current position, meaning that we have to add 1.
|
|
|
|
// Otherwise, it is BEFORE our current position, in which case we add nothing.
|
|
|
|
Point3D cellBoundary = new Point3D(
|
|
|
|
x + (stepX > 0 ? 1 : 0),
|
|
|
|
y + (stepY > 0 ? 1 : 0),
|
|
|
|
z + (stepZ > 0 ? 1 : 0));
|
|
|
|
|
|
|
|
// NOTE: For the following calculations, the result will be Single.PositiveInfinity
|
|
|
|
// when ray.Direction.X, Y or Z equals zero, which is OK. However, when the left-hand
|
|
|
|
// value of the division also equals zero, the result is Single.NaN, which is not OK.
|
|
|
|
|
|
|
|
// Determine how far we can travel along the ray before we hit a voxel boundary.
|
|
|
|
Vector3 tMax = new Vector3(
|
|
|
|
(cellBoundary.X - ray.Position.X) / ray.Direction.X, // Boundary is a plane on the YZ axis.
|
|
|
|
(cellBoundary.Y - ray.Position.Y) / ray.Direction.Y, // Boundary is a plane on the XZ axis.
|
|
|
|
(cellBoundary.Z - ray.Position.Z) / ray.Direction.Z); // Boundary is a plane on the XY axis.
|
|
|
|
if (Single.IsNaN(tMax.X)) tMax.X = Single.PositiveInfinity;
|
|
|
|
if (Single.IsNaN(tMax.Y)) tMax.Y = Single.PositiveInfinity;
|
|
|
|
if (Single.IsNaN(tMax.Z)) tMax.Z = Single.PositiveInfinity;
|
|
|
|
|
|
|
|
// Determine how far we must travel along the ray before we have crossed a gridcell.
|
|
|
|
Vector3 tDelta = new Vector3(
|
|
|
|
stepX / ray.Direction.X, // Crossing the width of a cell.
|
|
|
|
stepY / ray.Direction.Y, // Crossing the height of a cell.
|
|
|
|
stepZ / ray.Direction.Z); // Crossing the depth of a cell.
|
|
|
|
if (Single.IsNaN(tDelta.X)) tDelta.X = Single.PositiveInfinity;
|
|
|
|
if (Single.IsNaN(tDelta.Y)) tDelta.Y = Single.PositiveInfinity;
|
|
|
|
if (Single.IsNaN(tDelta.Z)) tDelta.Z = Single.PositiveInfinity;
|
|
|
|
|
|
|
|
// For each step, determine which distance to the next voxel boundary is lowest (i.e.
|
|
|
|
// which voxel boundary is nearest) and walk that way.
|
|
|
|
for (int i = 0; i < maxDepth; i++)
|
|
|
|
{
|
|
|
|
// Return it.
|
|
|
|
yield return new Point3D(x, y, z);
|
|
|
|
|
|
|
|
// Do the next step.
|
|
|
|
if (tMax.X < tMax.Y && tMax.X < tMax.Z)
|
|
|
|
{
|
|
|
|
// tMax.X is the lowest, an YZ cell boundary plane is nearest.
|
|
|
|
x += stepX;
|
|
|
|
tMax.X += tDelta.X;
|
|
|
|
}
|
|
|
|
else if (tMax.Y < tMax.Z)
|
|
|
|
{
|
|
|
|
// tMax.Y is the lowest, an XZ cell boundary plane is nearest.
|
|
|
|
y += stepY;
|
|
|
|
tMax.Y += tDelta.Y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// tMax.Z is the lowest, an XY cell boundary plane is nearest.
|
|
|
|
z += stepZ;
|
|
|
|
tMax.Z += tDelta.Z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|