载入中...
搜索中...
未找到
WorldDebugDraw.cpp
浏览该文件的文档.
1#include "physics/World.h"
2
3#include "graphics/Graphics.h"
6
7#include <Box2D/Box2D.h>
8
9#include <vector>
10
11namespace eve::physics {
12namespace {
13
15
22class DebugDraw final : public b2Draw {
23public:
24 DebugDraw() { SetFlags(e_shapeBit | e_jointBit | e_aabbBit); }
25
26 void begin(graphics::PrimitiveCanvas2D *canvas, float meter) {
27 canvas_ = canvas;
28 meter_ = meter;
29 }
30
31 void end() { canvas_ = nullptr; }
32
33 void DrawPolygon(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color) override {
34 drawPoly(vertices, vertexCount, color, false);
35 }
36
37 void DrawSolidPolygon(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color) override {
38 drawPoly(vertices, vertexCount, color, true);
39 }
40
41 void DrawCircle(const b2Vec2 &center, float32 radius, const b2Color &color) override {
42 drawCircle(center, radius, color, false);
43 }
44
45 void DrawSolidCircle(const b2Vec2 &center, float32 radius, const b2Vec2 & /*axis*/, const b2Color &color) override {
46 drawCircle(center, radius, color, true);
47 }
48
49 void DrawSegment(const b2Vec2 &p1, const b2Vec2 &p2, const b2Color &color) override {
50 if (!canvas_) return;
51 canvas_->drawLine({p1.x * meter_, p1.y * meter_}, {p2.x * meter_, p2.y * meter_}, strokePaint(color));
52 }
53
54 void DrawTransform(const b2Transform &xf) override {
55 DrawSegment(xf.p, xf.p + 0.5f * xf.q.GetXAxis(), b2Color(1, 0, 0));
56 DrawSegment(xf.p, xf.p + 0.5f * xf.q.GetYAxis(), b2Color(0, 1, 0));
57 }
58
59private:
60 static graphics::PrimitivePaint strokePaint(const b2Color &color) {
61 graphics::PrimitivePaint paint;
62 paint.color = Color(color.r, color.g, color.b, color.a * 0.8f);
63 paint.mode = graphics::PaintMode::Stroke;
64 paint.stroke.width = 1.5f;
65 paint.stroke.cap = graphics::LineCap::Round;
66 paint.stroke.join = graphics::LineJoin::Round;
67 return paint;
68 }
69
70 void drawPoly(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color, bool solid) {
71 if (!canvas_ || vertexCount < 2) return;
72 std::vector<glm::vec2> points;
73 points.reserve(static_cast<std::size_t>(vertexCount));
74 for (int32 i = 0; i < vertexCount; ++i) points.emplace_back(vertices[i].x * meter_, vertices[i].y * meter_);
75 graphics::PrimitivePaint paint = strokePaint(color);
76 if (solid && vertexCount >= 3) {
78 paint.color.a = color.a * 0.35f;
79 graphics::Path2D path;
80 path.moveTo(points.front());
81 for (std::size_t i = 1; i < points.size(); ++i) path.lineTo(points[i]);
82 path.close();
83 canvas_->drawPath(path, paint);
84 } else {
85 canvas_->drawPolyline(points, true, paint);
86 }
87 }
88
89 void drawCircle(const b2Vec2 &center, float32 radius, const b2Color &color, bool solid) {
90 if (!canvas_) return;
91 graphics::PrimitivePaint paint = strokePaint(color);
92 if (solid) {
94 paint.color.a = color.a * 0.35f;
95 }
96 canvas_->drawCircle({center.x * meter_, center.y * meter_}, radius * meter_, paint);
97 }
98
99 graphics::PrimitiveCanvas2D *canvas_ = nullptr;
100 float meter_ = 30.f;
101};
102
103class DebugDrawBinding final {
104public:
105 DebugDrawBinding(b2World *world, b2Draw *draw) : world_(world) { world_->SetDebugDraw(draw); }
106 ~DebugDrawBinding() { world_->SetDebugDraw(nullptr); }
107
108 DebugDrawBinding(const DebugDrawBinding &) = delete;
109 DebugDrawBinding &operator=(const DebugDrawBinding &) = delete;
110
111private:
112 b2World *world_;
113};
114
115} // namespace
116
118 b2World *world = raw();
119 if (!world || !isValid() || !gfx) return;
120
121 DebugDraw draw;
123 draw.begin(&canvas, getMeter());
124 DebugDrawBinding binding(world, &draw);
125 world->DrawDebugData();
126 draw.end();
127 gfx->drawPrimitiveCanvas(canvas);
128}
129
130} // namespace eve::physics
std::uint32_t vertexCount
MeshVfxBatchedDraw draw
eve::action::ActionVfxBinding binding
World3D * world
std::vector< Point > vertices
float radius
std::string path
Definition PlayHost.cpp:110
std::shared_ptr< const std::vector< glm::vec2 > > points
float begin
virtual void drawPrimitiveCanvas(const PrimitiveCanvas2D &canvas)=0
Resolves and queues one frame-local primitive Canvas.
Frame-local Skia-style recorder for 2D line primitives.
void drawDebug(graphics::Graphics *gfx)
Optional: draw fixture AABBs via Graphics::drawSolidRect.
b2World * raw()
Exposes the underlying Box2D world for tightly-scoped backend integration.
Definition World.h:378
bool isValid() const
True while the underlying Box2D world is alive.
Definition World.h:306
float getMeter() const
Definition World.h:161
eve::Color Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
glm::vec4 Color
Render-neutral RGBA color shared by graphics-facing modules.
Definition RenderTypes.h:8
glm::vec4 color