载入中...
搜索中...
未找到
World3DQueries.cpp
浏览该文件的文档.
1// eve-hot-path
3
4#include <algorithm>
5#include <cfloat>
6#include <cmath>
7#include <utility>
8
9namespace eve::physics {
10using namespace world3d_detail;
11
12bool World3D::sphereCast(float x1, float y1, float z1, float x2, float y2, float z2, float radius, uint64_t maskBits,
13 int ignoredBodyId, CameraSphereHit3D *out) const {
14 if (!out) return false;
15 *out = worldValue(rigidBackend_->sphereCast(x1, y1, z1, x2, y2, z2, radius, maskBits, ignoredBodyId));
16 return out->hit;
17}
18
19
20int World3D::rayCast(float x1, float y1, float z1, float x2, float y2, float z2) {
21 rayCastAll(x1, y1, z1, x2, y2, z2, 1);
22 return rayHitBodyId_;
23}
24
25int World3D::rayCastFiltered(float x1, float y1, float z1, float x2, float y2, float z2, uint64_t maskBits) {
26 return worldValue(rigidBackend_->rayCastFiltered(x1, y1, z1, x2, y2, z2, maskBits));
27}
28
29
30eve::Result<RayHit3D> World3D::rayCastOwned(float x1, float y1, float z1, float x2, float y2, float z2,
32 return rigidBackend_->rayCastOwned(x1, y1, z1, x2, y2, z2, filter);
33}
34
35
36int World3D::rayCastAll(float x1, float y1, float z1, float x2, float y2, float z2, int maxHits) {
37 return worldValue(rigidBackend_->rayCastAll(x1, y1, z1, x2, y2, z2, maxHits));
38}
39
40
41int World3D::queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
42 return worldValue(rigidBackend_->queryAABB(minX, minY, minZ, maxX, maxY, maxZ));
43}
44
45
46eve::Result<BroadPhaseAabb3D> World3D::queryAabbBroadPhaseOwned(float minX, float minY, float minZ, float maxX,
47 float maxY, float maxZ, QueryFilter3D filter) {
48 return rigidBackend_->queryAabbBroadPhaseOwned(minX, minY, minZ, maxX, maxY, maxZ, filter);
49}
50
51
52int World3D::querySphere(float x, float y, float z, float radius) {
53 return worldValue(rigidBackend_->querySphere(x, y, z, radius));
54}
55
56
57int World3D::queryCapsule(float ax, float ay, float az, float bx, float by, float bz, float radius) {
58 return worldValue(rigidBackend_->queryCapsule(ax, ay, az, bx, by, bz, radius));
59}
60
61
62eve::Result<CapsuleOverlap3D> World3D::queryCapsuleOwned(float ax, float ay, float az, float bx, float by, float bz,
64 return rigidBackend_->queryCapsuleOwned(ax, ay, az, bx, by, bz, radius, filter);
65}
66
67
68int World3D::queryBox(float x, float y, float z, float width, float height, float depth, float qx, float qy, float qz,
69 float qw) {
70 return worldValue(rigidBackend_->queryBox(x, y, z, width, height, depth, qx, qy, qz, qw));
71}
72
73
74int World3D::castSphere(float x, float y, float z, float radius, float dx, float dy, float dz) {
75 castSphereAll(x, y, z, radius, dx, dy, dz, 1);
76 return shapeCastBodyId_;
77}
78
79int World3D::castSphereAll(float x, float y, float z, float radius, float dx, float dy, float dz, int maxHits) {
80 return worldValue(rigidBackend_->castSphereAll(x, y, z, radius, dx, dy, dz, maxHits));
81}
82
83
84int World3D::castCapsule(float ax, float ay, float az, float bx, float by, float bz, float radius, float dx, float dy,
85 float dz) {
86 castCapsuleAll(ax, ay, az, bx, by, bz, radius, dx, dy, dz, 1);
87 return shapeCastBodyId_;
88}
89
90int World3D::castCapsuleAll(float ax, float ay, float az, float bx, float by, float bz, float radius, float dx,
91 float dy, float dz, int maxHits) {
92 return worldValue(rigidBackend_->castCapsuleAll(ax, ay, az, bx, by, bz, radius, dx, dy, dz, maxHits));
93}
94
95
96int World3D::castBox(float x, float y, float z, float width, float height, float depth, float qx, float qy, float qz,
97 float qw, float dx, float dy, float dz) {
98 castBoxAll(x, y, z, width, height, depth, qx, qy, qz, qw, dx, dy, dz, 1);
99 return shapeCastBodyId_;
100}
101
102int World3D::castBoxAll(float x, float y, float z, float width, float height, float depth, float qx, float qy, float qz,
103 float qw, float dx, float dy, float dz, int maxHits) {
104 return worldValue(rigidBackend_->castBoxAll(x, y, z, width, height, depth, qx, qy, qz, qw, dx, dy, dz, maxHits));
105}
106
107
108int World3D::closestPoint(float x, float y, float z, float maxDistance) {
109 return worldValue(rigidBackend_->closestPoint(x, y, z, maxDistance));
110}
111
112
113} // namespace eve::physics
114
115namespace eve::physics {
117 double endY, double endZ,
120 const double values[] = {x, y, z, endX, endY, endZ, endX - x, endY - y, endZ - z};
121 for (double value : values)
122 if (!std::isfinite(value) || std::abs(value) > FLT_MAX)
124 "Sweep coordinates exceed the finite physics float domain",
125 "motion"));
126 if (!isValid() || !std::isfinite(settings.radius) || settings.radius <= 0 || settings.ignoredBodyId < -1)
128 "Sphere sweep requires a live world and valid radius/filter",
129 "worldSweep"));
131 {settings.categoryBits, settings.maskBits, settings.ignoredBodyId, -1});
132 auto cast = rigidBackend_->castSphereAll(static_cast<float>(x), static_cast<float>(y), static_cast<float>(z),
133 settings.radius, static_cast<float>(endX - x),
134 static_cast<float>(endY - y), static_cast<float>(endZ - z), 257);
135 if (!cast) return Output::failure(cast.status());
136 if (cast.value() > 256)
137 return Output::failure(
138 eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "Sphere sweep contact budget exceeded", "contacts"));
139 std::vector<eve::WorldSphereContact> output;
140 for (int i = 0; i < cast.value(); ++i) {
141 // Sweeps report new crossings only. Backends may return an initial overlap with
142 // fraction zero and no usable normal; it is not a new impact after a bounce.
143 if (getShapeCastResultFraction(i) == 0.0f) continue;
144 const int bodyId = getShapeCastResultBodyId(i);
145 auto* body = findBodyById(bodyId);
146 if (!body)
147 return Output::failure(
148 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "Sweep returned a stale body", "body"));
150 contact.identity = {runtimeHandle().packed(), body->runtimeHandle().packed()};
151 contact.worldEpoch = queryEpoch();
152 contact.bodyId = bodyId;
155 contact.x = x + (endX - x) * contact.fraction;
156 contact.y = y + (endY - y) * contact.fraction;
157 contact.z = z + (endZ - z) * contact.fraction;
161 output.push_back(contact);
162 }
163 return Output::success(std::move(output));
164}
165
167 PhysicsVector3D worldPoint) {
168 if (!isValid() || contact.identity.world != runtimeHandle().packed() || contact.worldEpoch == 0 ||
169 contact.worldEpoch != queryEpoch())
171 eve::DiagnosticCode::NotFound, "Sweep contact world was destroyed, replaced or restored", "contact.world"));
172 auto* body = findBody(PhysicsBodyHandle::fromPacked(contact.identity.body));
173 if (!body || !body->isValid() || body->getId() != contact.bodyId)
175 eve::DiagnosticCode::NotFound, "Sweep contact body was destroyed or replaced", "contact.body"));
176 for (float value : {impulse.x, impulse.y, impulse.z, worldPoint.x, worldPoint.y, worldPoint.z})
177 if (!std::isfinite(value))
179 eve::DiagnosticCode::InvalidArgument, "Impulse and application point must be finite", "impulse"));
180 auto type = body->backend_->getType();
181 if (!type) return eve::Result<void>::failure(type.status());
182 if (type.value() != "dynamic")
184 eve::DiagnosticCode::Unsupported, "Sweep impulse requires a dynamic body", "contact.body"));
185 return body->backend_->applyLinearImpulseAt(impulse.x, impulse.y, impulse.z, worldPoint.x, worldPoint.y,
186 worldPoint.z);
187}
188} // namespace eve::physics
double z
double y
double x
std::string output
std::uint32_t height
std::uint32_t width
double radius
Definition MeleeHit.cpp:42
OceanWakeSettings settings
glm::dvec3 dx
glm::dvec3 dz
std::set< std::string > values
std::string body
Object::Ptr value
std::string filter
ScriptStructure out
float dy
Definition Shadow.cpp:151
std::uint32_t depth
Contact contact
std::array< double, 3 > impulse
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static constexpr RuntimeHandle fromPacked(std::uint64_t value) noexcept
Reconstructs a handle from an explicit packed boundary value.
constexpr std::uint64_t packed() const noexcept
Encodes the two coordinates for a legacy integer boundary.
eve::Result< RayHit3D > rayCastOwned(float x1, float y1, float z1, float x2, float y2, float z2, QueryFilter3D filter={})
Performs one ray query and returns an owning, generation-qualified snapshot.
int castSphere(float x, float y, float z, float radius, float dx, float dy, float dz)
Sweep a sphere through the world and return the earliest hit body id, or -1.
int getShapeCastResultShapeId(int index) const
Shape ID of a sorted Shape Cast result.
int queryCapsule(float ax, float ay, float az, float bx, float by, float bz, float radius)
Query shapes overlapping an arbitrarily oriented capsule. The endpoints are the centers of its hemisp...
int closestPoint(float x, float y, float z, float maxDistance)
Find the closest collision surface within maxDistance of a world point. Uses the current query catego...
float getShapeCastResultFraction(int index) const
Translation fraction in [0,1] of a Shape Cast result.
PhysicsWorldHandle runtimeHandle() const noexcept
Process-local identity used by PhysicsLink; invalid after destruction.
Definition World3D.h:136
eve::Result< BroadPhaseAabb3D > queryAabbBroadPhaseOwned(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, QueryFilter3D filter={})
Queries one AABB into a fixed owning result without using the legacy query cache.
int castBox(float x, float y, float z, float width, float height, float depth, float qx, float qy, float qz, float qw, float dx, float dy, float dz)
Sweep an oriented box without changing its rotation.
eve::Result< std::vector< eve::WorldSphereContact > > sweepSphereOwned(double x, double y, double z, double endX, double endY, double endZ, eve::WorldSphereSweepSettings settings)
Owning, filtered sphere sweep for optional runtime consumers. @thread Owner thread; no callbacks....
Body3D * findBody(PhysicsBodyHandle handle) const
Borrows a live body; null for stale/foreign handles. Never delete lookup results.
Definition World3D.cpp:431
int castCapsule(float ax, float ay, float az, float bx, float by, float bz, float radius, float dx, float dy, float dz)
Sweep an arbitrarily oriented capsule through the world. The endpoints are cap centers and d{x,...
int querySphere(float x, float y, float z, float radius)
Query shapes overlapping a sphere in world meters.
float getShapeCastResultNormalZ(int index) const
Contact normal Z of a Shape Cast result.
int castCapsuleAll(float ax, float ay, float az, float bx, float by, float bz, float radius, float dx, float dy, float dz, int maxHits)
Collects the nearest maxHits swept-capsule intersections.
int rayCast(float x1, float y1, float z1, float x2, float y2, float z2)
Closest raycast from (x1,y1,z1) to (x2,y2,z2) in meters. Returns hit body id, or -1....
int castBoxAll(float x, float y, float z, float width, float height, float depth, float qx, float qy, float qz, float qw, float dx, float dy, float dz, int maxHits)
Collects the nearest maxHits swept-oriented-box intersections.
float getShapeCastResultNormalX(int index) const
Contact normal X of a Shape Cast result.
eve::Result< CapsuleOverlap3D > queryCapsuleOwned(float ax, float ay, float az, float bx, float by, float bz, float radius, QueryFilter3D filter={})
Performs one capsule overlap and returns an owning summary without exposing the query cache.
int queryBox(float x, float y, float z, float width, float height, float depth, float qx, float qy, float qz, float qw)
Query shapes overlapping an oriented box.
bool isValid() const
True while the underlying Box3D world is alive.
Definition World3D.cpp:239
bool sphereCast(float x1, float y1, float z1, float x2, float y2, float z2, float radius, uint64_t maskBits, int ignoredBodyId, CameraSphereHit3D *out) const
Internal camera query: swept sphere against non-sensor shapes.
std::uint64_t queryEpoch() const noexcept
Monotonic query binding revision; successful restore invalidates prior sessions.
Definition World3D.h:645
eve::Result< void > applySweepContactImpulse(const eve::WorldSphereContact &contact, PhysicsVector3D impulse, PhysicsVector3D worldPoint)
Explicitly apply a world-space impulse (N s) to a sweep contact's dynamic body.
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Query shapes overlapping an AABB in meters (min/max corners). Returns match count; read ids with getQ...
int castSphereAll(float x, float y, float z, float radius, float dx, float dy, float dz, int maxHits)
Collects the nearest maxHits swept-sphere intersections.
Body3D * findBodyById(int bodyId) const
Borrows a live body by stable event/query id; never delete lookup results.
Definition World3D.cpp:439
float getShapeCastResultNormalY(int index) const
Contact normal Y of a Shape Cast result.
int rayCastAll(float x1, float y1, float z1, float x2, float y2, float z2, int maxHits)
Collects the nearest maxHits ray intersections, sorted by fraction then Shape ID.
int rayCastFiltered(float x1, float y1, float z1, float x2, float y2, float z2, uint64_t maskBits)
带形状类别掩码的最短射线查询(掩码为接受的 shape categoryBits)。
int getShapeCastResultBodyId(int index) const
Body ID of a sorted Shape Cast result.
T worldValue(eve::Result< T > result)
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Owning sphere sweep contact; position is the moving sphere center, not the obstacle surface.
WorldSweepIdentity identity
Immutable per-binding sphere and filter policy.
CameraSphereHit3D public API.
Definition World3DTypes.h:5
Allocation-free owning three-dimensional physics value.
Definition RigidTypes3D.h:4
Query filter applied atomically for one owning query operation.