载入中...
搜索中...
未找到
JoltExplosion.cpp
浏览该文件的文档.
1// eve-hot-path
2// Jolt requires its platform header before any other native header.
3#include <Jolt/Jolt.h>
4
5// Provider implementation dependencies.
6#include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
7#include "physics/World3D.h"
10namespace eve::physics::jolt {
11namespace {
12float projectedArea(const ShapeState& shape, JPH::Vec3Arg direction) {
13 const auto& g = shape.geometry;
14 switch (g.kind) {
15 case ShapeKind3D::Sphere: return JPH::JPH_PI * g.a * g.a;
17 return JPH::JPH_PI * g.b * g.b + 4 * g.a * g.b * JPH::Vec3::sAxisY().Cross(direction).Length();
19 return 4 * (g.b * g.c * std::abs(direction.GetX()) + g.a * g.c * std::abs(direction.GetY()) +
20 g.a * g.b * std::abs(direction.GetZ()));
22 const auto& hull = static_cast<const JPH::ConvexHullShape&>(*shape.native);
23 float area = 0;
24 for (JPH::uint face = 0; face < hull.GetNumFaces(); ++face) {
25 JPH::uint indices[256];
26 const auto count = hull.GetFaceVertices(face, 256, indices);
27 const auto origin = hull.GetPoint(indices[0]);
28 for (JPH::uint i = 1; i + 1 < count; ++i)
29 area += .5f * std::max(0.f, (hull.GetPoint(indices[i]) - origin)
30 .Cross(hull.GetPoint(indices[i + 1]) - origin)
31 .Dot(direction));
32 }
33 return area;
34 }
35 default: return 0;
36 }
37}
38} // namespace
39eve::Result<int> WorldBackend::explode(float x, float y, float z, float radius, float falloff, float strength,
40 int mask) {
41 if (!isValid()) return rejected<int>("World expired", eve::DiagnosticCode::StaleHandle);
42 if (!isFinite(x, y, z) || !isFinite(radius, falloff, strength) || radius < 0 || falloff < 0 ||
43 !std::isfinite(radius + falloff))
44 return rejected<int>("Invalid explosion", eve::DiagnosticCode::InvalidArgument);
45 owner->explosionResults_.clear();
46 struct Impulse {
47 JPH::RVec3 point;
48 JPH::Vec3 impulse;
49 std::shared_ptr<BodyState> body;
50 };
51 std::vector<Impulse> impulses;
52 const JPH::RVec3 center(x, y, z);
53 // Prepare and validate every impulse before mutating any body.
54 for (const auto& [id, body] : state->bodies) {
55 if (!body->active || !body->owner || body->api().GetMotionType(body->id) != JPH::EMotionType::Dynamic) continue;
56 for (const auto& shape : body->shapes) {
57 if (!shape->owner || shape->native->GetType() != JPH::EShapeType::Convex ||
58 !(shape->filter.categoryBits & uint32_t(mask)) || shape->owner->getExplosionScale() == 0)
59 continue;
61 const float distance = float((point - center).Length());
62 if (distance > radius + falloff) continue;
63 const auto& g = shape->geometry;
64 const auto rotation =
65 body->api().GetRotation(body->id) * JPH::Quat(g.localQx, g.localQy, g.localQz, g.localQw);
66 if (distance <= 1.e-6f)
67 point = body->api().GetWorldTransform(body->id) *
68 (JPH::Vec3(g.localX, g.localY, g.localZ) +
69 JPH::Quat(g.localQx, g.localQy, g.localQz, g.localQw) * shape->native->GetCenterOfMass());
70 const auto direction = JPH::Vec3(point - center).NormalizedOr(JPH::Vec3::sAxisX());
71 const float scale =
72 distance > radius && falloff > 0 ? std::clamp((radius + falloff - distance) / falloff, 0.f, 1.f) : 1.f;
73 const float magnitude = strength * projectedArea(*shape, rotation.Conjugated() * direction) * scale *
74 shape->owner->getExplosionScale();
75 if (!std::isfinite(magnitude))
76 return rejected<int>("Explosion impulse overflow", eve::DiagnosticCode::InvalidArgument);
77 impulses.push_back({point, direction * magnitude, body});
78 }
79 }
80 struct Before {
81 std::shared_ptr<BodyState> body;
82 JPH::Vec3 linear, angular;
83 };
84 std::vector<Before> before;
85 for (const auto& impulse : impulses) {
86 auto body = impulse.body;
87 if (std::none_of(before.begin(), before.end(), [&](const Before& entry) { return entry.body == body; }))
88 before.push_back({body, body->api().GetLinearVelocity(body->id), body->api().GetAngularVelocity(body->id)});
89 body->api().AddImpulse(body->id, impulse.impulse, impulse.point);
90 }
91 for (const auto& entry : before) {
92 const auto dv = entry.body->api().GetLinearVelocity(entry.body->id) - entry.linear;
93 const auto dw = entry.body->api().GetAngularVelocity(entry.body->id) - entry.angular;
94 if (dv.LengthSq() <= 1.e-12f && dw.LengthSq() <= 1.e-12f) continue;
95 owner->explosionResults_.push_back(
96 {entry.body->owner->getId(), dv.GetX(), dv.GetY(), dv.GetZ(), dw.GetX(), dw.GetY(), dw.GetZ()});
97 }
98 std::sort(owner->explosionResults_.begin(), owner->explosionResults_.end(),
99 [](const auto& a, const auto& b) { return a.bodyId < b.bodyId; });
100 return eve::Result<int>::success(int(owner->explosionResults_.size()));
101}
102} // namespace eve::physics::jolt
double z
double y
double x
int mask
b3Vec3 angular
float strength
float linear
tensor::Graph g
Definition GpuGraph.cpp:7
ShaderImageInput shape
float area
Definition Grass.cpp:62
std::vector< std::uint32_t > indices
std::array< float, 4 > rotation
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
std::shared_ptr< OceanGpuSpectrum > owner
glm::dvec3 origin
glm::dvec2 center
std::string body
RoadLaneDirection direction
int count
std::array< double, 3 > impulse
glm::vec3 point
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
std::shared_ptr< State > state
Definition JoltWorld.h:17
eve::Result< int > explode(float x, float y, float z, float radius, float falloff, float impulsePerArea, int maskBits) override
explode; uses the class ownership, thread and failure contract.
bool isValid() const noexcept override
Native world liveness.
Definition JoltWorld.cpp:90
bool isFinite(float x, float y, float z)
JPH::RVec3 closestConvexPoint(const ShapeState &shape, JPH::RVec3Arg target)
Returns a convex leaf's world closest point; shape/body are borrowed only during this idle-thread cal...