2014-11-22 44 views
0

我試圖在我自己的遊戲中設置Box2D。我目前有一些代碼工作,我添加了一個DebugDraw類到我的遊戲中,它繪製了由Box2D創建的多邊形。調試繪製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應用程序的代碼進行了比較,我找不到任何真正的區別。

回答

1

看一看在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); 
} 
+0

謝謝:)這確實解決了!我真的必須研究如何在遊戲中使用矩陣... – Dries 2014-11-22 21:36:02

相關問題