Java 精灵没有';相机平移时,请不要跟随box2d主体,但如果相机在libgdx中是静态的,则可以正常工作
我的问题是,当我不移动相机时,我的精灵会很好地跟随我的box2d身体。但是当我实现滚动时,我使用cam.translate将精灵移动到每一帧的高度,直到它消失。 奇怪的是,如果我检查我的身体和精灵之间的坐标是否完全相同 更新: 我尝试使用cam.position.set(cam.position.x,cam.position.y*1.001f,0)来重新放置cam.translate,但我遇到了同样的问题 变量包括:Java 精灵没有';相机平移时,请不要跟随box2d主体,但如果相机在libgdx中是静态的,则可以正常工作,java,camera,libgdx,sprite,Java,Camera,Libgdx,Sprite,我的问题是,当我不移动相机时,我的精灵会很好地跟随我的box2d身体。但是当我实现滚动时,我使用cam.translate将精灵移动到每一帧的高度,直到它消失。 奇怪的是,如果我检查我的身体和精灵之间的坐标是否完全相同 更新: 我尝试使用cam.position.set(cam.position.x,cam.position.y*1.001f,0)来重新放置cam.translate,但我遇到了同样的问题 变量包括: private GalaxyFighter jeu; //render va
private GalaxyFighter jeu;
//render variables
private OrthographicCamera cam;
private Viewport gamePort;
private Hud hud;
private float maxScrolling;
//tilded map variables
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
//box2d variables
private World world;
private Box2DDebugRenderer b2dr;
private SpaceShip player;
private Sprite spriteSpaceShip;
建造商:
public EcranJeu(GalaxyFighter jeu, float maxScrolling){
this.jeu = jeu;
cam = new OrthographicCamera();
gamePort = new FitViewport(GalaxyFighter.V_WIDTH / GalaxyFighter.PPM, GalaxyFighter.V_HEIGHT / GalaxyFighter.PPM, cam);
hud = new Hud(jeu.batch);
this.maxScrolling = maxScrolling;
maploader = new TmxMapLoader();
map = maploader.load("level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / GalaxyFighter.PPM);
cam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0,0), true);
b2dr = new Box2DDebugRenderer();
player = new SpaceShip(world);
spriteSpaceShip = new Sprite(new Texture("redship1.png"));
player.b2body.setUserData(spriteSpaceShip);
new B2WorldCreator(world, map);
world.setContactListener(new WorldContactListener());
}
以下是我在render中调用的更新方法:
public void update(float dt){
handleInput(dt);
world.step(1/60f, 6, 2);
if(cam.position.y < (maxScrolling / GalaxyFighter.PPM))
cam.translate(0, 0.5f / GalaxyFighter.PPM);
limitPlayer();
spriteSpaceShip.setPosition(player.b2body.getPosition().x * GalaxyFighter.PPM - 8, player.b2body.getPosition().y * GalaxyFighter.PPM - 8);
System.out.println("Pos player x = " + (player.b2body.getPosition().x * GalaxyFighter.PPM - 8) + " y = " + (player.b2body.getPosition().y * GalaxyFighter.PPM - 8));
System.out.println("Pos sprite x = " + spriteSpaceShip.getX() + " y = " + spriteSpaceShip.getY());
cam.update();
renderer.setView(cam);
}
我不知道哪里是我的错误,所以我希望有人会发现它!
谢谢你抽出时间
编辑:这是我正在使用的另一个类,我完成了gameScreen类
抬头显示器
以及作为玩家的宇宙飞船:
public class SpaceShip extends Sprite {
public World world;
public Body b2body;
public SpaceShip(World world){
this.world = world;
defineSpaceShip();
}
public void defineSpaceShip(){
BodyDef bdef = new BodyDef();
bdef.position.set(GalaxyFighter.V_WIDTH / 2 / GalaxyFighter.PPM, 3 / GalaxyFighter.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(5 / GalaxyFighter.PPM);
fdef.filter.categoryBits = GalaxyFighter.SPACESHIP_BIT;
fdef.filter.maskBits = GalaxyFighter.DEFAULT_BIT | GalaxyFighter.ASTEROIDE_BIT |GalaxyFighter.BONUS1_BIT|GalaxyFighter.BONUS2_BIT|GalaxyFighter.BONUS3_BIT|GalaxyFighter.PISTE_BIT|GalaxyFighter.BORDSMAP_BIT;
fdef.shape = shape;
b2body.createFixture(fdef);
EdgeShape head_spaceship = new EdgeShape();
head_spaceship.set(new Vector2(-4 / GalaxyFighter.PPM, 0), new Vector2(4 / GalaxyFighter.PPM, 0));
fdef.shape = head_spaceship;
fdef.isSensor = true;
b2body.createFixture(fdef).setUserData("head_spaceship");
EdgeShape head_spaceship2 = new EdgeShape();
head_spaceship2.set(new Vector2(0, -4 / GalaxyFighter.PPM), new Vector2(0, 4 / GalaxyFighter.PPM));
fdef.shape = head_spaceship2;
fdef.isSensor = true;
b2body.createFixture(fdef).setUserData("head_spaceship2");
}
你有没有试过将相机的位置设置为玩家的位置,而不仅仅是线性平移?像这样:
cam.position.set(0, spriteSpaceShip.getY(), 0);
你有没有试过将相机的位置设置为玩家的位置,而不仅仅是线性平移?像这样:
cam.position.set(0, spriteSpaceShip.getY(), 0);
您从
hud.stage.getCamera()
获取的Camera
与cam
相同吗?您似乎正在使用一个摄影机渲染Box2D调试视图,而使用另一个摄影机渲染SpriteSpace。是的,正如您在HUD类中看到的那样,我使用了另一个cam。这是一个问题吗?请尝试将同一个摄影机用于Box2D调试渲染器和Sprite。您的意思是替换为:“jeu.batch.setProjectionMatrix(HUD.stage.getCamera().组合)对这个:“jeu.batch.setProjectionMatrix(cam.combined);”因为我这么做了,它什么也没有改变,cam.translate(0,0.5f/GalaxyFighter.PPM)中的PPM是什么意思
?从hud.stage.getCamera()
获取的摄像机是否与cam
相同?您似乎正在使用一个摄影机渲染Box2D调试视图,而使用另一个摄影机渲染SpriteSpace。是的,正如您在HUD类中看到的那样,我使用了另一个cam。这是一个问题吗?请尝试将同一个摄影机用于Box2D调试渲染器和Sprite。您的意思是替换为:“jeu.batch.setProjectionMatrix(HUD.stage.getCamera().组合)对这个:“jeu.batch.setProjectionMatrix(cam.combined);”因为我这么做了,它什么也没有改变,cam.translate(0,0.5f/GalaxyFighter.PPM)中的PPM是什么意思代码>?camera类中不存在.setPosition()和正交cameraI我不想带着相机跟随玩家我想做一个滚动游戏,在这个游戏中,相机向上移动,玩家被限制在相机区域,就像我做的那样。问题是,当我不翻译相机时,一切正常,精灵跟随我的box2d对象,但当我一点一点地激活滚动时,精灵会一直向上移动,直到消失。试试看,我只是再次编辑了它。现在它只跟随y轴上的播放器哦,我明白了,等一下。对不起,我好像弄不明白。the.setPosition()在camera类和正交cameraI中不存在我不想跟随带摄像头的玩家我想做一个滚动游戏,其中摄像头上升,玩家被限制在摄像头区域,我做了。问题是,当我不翻译相机时,一切正常,精灵跟随我的box2d对象,但当我一点一点地激活滚动时,精灵会一直向上移动,直到消失。试试看,我只是再次编辑了它。现在它只跟随y轴上的玩家哦,我明白了,等等。对不起,我好像弄不明白。
cam.position.set(0, spriteSpaceShip.getY(), 0);