在 AndEngine 中用相机追逐玩家并限制世界的界限 [英] Chase player with camera in AndEngine and limit world's bounds

查看:21
本文介绍了在 AndEngine 中用相机追逐玩家并限制世界的界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using AndEngine for Android, I would like to have my scene look like this:

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

The red box is the world which must be limited to a given size, say 2000px*450px.

蓝色框是Camera,它也有限制(像往常一样),例如750px*450px.

The blue box is the Camera, which is limited as well (as usual), for example to 750px*450px.

对于整个场景,我有一个正好 450px 高的背景图像.所以我的 Camera 可以缩放到任何合适的尺寸,但背景必须完全适合高度.Camera 的宽度可能是可变的.

For the whole scene, I have a background image that is exactly 450px high. So my Camera can be scaled to whatever size is appropriate, but the background must exactly fit to the height. The width of the Camera may be variable.

玩家(圆圈)必须始终位于中心(水平),但不得离开世界边界.

The player (circle) must always be in the center (horizontally) but may not leave the world's boundaries.

为了实现这一点,我尝试添加两种类型的尺寸:

To achieve this, I've tried adding two types of sizes:

  • 相机尺寸(CAMERA_WIDTHCAMERA_HEIGHT)
  • 世界大小(WORLD_WIDTHWORLD_HEIGHT)

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

And this function was to add boundaries to the world so that the physics engine prevents the player from leaving those boundaries:

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);
}

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

设置相机追逐玩家对我来说有错误的结果:玩家确实一直都在屏幕的中心,但我希望玩家只停留在水平中心,而不是垂直中心.

Setting the camera to chase the player has the wrong result for me: The player does really stay in the center of the screen all time, but I want the player only to stay in the center horizontally, not vertically.

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

What am I doing wrong and what can I change? And the basic question is: How can I make the world wider than the camera view, while the height is equal to the camera view. The result should be that you can horizontally walk through your world (moving camera) and you can always see the full height.

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

As you define the coordinates of the Rectangle's center and not its top-left corner, you have to do it like this, it seems:

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 或任何最新版本?

However, I had found the other solution in several tutorials. Are these authors not testing their code before writing the tutorials or did the behaviour change from GLES1 to GLES2 or with any recent version?

推荐答案

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

i think your question about the world boundaries is self answered, isn't it?

物理世界边界

为了进一步研究,您可以从 Play 商店下载 nicolas 的 AndEngine 示例应用程序并在此处查找不同的示例(GLES_2,还没有寻找 AnchorCenter):https://github.com/nicolasgramlich/AndEngineExamples/tree/GLES2/src/org/andengine/examples

for further research you can download nicolas' AndEngine Examples App from the Play Store and look up the different examples here (GLES_2, didn't look for AnchorCenter yet): https://github.com/nicolasgramlich/AndEngineExamples/tree/GLES2/src/org/andengine/examples

取自 PhysicsExample,如果边界设置为相机边界,则矩形的代码应如下所示.在您的情况下,您可以根据需要扩展宽度(3 倍 CAMERA_WIDTH?)

Taken from the PhysicsExample, the code for the rectangles should look like this, if the bounds are set to the camera bounds. in your case, you can extend width like you want (3 times 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

Camera following player for the Camera to follow your player, you can lookup the code of the BoundCameraExample https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/BoundCameraExample.java

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

the interesting part for you should be the addFace method at the bottom

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);
}

此方法为您的玩家"(在本例中为盒装脸)创建了一个物理体 + 精灵,并将精灵设置为相机跟随的追逐实体.由于相机有边界,它不能超过并且你的相机将具有你的 PhysicWorld 边界的高度,你可以使用它让你的相机在 x 方向跟随玩家,而不是在 y 方向.

this method creates a physics body + sprite for "your player" (in this case, a boxed face) and sets the sprite as a chaseEntity for the camera to follow. Since the camera has bounds, that it can't exceed and your camera will have the height of your PhysicWorld boundaries, you can use this to let your camera follow the player in x, but not in y direction.

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

if you (i don't know why) don't want to use these boundaries, you can overwrite the onUpdate method of your Sprite and re-locate your camera only in x-direction, instead of 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 位置的静态最终字段.

where the CAMERA_Y_POSITION is a static final field with the y-position.

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

I hope this answers your question(s). :-)

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

edit: oops, i forgot to mention, how to achieve the camera to be bound and i will edit the world width above:

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

所有设置都像你给定的图像(除了脸部的确切位置,必须给addFace(px, py))

all settings are like your image given (except the exact position of the face, that has to be given to the addFace(px, py))

Andengine GLES2 与 GLES2-AnchorCenter 中场景边界的差异

就我理解的问题而言,我认为您会使用 GLES2,我想到了 AndEngine 的(较旧的)默认 GLES2 分支并张贴了边界.正如您之前发现自己并在评论中所述,您使用另一种方法来设置矩形 - 您需要将矩形中心设置为 pX 和 pY.这样做的原因实际上是,使用 AnchorCenter 分支,您将不再设置实体的左上角位置,而是使用它的中心位置.

As far as i understood the question, i thought you would use GLES2, i thought of the (older) default GLES2 branch of AndEngine and posted the boundaries. As you found out yourself before and stated in the comments, you use another approach to set the rectangles - where you need to set the rectangles center as pX and pY. The reason for this is in fact, that with the AnchorCenter branch, you won't set the upper left position of an entity anymore and instead use it's center position.

这篇关于在 AndEngine 中用相机追逐玩家并限制世界的界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆