在AndEngine中使用相机追逐玩家并限制世界的界限

caw*_*caw 1 android 2d opengl-es andengine

使用AndEngine for Android,我想让我的场景看起来像这样:

在此输入图像描述

例如,红色框是世界,必须限制在给定的大小2000px*450px.

蓝色框是Camera,也是有限的(通常),例如750px*450px.

对于整个场景,我的背景图像非常450px高.所以我Camera可以缩放到适当的大小,但背景必须完全适合高度.宽度Camera可以是可变的.

玩家(圈子)必须始终位于中心(水平),但可能不会离开世界的边界.

为此,我尝试添加两种尺寸:

  • 相机尺寸(CAMERA_WIDTH,CAMERA_HEIGHT)
  • 世界大小(WORLD_WIDTH,WORLD_HEIGHT)

而这个功能是为世界添加边界,以便物理引擎阻止玩家离开这些边界:

private void createWorldBoundaries() {
    Body body;
    final Rectangle wall_top = new Rectangle(0, WORLD_HEIGHT-5, WORLD_WIDTH, 10, mVertexManager);
    final Rectangle wall_bottom = new Rectangle(0, 5, WORLD_WIDTH, 10, mVertexManager);
    final Rectangle wall_left = new Rectangle(5, 0, 10, WORLD_HEIGHT, mVertexManager);
    final Rectangle wall_right = new Rectangle(WORLD_WIDTH-5, 0, 10, WORLD_HEIGHT, mVertexManager);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_top, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_top.setUserData(body);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_bottom, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_bottom.setUserData(body);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_left, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_left.setUserData(body);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_right, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_right.setUserData(body);
    attachChild(wall_top);
    attachChild(wall_bottom);
    attachChild(wall_left);
    attachChild(wall_right);
}
Run Code Online (Sandbox Code Playgroud)

但不幸的是,这不起作用.(见编辑)

设置相机以追逐播放器对我来说有错误的结果:播放器确实始终保持在屏幕的中心,但我希望播放器只能水平放置在中心,而不是垂直放置.

我做错了什么,我能改变什么?基本问题是:如何使世界比相机视图更宽,而高度等于相机视图.结果应该是你可以横向穿过你的世界(移动相机),你总能看到全高.

编辑:

当你定义中心的坐标Rectangle而不是它的左上角时,你必须像这样做,看来:

final Rectangle wall_top = new Rectangle(WORLD_WIDTH/2, WORLD_HEIGHT-1, WORLD_WIDTH, 2, mVertexManager);
final Rectangle wall_bottom = new Rectangle(WORLD_WIDTH/2, FIELD_BASELINE_Y+1, WORLD_WIDTH, 2, mVertexManager);
final Rectangle wall_left = new Rectangle(1, WORLD_HEIGHT/2, 2, WORLD_HEIGHT, mVertexManager);
final Rectangle wall_right = new Rectangle(WORLD_WIDTH-1, WORLD_HEIGHT/2, 2, WORLD_HEIGHT, mVertexManager);
Run Code Online (Sandbox Code Playgroud)

但是,我在几个教程中找到了另一个解决方案.这些作者是否在编写教程之前没有测试他们的代码,或者行为从GLES1更改为GLES2或是否有任何最新版本?

Chr*_* R. 6

我认为你关于世界边界的问题是自我回答的,不是吗?

PhysicsWorld Boundaries

如需进一步研究,您可以从Play商店下载nicolas的AndEngine Examples App,并在此查找不同的示例(GLES_2,尚未查找AnchorCenter):https://github.com/nicolasgramlich/AndEngineExamples/tree/GLES2/ SRC /组织/ andengine /例子

取自PhysicsExample,如果边界设置为摄像机边界,则矩形的代码应如下所示.在你的情况下,你可以像你想要的那样扩展宽度(3倍CAMERA_WIDTH?)

    final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, WORLD_WIDTH, 2, vertexBufferObjectManager);
    final Rectangle roof = new Rectangle(0, 0, WORLD_WIDTH, 2, vertexBufferObjectManager);
    final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
    final Rectangle right = new Rectangle(WORLD_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
Run Code Online (Sandbox Code Playgroud)

相机跟随玩家 的相机跟随你的播放器,你可以查找BoundCameraExample的代码https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/BoundCameraExample.java

有趣的部分应该是底部的addFace方法

private void addFace(final float pX, final float pY) {
    final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
final AnimatedSprite face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager()).animate(100);
final Body body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));

this.mBoundChaseCamera.setChaseEntity(face);
}
Run Code Online (Sandbox Code Playgroud)

这个方法为"你的玩家"(在这种情况下是一个盒装的脸)创建一个物理体+精灵,并将精灵设置为摄影师要追随的chaseEntity.由于相机有界限,它不能超过你的相机将具有PhysicWorld边界的高度,你可以使用它来让你的相机跟随x中的播放器,而不是y方向.

如果你(我不知道为什么)不想使用这些边界,你可以覆盖你的Sprite的onUpdate方法,只在x方向重新定位你的相机,而不是xy coords

face.registerUpdateHandler(new IUpdateHandler() {
   @Override
   public void onUpdate(final float pSecondsElapsed) {
      float[] coord = face.getSceneCenterCoordinates();
      this.mBoundChaseCamera.setCenter(sceneCenterCoordinates[0], CAMERA_Y_POSITION);
   }
}
Run Code Online (Sandbox Code Playgroud)

其中CAMERA_Y_POSITION是具有y位置的静态最终字段.

我希望这能回答你的问题).:-)

编辑:哎呀,我忘了提,如何实现相机绑定,我将编辑上面的世界宽度:

    this.mBoundChaseCamera.setBounds(0, 0,
            WORLD_WIDTH, CAMERA_HEIGHT);
Run Code Online (Sandbox Code Playgroud)

所有设置都与您给出的图像相似(除了脸部的确切位置,必须给予addFace(px, py))

编辑:Andengine GLES2与GLES2-AnchorCenter中场景边界之间的差异

据我所知,我认为你会使用GLES2,我想到了AndEngine的(较旧的)默认GLES2分支并发布了边界.正如您之前发现并在评论中说明的那样,您可以使用另一种方法来设置矩形 - 您需要将矩形中心设置为pX和pY.其实的原因是,使用AnchorCenter分支时,您不再设置实体的左上角位置,而是使用它的中心位置.