Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Libgdx+;Box2d使用2个摄像头_Java_Android_Libgdx_Sprite_Box2d - Fatal编程技术网

Java Libgdx+;Box2d使用2个摄像头

Java Libgdx+;Box2d使用2个摄像头,java,android,libgdx,sprite,box2d,Java,Android,Libgdx,Sprite,Box2d,最近我读到,只要您使用Box2d,您就可以使用2个摄像头,它只能以米为单位工作 在渲染器类中,我接收游戏中的所有实体,然后绘制它们。 我使用ashley库,因此我的所有实体都是由组件组成的 现在,在渲染器方法中,我检查当前实体是否具有BodyComponent。如果为true,则它将在具有米视口的PhysicCam中绘制精灵。否则,它将只在sceneCam中绘制精灵,它有一个像素视口 我的问题是,我试图根据身体的位置(以米为单位)绘制精灵,但它并不是精确地绘制在身体所在的位置 注意:我还有phy

最近我读到,只要您使用Box2d,您就可以使用2个摄像头,它只能以米为单位工作

在渲染器类中,我接收游戏中的所有实体,然后绘制它们。 我使用ashley库,因此我的所有实体都是由组件组成的

现在,在渲染器方法中,我检查当前实体是否具有
BodyComponent
。如果为true,则它将在具有米视口的
PhysicCam
中绘制精灵。否则,它将只在sceneCam中绘制精灵,它有一个像素视口

我的问题是,我试图根据身体的位置(以米为单位)绘制精灵,但它并不是精确地绘制在身体所在的位置

注意:我还有
physicdebugsystem
,它基本上只调用
DebugRenderer.render()
方法

以下是我的RenderSystem类:

package engine.systems;

import com.badlogic.ashley.core.Component;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;

import engine.Values;
import engine.components.BodyComponent;
import engine.components.SpriteComponent;
import engine.components.TransformComponent;

public class RenderSystem extends IteratingSystem{

    private SpriteBatch batch;
    private Array<Entity> renderQueue;
    private OrthographicCamera sceneCam;
    private OrthographicCamera physicsCam;
    private ComponentMapper<SpriteComponent> txt;
    private ComponentMapper<TransformComponent> trs;

    @SuppressWarnings("unchecked")
    public RenderSystem(SpriteBatch batch) {
        super(Family.all(TransformComponent.class, SpriteComponent.class).get());
        txt = ComponentMapper.getFor(SpriteComponent.class);
        trs = ComponentMapper.getFor(TransformComponent.class);
        sceneCam = new OrthographicCamera(Values.WIDTH, Values.HEIGHT);
        sceneCam.setToOrtho(false);
        physicsCam = new OrthographicCamera(Values.WIDTH, Values.HEIGHT);
        physicsCam.setToOrtho(false);
        renderQueue = new Array<Entity>();
        this.batch = batch;
    }

    @Override
    public void update(float deltaTime) {
        super.update(deltaTime);
        Gdx.gl.glClearColor(230/255f, 242/255f, 242/255f, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        sceneCam.update();
        physicsCam.update();
        batch.enableBlending();
        batch.begin();
        for (Entity entity : renderQueue) {
            Component comp = entity.getComponent(BodyComponent.class);
            if(comp == null) {
                batch.setProjectionMatrix(sceneCam.combined);
            } else {
                batch.setProjectionMatrix(physicsCam.combined);
            }
            SpriteComponent texture = txt.get(entity);
            TransformComponent transform = trs.get(entity);
            if (texture.region == null || transform.isHidden) {
                continue;
            }
            texture.region.setPosition(transform.position.x, transform.position.y);
            texture.region.setRotation(transform.rotation);
            texture.region.setScale(transform.scale.x, transform.scale.y);
            texture.region.draw(batch);
        }
        batch.end();
        renderQueue.clear();
    }

    @Override
    protected void processEntity(Entity entity, float deltaTime) {
        renderQueue.add(entity);
    }

    public OrthographicCamera getCamera() {
        return sceneCam;
    }

}
以下是我在模拟过程中拍摄的屏幕截图:

球精灵看起来被渲染为偏离box2d主体的原因是因为纹理的锚定点是它的左下角,这意味着当设置纹理的位置时,基本上设置了它的左下角的位置。要正确定位球,您需要从球的位置减去球的一半宽度和一半高度:

final float halfWidth = texture.region.getRegionWidth() * 0.5f;
final float halfHeight = texture.region.getRegionHeight() * 0.5f;
texture.region.setPosition(transform.position.x - halfWidth, transform.position.y - halfHeight);

我还要提醒你,从你发布的代码来看,你似乎没有缩放你的box2d实体,这意味着你的球是
值。球宽
米。从截图上看,它的半径大约为2.5米。那是一个大球

我读了这篇文章,但我仍然不明白我的代码出了什么问题。我使用一个摄像头用于GUI,一个摄像头用于物理世界。为什么它仍然不工作?只用一个摄像头。在屏幕截图中没有任何你应该使用香蕉单位的东西。不要让事情变得更难。
final float halfWidth = texture.region.getRegionWidth() * 0.5f;
final float halfHeight = texture.region.getRegionHeight() * 0.5f;
texture.region.setPosition(transform.position.x - halfWidth, transform.position.y - halfHeight);