首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在libgdx中绘制box2d

在libgdx中绘制box2d
EN

Stack Overflow用户
提问于 2015-03-14 00:55:31
回答 1查看 1.7K关注 0票数 0

我有一个使用FitViewport的大小为800x480的游戏世界,并且最初是用像素绘制box2d车身和固定装置,所以所有的物理看起来都是浮动的和缓慢的。看一看文档,我意识到box2d使用度量单位,所以我浏览了box2d的位置和大小,转换成32倍,所以我最终得到了一个25x15米的box2d世界。

我遇到的问题是,现在box2d对象呈现得非常小。我如何将它们缩小,使它们在屏幕上显示为规则大小?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 05:50:52

您需要在world和box2之间使用转换因子。

在create()中,您将拥有:

代码语言:javascript
运行
复制
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,以创建您的相机。但是,如果您想要在您的身体上画一些精灵,您将在render()中使用BOX_TO_WORLD因子。例如,您希望使用sprite批处理在您的正方形上绘制一个sprite,其大小和位置是您将拥有的render():

代码语言:javascript
运行
复制
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();
}

希望它能回答你的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29044249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档