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

时间:2013-07-05 20:09:09

标签: android opengl-es 2d andengine

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

enter image description here

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

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

对于整个场景,我的背景图像正好450px高。所以我的Camera可以缩放到任何合适的大小,但背景必须完全适合高度。 Camera的宽度可以是变量。

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

为实现这一目标,我尝试添加两种尺寸:

  • 相机尺寸(CAMERA_WIDTHCAMERA_HEIGHT
  • 世界尺寸(WORLD_WIDTHWORLD_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);
}

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

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

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

修改

当您定义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);

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

1 个答案:

答案 0 :(得分:6)

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

PhysicsWorld Boundaries

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

取自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);

相机跟随播放器 要让相机跟随您的播放器,您可以查找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);
}

这个方法为“你的玩家”(在这种情况下是一个盒装的脸)创建一个物理体+精灵,并将精灵设置为摄像机要遵循的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);
   }
}

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

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

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

    this.mBoundChaseCamera.setBounds(0, 0,
            WORLD_WIDTH, CAMERA_HEIGHT);

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

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

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