我试图创建一个Box2D程序,但我需要启用debugdraw来查看正在进行的操作。我正在考虑使用Box2D库中包含的Helloworld程序作为模板。不幸的是,它没有使用debugdraw,我似乎无法实现它。 Testbed使用它,但它有点复杂,解释得太差,无法用作模板。那么这里的任何人都可以使用debugdraw分享Helloworld.cpp(或使用Box2D的类似基本程序)的示例(最好像在Testbed中一样实现)吗?
我使用VS2013,如果重要的话。
谢谢和问候。
答案 0 :(得分:0)
如果您还没有想到这一点(以及未来的观众),核心Box2D库没有可以简单地“启用”的调试绘图功能。它是一个物理引擎,而不是渲染引擎。
那就是说,你不必从头开始做所有事情。 Testbed有一个调试绘图的实现,你可以在那里运行make a test。 Box2D还提供了一种绘图模板,以防您想要实现自己的渲染代码。从本质上讲,有一些回调可以放置代码,例如,多边形渲染。对于每个多边形,Box2D的每个帧都将调用该回调,并且您的多边形将在屏幕上呈现!
你问了一个简单的例子,所以这里有一个使用GLFW进行窗口化和裸露的OpenGL来进行渲染。这里有很多教程,但你并不需要了解它们以了解代码。
Main.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;
}
Physics.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
Physics.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) {}