gpt4 book ai didi

c++ - Box2D:在 Helloworld.cpp 或类似的基本程序中使用 debugdraw?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:17 24 4
gpt4 key购买 nike

我正在尝试创建一个 Box2D 程序,但我需要启用 debugdraw 以查看发生了什么。我正在考虑使用 Box2D 库中包含的 Helloworld 程序作为模板。不幸的是,它没有使用debugdraw,我似乎无法实现它。 Testbed 使用它,但它有点复杂,而且解释得太差,无法用作模板。那么这里的任何人都可以分享一个使用 debugdraw 的 Helloworld.cpp(或使用 Box2D 的类似基本程序)的示例(最好像在 Testbed 中那样实现)吗?

如果重要的话,我正在使用 VS2013。

感谢和问候。

最佳答案

如果您还没有弄清楚这一点(对于 future 的观众),核心 Box2D 库没有可以简单地“启用”的调试绘图功能。它是物理引擎,而不是渲染引擎。

也就是说,您不必从头开始做所有事情。 Testbed 有调试绘图的实现,你可以make a test可以在那里运行。 Box2D 还提供了一种绘图模板,以供您实现自己的渲染代码。本质上,有一些回调,您可以在其中放置代码,例如多边形渲染。每一帧,对于每个多边形,Box2D 将调用该回调,您的多边形将呈现在屏幕上!

您要求提供一个简单的示例,所以这里有一个使用 GLFW 进行窗口化和使用裸机 OpenGL 进行渲染所需的示例。有很多关于这些的教程,但您实际上不需要了解它们就可以理解代码。

主要.cpp:

#define GLEW_STATIC
#include <GLFW\glfw3.h>
#include <Box2D\Box2D.h>
#include "Physics.h"

int main() {
// Initialize window (this is GLFW-specific)
GLFWwindow* window;
if (!glfwInit()) return 1;
window = glfwCreateWindow(960, 540, "Box2D Debug Draw", NULL, NULL);
if (!window) {
glfwTerminate();
return 2;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);

DebugDraw debugDraw; // Declare an instance of our DebugDraw class so we can actually use it

// Set up Box2D world, bodies, etc. (you should be familiar with most of this)
int velocityIterations = 8;
int positionIterations = 3;
b2World* world;
b2Body* groundBody;
b2Body* testBody;
{
b2Vec2 gravity(0.0f, -10.0f);
world = new b2World(gravity);
world->SetDebugDraw(&debugDraw); // YOU NEED THIS SO BOX2D KNOWS WHERE TO LOOK FOR DRAWING CALLBACKS
debugDraw.SetFlags(b2Draw::e_shapeBit);
}
{
b2BodyDef gbDef;
gbDef.position.Set(0.0f, -10.0f);
groundBody = world->CreateBody(&gbDef);

b2PolygonShape gbShape;
gbShape.SetAsBox(50.0f, 10.0f);

groundBody->CreateFixture(&gbShape, 0.0f);
}
{
b2BodyDef tbDef;
tbDef.type = b2_dynamicBody;
tbDef.position.Set(0.0f, 10.0f);
testBody = world->CreateBody(&tbDef);

b2PolygonShape tbShape;
tbShape.SetAsBox(1.0f, 1.0f);

b2FixtureDef tbFix;
tbFix.shape = &tbShape;
tbFix.density = 1.0f;
tbFix.friction = 0.3f;

testBody->CreateFixture(&tbFix);
}

// Loop until user exits
b2Timer timer;
float last = timer.GetMilliseconds();
while (!glfwWindowShouldClose(window)) {
float now = timer.GetMilliseconds();
world->Step((now - last) / 1000, 8, 3); // Step simulation forward by amount of time since last step
last = now;

glClear(GL_COLOR_BUFFER_BIT); // Clear screen
glPushMatrix(); // Push matrix so we can revert after doing some transformations
glScalef(40.0f / 960.0f, 40.0f / 540.0f, 1.0f); // Zoom out
glTranslatef(0.0f, -5.0f, 0.0f); // Pan up
world->DrawDebugData(); // CALL THE DRAWING CALLBACKS WE SET UP IN PHYSICS CLASS
glPopMatrix(); // Revert transformations

glfwSwapBuffers(window); // Push updated buffers to window
glfwPollEvents(); // Poll user events so OS doesn't think app is frozen
}

// Garbage collection
delete world;
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

物理.h:

#ifndef PHYSICS
#define PHYSICS

#include <Box2D\Box2D.h>

class DebugDraw : public b2Draw { // b2Draw has all the virtual functions that we need to override here
public:
// We won't be implementing all of these, but if we don't declare them here we'll get an override error
void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
void DrawTransform(const b2Transform& xf);
};

#endif

物理.cpp:

#include <GL\glew.h>
#include "Physics.h"

void DebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) {
// Standard OpenGL rendering stuff
glColor4f(color.r, color.g, color.b, color.a);
glBegin(GL_POLYGON);
for (int i = 0; i < vertexCount; i++) {
glVertex2f(vertices[i].x, vertices[i].y);
}
glEnd();
}

// We just need to have these to prevent override errors, they don't actually do anything right now
void DebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) {}
void DebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) {}
void DebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) {}
void DebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) {}
void DebugDraw::DrawTransform(const b2Transform& xf) {}

关于c++ - Box2D:在 Helloworld.cpp 或类似的基本程序中使用 debugdraw?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com