2015-03-14 3 views
0

У меня есть игровой мир размером 800x480 с использованием FitViewport и был первоначально рендерингом box2d body + fixtures с использованием пикселей, поэтому вся физика появилась плавающей и медленной. Глядя на документацию, я понял, что box2d использует единицы измерения, поэтому я прошел и преобразовал позиции box2d и размеры в 32 раза, поэтому в итоге я получил поле box2d размером 25x15 метров.Рендеринг box2d в libgdx

Проблема, с которой я сталкиваюсь, заключается в том, что теперь объекты box2d визуализируются невероятно мало. Как я могу масштабировать их назад, чтобы они отображались на экране?

+0

Итак? Любое обновление вашей проблемы? Вы попробовали мой ответ? Это решило вашу проблему? Если это не решило, что вы наблюдаете? – vdlmrc

ответ

1

Вам нужно использовать коэффициент конверсии между миром и box2.

В вашем создании(), вы будете иметь:

private OrthographicCamera camera; 
private BodyDef bodyDef; 
private Body body; 
private PolygonShape box; 
static float WORLD_TO_BOX, BOX_TO_WORLD, bodyWidth, bodyHieght; 

public Create(){ 
    //Conversion factors 
    WORLD_TO_BOX = 1/32f; 
    BOX_TO_WORLD = 1/WORLD_TO_BOX; 

    //Creation of the camera with your factor 32 
    camera = new OrthographicCamera(); 
    camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX; 
    camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX; 
    camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f); 
    camera.update(); 

    //Let's say you want to create a body in the center of your screen 
    BodyDef = new BodyDef(); 
    BodyDef.position.set(new Vector2(camera.viewportWidth/2, -camera.viewportHeight/2)); 
    body = world.createBody(BodyDef); 
    box = new PolygonShape(); 
    //Let's say you want your body to have the shape of a square which sides size is one fifth of your camera width 
    bodyWidth = camera.viewportWidth/10; 
    bodyHeight = camera.viewportWidth/10; 
    box.setAsBox(bodyWidth, bodyHeight); 
    body.createFixture(box, 0.0f); 
} 

На данный момент вы использовали только фактор WORLD_TO_BOX преобразования, чтобы создать камеру. Но если вы хотите нарисовать некоторые спрайты на своем теле, вы будете использовать коэффициент BOX_TO_WORLD в render(). Например, вы хотите использовать SpriteBatch нарисовать спрайт на вашей площади, с точным размером и положением, в визуализации() вы будете иметь:

private Texture texture; 

public void render(){ 
    game.batch.begin(); 
    game.batch.setColor(1,1,1,1); 
//In Box2D, the orgin of a body is the center of the body, 
//while in your world the orgin of a sprite is the bottom left corner 
//I Box2D the width and the height of a body will be twice the values you entered, 
//while in your world the width and the height of a sprite are the exact values you entered. 
//Taking all this into account, to draw a sprite above a body you need to : 
//1 - center the sprite above the body. Ex : (body.getPosition().x - bodyWidth) for the X position 
//2 - draw the sprite at the right size, taking in account the factor 2. Ex : bodyWidth * 2 for the widht of the sprite 
//3 - Apply the BOX_TO_WORLD factor to all every sizes and distances 
    game.batch.draw(texture, 
        BOX_TO_WORLD * (body.getPosition().x - bodyWidth), 
        BOX_TO_WORLD * (body.getPosition().y - bodyHeight), 
        BOX_TO_WORLD * bodyWidth * 2, 
        BOX_TO_WORLD * bodyHeight * 2); 
    game.batch.end(); 
} 

Надеются, что это отвечает на ваш вопрос.