与测试平台应用程序相比,调试绘制Box2D非常大

debug draw Box2D very large compared to Testbed app

本文关键字:绘制 调试 Box2D 非常 测试 平台 应用程序      更新时间:2023-10-16

我正在尝试在自己的游戏中设置Box2D。我目前有一些代码在工作,我在我的游戏中添加了一个 DebugDraw 类,该类绘制了 Box2D 创建的多边形。

现在,如果我将其与 Testbed 应用程序中的代码进行比较(我复制了一些代码以重新创建 OneSidedPlatform 测试),它在我的游戏中显示的比在原始应用程序中要大得多。

我想这与OpenGL/SDL本身有关,所以这里有一些代码:

这在我的游戏中设置了OpenGL:

// START SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
    logSDLError(std::cout, "SDL_Init");
    return 1;
}
// SETUP OPENGL SETTINGS (THEY CAN BE SET WITHOUT ACTUALLY BEING USED)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// CREATE A WINDOW
m_pWindow = SDL_CreateWindow("SDL/OpenGL Game Client - With Networking Library", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
if (m_pWindow == nullptr)
{
    logSDLError(std::cout, "CreateWindow");
    return 2;
}
m_GlContext = SDL_GL_CreateContext(m_pWindow);
if( m_GlContext == NULL )
{
    printf( "OpenGL context could not be created! SDL Error: %sn", SDL_GetError() );
}
else
{
    //INITIALIZE GLEW
    glewExperimental = GL_TRUE; 
    GLenum glewError = glewInit();
    if( glewError != GLEW_OK )
    {
        printf( "Error initializing GLEW! %sn", glewGetErrorString( glewError ) );
    }
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
// CREATE A RENDERER TO DRAW THE WINDOW TO
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (m_pRenderer == nullptr)
{
    logSDLError(std::cout, "CreateRenderer");
    return 3;
}

这是 Box2D 对象的创建(从原始测试应用程序复制):

// Ground
{
    b2BodyDef bd;
    b2Body* ground = Engine::GetInstance()->GetPhysicsWorld()->CreateBody(&bd);
    b2EdgeShape shape;
    shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
    ground->CreateFixture(&shape, 0.0f);
}
// Platform
{
    b2BodyDef bd;
    bd.position.Set(0.0f, 10.0f);
    b2Body* body = Engine::GetInstance()->GetPhysicsWorld()->CreateBody(&bd);
    b2PolygonShape shape;
    shape.SetAsBox(3.0f, 0.5f);
    m_platform = body->CreateFixture(&shape, 0.0f);
    m_bottom = 10.0f - 0.5f;
    m_top = 10.0f + 0.5f;
}
// Actor
{
    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.position.Set(0.0f, 12.0f);
    b2Body* body = Engine::GetInstance()->GetPhysicsWorld()->CreateBody(&bd);
    m_radius = 0.5f;
    b2CircleShape shape;
    shape.m_radius = m_radius;
    m_character = body->CreateFixture(&shape, 20.0f);
    body->SetLinearVelocity(b2Vec2(0.0f, -50.0f));
    m_state = e_unknown;
}
// Set debug
Engine::GetInstance()->GetPhysicsWorld()->SetDebugDraw(&m_debugDraw);
m_debugDraw.SetFlags(b2Draw::e_shapeBit);

我不知道我必须去哪里改变我的游戏视图或Box2D的规模。我还将我的OpenGL代码与Testbed应用程序中的代码进行了比较,但我找不到任何真正的区别。

在 TestBed app 的 Main.cpp 中查看此方法:

static void Resize(int32 w, int32 h)
{
    ...
    // those are the lines you may consider incorporating into your code:
    b2Vec2 lower = settings.viewCenter - extents;
    b2Vec2 upper = settings.viewCenter + extents;
    // L/R/B/T
    gluOrtho2D(lower.x, upper.x, lower.y, upper.y);
}