我正在尝试在自己的游戏中设置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: %s\n", SDL_GetError() );
}
else
{
//INITIALIZE GLEW
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if( glewError != GLEW_OK )
{
printf( "Error initializing GLEW! %s\n", 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应用程序中的代码进行了比较,但我找不到任何真正的差异。
答案 0 :(得分:1)
从TestBed应用程序中查看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);
}