繁体   English   中英

LibGDX TiledMap-不检测碰撞

[英]LibGDX TiledMap - don't detect Collisions

首先,对不起,因为我的英语不好。 自从我学德语以来,我就忘记了英语。

我正在使用libGDX进行测试,我的代码未检测到任何冲突。

在我的屏幕上:

    public Pantalla(SpriteBatch batch_1) {
        batch = batch_1;
        screenWidth = Gdx.graphics.getWidth();
        screenHeight = Gdx.graphics.getHeight();

        stage = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()),batch);
        Gdx.input.setInputProcessor(stage);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 1000, 840);
        camera.update();

        mapas =  new Mapas(camera);

        //              ACTORS 
        indiana_actor = new indiana_Actor();

    //Here comes TOUCHPAD with Skin blaBlaBla...
     if (touchpad.isTouched()) {  
              if (touchpad.getKnobX() > 120) {
                            indiana_actor.moveBy(32,0);
                            camera.translate(32, 0);
                            return; }
}

stage.addActor(indiana_actor);
            stage.addActor(touchpad);
            Gdx.input.setInputProcessor(stage);
}



    public void render(float delta) {//TODO RENDER
         Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


            mapas.MapasRender(camera,indiana_actor);
            batch.begin();
        batch.end();

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

    }

indiana_actor类别:

 public indiana_Actor() {
        W=Gdx.graphics.getWidth(); H=Gdx.graphics.getHeight();
        bounds=new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());

    }
    Animation anim_temp;


    @Override
    public void draw(Batch batch, float parentAlpha) {
        stateTime += Gdx.graphics.getDeltaTime();
        batch.setColor(getColor());
batch.draw(Assets.indiana_stop_arriba, (W/2), (H/2), 110, 160);

        bounds=new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());

    }

和Mapas类。 在这些类中,我在“ objetos” TiledLayer中获得对象,并尝试在渲染器中使用for(...)检查冲突。

public Mapas(OrthographicCamera camera2){
    map = new TmxMapLoader().load("terrain/prueba.tmx");
    renderer = new OrthogonalTiledMapRenderer(map, 10);//ESCALA
    renderer.setView(camera2);

    collisionObjects = map.getLayers().get("objetos").getObjects();
    collisionRects = new Array<Rectangle>();
    collisionRects_total=collisionObjects.getCount();
    int tileWidth = 32; // whatever your tile width is
    int tileHeight = 32; // whatever your tile height is
    for (int i = 0; i < collisionObjects.getCount(); i++) {
        RectangleMapObject obj = (RectangleMapObject) collisionObjects.get(i);
        Rectangle rect = obj.getRectangle();
        collisionRects.add(new Rectangle(rect.x / tileWidth, rect.y / tileHeight, rect.width / tileWidth, rect.height / tileHeight));
    }


}
public void MapasRender(OrthographicCamera camera2,indiana_Actor indi){
    camera2.update();
    renderer.setView(camera2);
    renderer.render();

    for (int i = 0; i < collisionRects_total; i++) {
        Rectangle rect = collisionRects.get(i);
        if (indi.bounds.overlaps(rect)){
            Gdx.app.log("EVENTO", "MAPAS RENDER - COLISION!");
        }if (rect.overlaps(indi.bounds)){
            Gdx.app.log("EVENTO", "MAPAS RENDER - COLISION!");
        }
    }
} 

我知道(通过logcat)“ for(int i = 0; i <crashRects_total; i ++){Rectangle rect = crashRects.get(i);“总是得到objectsRectangle,但是在下一行中发现任何重叠。

那是actor bounds-rectangle的问题吗?在通过地图移动actor时有问题吗?

提前致谢!!

为什么不首先检查您的碰撞矩形是否正确绘制在位置上。 您可以致电

  shapeRenderer.begin(ShapeType.Line);
  shapeRenderer.setColor(Color.NAVY);

  shapeRenderer.rect(object.getrect().x,
 object.getrect().y,object.getrect().width,
  object.getrect().height);

可能是您在错误的位置绘制了碰撞矩形,因此永远不会发生碰撞。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM