Path: blob/master/modules/jolt_physics/spaces/jolt_query_collectors.h
10278 views
/**************************************************************************/1/* jolt_query_collectors.h */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#pragma once3132#include "../jolt_project_settings.h"33#include "jolt_space_3d.h"3435#include "Jolt/Jolt.h"3637#include "Jolt/Core/STLLocalAllocator.h"38#include "Jolt/Physics/Collision/InternalEdgeRemovingCollector.h"39#include "Jolt/Physics/Collision/Shape/Shape.h"4041template <typename TBase, int TDefaultCapacity>42class JoltQueryCollectorAll final : public TBase {43public:44typedef typename TBase::ResultType Hit;45typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;4647private:48HitArray hits;4950public:51JoltQueryCollectorAll() {52hits.reserve(TDefaultCapacity);53}5455bool had_hit() const {56return !hits.is_empty();57}5859int get_hit_count() const {60return hits.size();61}6263const Hit &get_hit(int p_index) const {64return hits[p_index];65}6667void reset() { Reset(); }6869virtual void Reset() override {70TBase::Reset();71hits.clear();72}7374virtual void AddHit(const Hit &p_hit) override {75hits.push_back(p_hit);76}77};7879template <typename TBase>80class JoltQueryCollectorAny final : public TBase {81public:82typedef typename TBase::ResultType Hit;8384private:85Hit hit;86bool valid = false;8788public:89bool had_hit() const { return valid; }9091const Hit &get_hit() const { return hit; }9293void reset() {94Reset();95}9697virtual void Reset() override {98TBase::Reset();99valid = false;100}101102virtual void AddHit(const Hit &p_hit) override {103hit = p_hit;104valid = true;105106TBase::ForceEarlyOut();107}108};109110template <typename TBase, int TDefaultCapacity>111class JoltQueryCollectorAnyMulti final : public TBase {112public:113typedef typename TBase::ResultType Hit;114typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;115116private:117HitArray hits;118int max_hits = 0;119120public:121explicit JoltQueryCollectorAnyMulti(int p_max_hits = TDefaultCapacity) :122max_hits(p_max_hits) {123hits.reserve(TDefaultCapacity);124}125126bool had_hit() const {127return hits.size() > 0;128}129130int get_hit_count() const {131return hits.size();132}133134const Hit &get_hit(int p_index) const {135return hits[p_index];136}137138void reset() {139Reset();140}141142virtual void Reset() override {143TBase::Reset();144hits.clear();145}146147virtual void AddHit(const Hit &p_hit) override {148if ((int)hits.size() < max_hits) {149hits.push_back(p_hit);150}151152if ((int)hits.size() == max_hits) {153TBase::ForceEarlyOut();154}155}156};157158template <typename TBase>159class JoltQueryCollectorClosest final : public TBase {160public:161typedef typename TBase::ResultType Hit;162163private:164Hit hit;165bool valid = false;166167public:168bool had_hit() const { return valid; }169170const Hit &get_hit() const { return hit; }171172void reset() {173Reset();174}175176virtual void Reset() override {177TBase::Reset();178valid = false;179}180181virtual void AddHit(const Hit &p_hit) override {182const float early_out = p_hit.GetEarlyOutFraction();183184if (!valid || early_out < hit.GetEarlyOutFraction()) {185TBase::UpdateEarlyOutFraction(early_out);186187hit = p_hit;188valid = true;189}190}191};192193template <typename TBase, int TDefaultCapacity>194class JoltQueryCollectorClosestMulti final : public TBase {195public:196typedef typename TBase::ResultType Hit;197typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity + 1>> HitArray;198199private:200HitArray hits;201int max_hits = 0;202203public:204explicit JoltQueryCollectorClosestMulti(int p_max_hits = TDefaultCapacity) :205max_hits(p_max_hits) {206hits.reserve(TDefaultCapacity + 1);207}208209bool had_hit() const {210return hits.size() > 0;211}212213int get_hit_count() const {214return hits.size();215}216217const Hit &get_hit(int p_index) const {218return hits[p_index];219}220221void reset() {222Reset();223}224225virtual void Reset() override {226TBase::Reset();227hits.clear();228}229230virtual void AddHit(const Hit &p_hit) override {231typename HitArray::const_iterator E = hits.cbegin();232for (; E != hits.cend(); ++E) {233if (p_hit.GetEarlyOutFraction() < E->GetEarlyOutFraction()) {234break;235}236}237238hits.insert(E, p_hit);239240if ((int)hits.size() > max_hits) {241hits.resize(max_hits);242}243}244};245246247