Box2d坐标系

时间:2015-08-17 19:14:36

标签: android libgdx coordinates box2d

在游戏中,我试图编码,我必须拥有两具尸体。一个StaticBody和一个DynamicBody。我想将DynamicBody置于StaticBody之上。不知怎的,我得到的是:

Bug

视口低,左角是否可能在0,0?为什么DynamicBody不能在StaticBody之上?

Gamescreen.java

package com.joelbrun.jetskirider.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.viewport.ScalingViewport;
import com.badlogic.gdx.utils.viewport.Viewport;


public class Gamescreen implements Screen {

    private static final float TIMESTEP = 1 / 60;
    private static final int VELOCITY_ITERATIONS = 8;
    private static final int POSITION_ITERATIONS = 3;
    public static final int GAMESCREEN_WIDTH = 1920;
    public static final int GAMESCREEN_HEIGHT = 720;
    public static final int OBSTACLE_TYPES = 5;
    private static final int WATER_HEIGHT = 200;
    private static final float JETSKI_BOX_X = 200;
    private static final float JETSKI_BOX_Y = 100;
    private World world;
    private Box2DDebugRenderer debugRenderer;
    public Texture jetski, wave;
    public Sprite background;
    public SpriteBatch batch;
    public OrthographicCamera camera;
    public Label score;
    Viewport viewport;

    @Override
    public void show() {
        world = new World(new Vector2(0, -9.81f), true);
        debugRenderer = new Box2DDebugRenderer();
        Texture[] texture = new Texture[OBSTACLE_TYPES];

        //Textures
        for (int i=0; i<OBSTACLE_TYPES; i++){
            texture[i] = new Texture(Gdx.files.internal("game/obstacles/obstacle" + Integer.toString(i+1)+".png"));
        }
        background = new Sprite(new Texture("game/background.png"));
        jetski = new Texture("game/jetski.png");
        wave = new Texture("game/water1.png");
        batch = new SpriteBatch();

        //Camera & Viewport
        camera = new OrthographicCamera();

        viewport = new ScalingViewport(Scaling.fillY, GAMESCREEN_WIDTH, GAMESCREEN_HEIGHT, camera);
        viewport.apply();
        camera.position.set(GAMESCREEN_WIDTH / 2, GAMESCREEN_HEIGHT / 2, 0);


        //body definition Water
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set(0, 0);
        Body body = world.createBody(bodyDef);


        //shape
        PolygonShape box = new PolygonShape();
        box.setAsBox(2560, WATER_HEIGHT);

        //fixture definition
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = box;
        fixtureDef.density = 1000;
        fixtureDef.friction = .25f;
        fixtureDef.restitution = .1f;

        Fixture fixture = body.createFixture(fixtureDef);

        box.dispose();

        //Jetski
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(1280,WATER_HEIGHT + 100);

        Body jetskiObject = world.createBody(bodyDef);

        PolygonShape jetskiBox = new PolygonShape();
        jetskiBox.setAsBox(JETSKI_BOX_X, JETSKI_BOX_Y);

        fixtureDef.shape = jetskiBox;
        fixtureDef.density = 100;
        fixtureDef.friction = 0;

        Fixture jetskiFixture = body.createFixture(fixtureDef);

        jetskiBox.dispose();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        debugRenderer.render(world, camera.combined);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.end();
        camera.update();

        world.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    }

    @Override
    public void resize(int width, int height) {
        int SCREEN_WIDTH = width;
        int SCREEN_HEIGHT = height;

        viewport.setWorldSize(GAMESCREEN_WIDTH, GAMESCREEN_HEIGHT);
        viewport.update(SCREEN_WIDTH, SCREEN_HEIGHT,true);
        camera.update();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void dispose() {
        batch.dispose();
        background.getTexture().dispose();
        jetski.dispose();
        wave.dispose();
        world.dispose();
        debugRenderer.dispose();
    }


}

3 个答案:

答案 0 :(得分:1)

对于Box2D,原点是视口的左下角。

您可以在设置BodyDef时设置身体的位置:

bodyDef.position.set(x, y);

不要忘记,在Box2D中,你身体的位置是身体的中心。因此,当您将PolygonShape设置为如下框时:

polygonShape.setAsBox(width, height);

您的实际包装箱宽度为宽* 2,实际包装箱高度为高度* 2

答案 1 :(得分:0)

您使用的是Box2D错误。你应该将Box2D缩小到一个小世界,因为如果没有这样做,物理交互将是1px = 1m而1920 x 720太大(这将反映为你的身体移动得太慢,因为它们太大了)。 / p>

检查您的代码:

PolygonShape box = new PolygonShape();
box.setAsBox(2560, WATER_HEIGHT); // <- your water object dimensions

PolygonShape jetskiBox = new PolygonShape();dimensions
jetskiBox.setAsBox(JETSKI_BOX_X, JETSKI_BOX_Y);  // <- your jetsky object 

然后,检查setAsBox方法:

  

public void setAsBox(float hx, float hy)

     

参数:

     

hx - 半宽

     

hy - 半高

(增加重点)

因此,对于你的水,你正在创建一个5120 x 400的静态体,而对于你的jetski 400 x 200,这个值太高了。

我会尝试使用official wiki中显示的示例,但请检查this等网站并阅读manual以了解Box2D的工作原理。我可以提取这个:

  

特别是,Box2D已经过调整,可以在0.1到10米之间移动物体。所以这意味着汤罐和公共汽车之间的物体应该很好。静态物体可能长达50米,没有太多麻烦。

     

作为2D物理引擎,很有可能使用像素作为单位。不幸的是,这将导致模拟不良和可能奇怪的行为。 Box2D将长度为200像素的物体视为45层建筑的大小。

另一件事,我不知道这是否会影响到某种程度,但我总是首先world.step(...)然后debugRenderer.render(...)结束,在你的代码中,你有这个倒置(也许这就是你的jetski没有被移动的原因。)

答案 2 :(得分:0)

您应该尝试使用chainshape而不是多边形来表示staticbody。这样动态的身体将保留在其中而不是“传送”出去。动态主体在创建时不得与静态主体相交。希望它能奏效!