Libgdx使用box2d进行桌面冲突,但未能在Android模拟器中发生冲突

时间:2015-04-30 03:43:10

标签: libgdx

我将动态物体与静态物体碰撞的代码完美地适用于桌面,但在Android模拟器中运行时,它无法检测到碰撞,动态物体在没有碰撞的情况下发生故障。 我使用的是Libgdx版本:1.5.0 代码:

package com.kg.game;
import java.util.Random;
import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
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.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
public class MyGdxGame extends ApplicationAdapter implements InputProcessor {
    private static final float VIEWPORT_WIDTH = 10;
    private static final float BALL_RADIUS = 0.15f;
    private static final int MAX_BALLS = 200;
    private World world;
    private Body[] ballModels;
    private Texture bottleTexture;
    private Texture ballTexture;
    private Sprite[] ballSprites;
    private Texture whiteTexture;
    private Sprite groundSprite;
    private SpriteBatch batch;
    private BitmapFont font;
    private OrthographicCamera camera;
    private final Random rand = new Random();
    float w;
    float h;
    Box2DDebugRenderer debugRenderer;
    private final TweenManager tweenManager = new TweenManager();
    @Override
    public void create() {
        debugRenderer = new Box2DDebugRenderer();
        world = new World(new Vector2(0, -10), true);
        createGround();
        createBalls();
        batch = new SpriteBatch();
        font = new BitmapFont();
        font.setColor(Color.BLACK);
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH * h / w);
        camera.position.set(0, (VIEWPORT_WIDTH * h / w) / 2, 0);
        camera.update();
        createSprites();
        Gdx.input.setInputProcessor(new InputAdapter() {
            @Override
            public boolean touchDown(int x, int y, int pointer, int button) {
                restart();
                return true;
            }
        });  
        restart();
    }
    private void createGround() {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 0);
        bd.type = BodyType.StaticBody;    
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(100, 4);    
        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;    
        world.createBody(bd).createFixture(fd);    
        shape.dispose();
    }    
    private void createBalls() {
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyType.DynamicBody;    
        CircleShape shape = new CircleShape();
        shape.setRadius(BALL_RADIUS);    
        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;    
        ballModels = new Body[MAX_BALLS];
        for (int i = 0; i < MAX_BALLS; i++) {
            ballModels[i] = world.createBody(ballBodyDef);
            ballModels[i].createFixture(fd);
        }
        shape.dispose();
    }    
    private void createSprites() {    
        ballTexture = new Texture(Gdx.files.internal("ball.png"));
        ballTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);    
        ballSprites = new Sprite[MAX_BALLS];
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i] = new Sprite(ballTexture);
            ballSprites[i].setSize(BALL_RADIUS * 2, BALL_RADIUS * 2);
            ballSprites[i].setOrigin(BALL_RADIUS, BALL_RADIUS);
        }    
        whiteTexture = new Texture(Gdx.files.internal("ground.png"));    
        groundSprite = new Sprite(whiteTexture);
        groundSprite.setSize(100, 4);
        groundSprite.setPosition(-VIEWPORT_WIDTH / 2, 0);
        groundSprite.setColor(Color.BLACK);
    }    
    private float elapsed = 0;    
    @Override
    public void render() {
        tweenManager.update(1 / 60f);
        world.step(1 / 60f, 10, 10);
        debugRenderer.render(world, camera.combined);
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
        GL20 gl = Gdx.gl20;
        gl.glClearColor(1, 1, 1, 1);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        // Update    
        for (int i = 0; i < MAX_BALLS; i++) {
            Vector2 ballPos = ballModels[i].getPosition();
            ballSprites[i].setPosition(ballPos.x - ballSprites[i].getWidth()
                    / 2, ballPos.y - ballSprites[i].getHeight() / 2);
            ballSprites[i].setRotation(ballModels[i].getAngle()
                    * MathUtils.radiansToDegrees);
        }    
        // Render    
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        groundSprite.draw(batch);
        for (int i = 0; i < MAX_BALLS; i++)
            ballSprites[i].draw(batch);
        batch.end();    
        // batch.getProjectionMatrix().setToOrtho2D(0, 0, w, h);
        batch.begin();
        font.draw(batch, "Touch the screen to restart", 5, h - 5);
        batch.end();    
    }    
    @Override
    public void dispose() {
        bottleTexture.dispose();
        ballTexture.dispose();
        batch.dispose();
        font.dispose();
        world.dispose();
    }    
    @Override
    public boolean keyDown(int keycode) {
        return false;
    }    
    @Override
    public boolean keyUp(int keycode) {    
        return true;
    }    
    @Override
    public boolean keyTyped(char character) {
        return false;
    }    
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return true;
    }    
    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }    
    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }    
    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }    
    @Override
    public boolean scrolled(int amount) {
        return false;
    }    
    private void restart() {    
        Vector2 vec = new Vector2();    
        for (int i = 0; i < MAX_BALLS; i++) {
            float tx = rand.nextFloat() * 1.0f - 0.5f;
            float ty = VIEWPORT_WIDTH * h / w;
            float angle = rand.nextFloat() * MathUtils.PI * 2;    
            ballModels[i].setActive(false);
            ballModels[i].setLinearVelocity(vec.set(0, 0));
            ballModels[i].setAngularVelocity(0);
            ballModels[i].setTransform(vec.set(tx, ty), angle);
        }    
        tweenManager.killAll();    
        Tween.call(new TweenCallback() {
            private int idx = 0;    
            @Override
            public void onEvent(int type, BaseTween<?> source) {
                if (idx < ballModels.length) {
                    ballModels[idx].setAwake(true);
                    ballModels[idx].setActive(true);
                    idx += 1;
                }
            }
        }).repeat(-1, 0.1f).start(tweenManager);
    }
}

Screensots:     desktop scrshot

Android Scrshot

1 个答案:

答案 0 :(得分:0)

这是一个想法,看着你的图像的比例,我认为错误可能在那里,我会看看这些线:

groundSprite.setSize(100, 4);                     //<--especially here
groundSprite.setPosition(-VIEWPORT_WIDTH / 2, 0); //<--especially here

    private void createGround() {
    BodyDef bd = new BodyDef();
    bd.position.set(0, 0);                        //<--especially here
    bd.type = BodyType.StaticBody;    
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(100, 4);                       //<--especially here

特别是位置和尺寸,如果它站在同一个地方,随着屏幕“设备”的改变。

你试图渲染调试?我看到那里,如果是这样,你可以在模拟设备中看到你的对象吗?

我希望我理解,但如果那是错误