有背景或摄像头和QUOT;滚动"基于字符内的位置 [英] Having the Background or Camera "Scroll" based on charcter position

查看:161
本文介绍了有背景或摄像头和QUOT;滚动"基于字符内的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个RPG游戏,有一个自顶向下视图。我想加载图片到被什么字符走在后台,但到目前为止,我还没有想出如何正确有背景重绘,这样它的滚动。大部分时间我发现的例子是自动滚动。

I'm working on an RPG game that has a Top-Down view. I want to load a picture into the background which is what the character is walking on, but so far I haven't figured out how to correctly have the background redraw so that it's "scrolling". Most of the examples I find are auto scrolling.

我希望相机仍然集中在,直到你的背景图像达到其边界的字符,则字符将移至不重画在其他位置的图像。

I want the camera to remained centered at the character until you the background image reaches its boundaries, then the character will move without the image re-drawing in another position.

推荐答案

你的问题有点不清楚,但我觉得我得到它的要点。让我们来看看你的要求。

Your question is a bit unclear, but I think I get the gist of it. Let's look at your requirements.

  1. 您有开销相机的寻找直接向下到一个二维平面上。我们可以重新present此作为一个简单的{X,Y}坐标对,对应于在飞机上的点的摄像机正在
  2. 摄像机可以跟踪某个对象的游戏世界中的运动,可能是球员,但更普遍的东西。
  3. 相机必须保持游戏世界的有限范围内。

这是很简单的实现。从广义上讲,地方你的更新()方法中,你需要进行的步骤,履行各自的这些要求:

Which is simple enough to implement. In broad terms, somewhere inside your Update() method you need to carry out steps to fulfill each of those requirements:

if (cameraTarget != null)
{
    camera.Position = cameraTarget.Position;
    ClampCameraToWorldBounds();
}

在换句话说,如果我们有一个目标对象,锁定了我们的立场,以自己的立场;但要确保我们不会去出界。

In other words: if we have a target object, lock our position to its position; but make sure that we don't go out of bounds.

ClampCameraToBounds()也容易实现。假设你有一些对象,全球,其中包含一个属性,再presents在全球范围内像素:

ClampCameraToBounds() is also simple to implement. Assuming that you have some object, world, which contains a Bounds property that represents the world's extent in pixels:

private void ClampCameraToWorldBounds()
{
    var screenWidth = graphicsDevice.PresentationParameters.BackBufferWidth;
    var screenHeight = graphicsDevice.PresentationParameters.BackBufferHeight;

    var minimumX = (screenWidth / 2);
    var minimumY = (screnHeight / 2);

    var maximumX = world.Bounds.Width - (screenWidth / 2);
    var maximumY = world.Bounds.Height - (screenHeight / 2);
    var maximumPos = new Vector2(maximumX, maximumY);

    camera.Position = Vector2.Clamp(camera.Position, minimumPos, maximumPos);
}

这可以确保相机从来没有超过半个屏幕更接近世界的边缘。为什么半个屏幕?因为我们已经定义了相机的{X,Y}为点的相机在看的,这意味着它应始终在屏幕上居中。

This makes sure that the camera is never closer than half of a screen to the edge of the world. Why half a screen? Because we've defined the camera's {x, y} as the point that the camera is looking at, which means that it should always be centered on the screen.

这应该给你,你在你的问题中指定的行为,一个摄像头。从这里,这是实现你的地形渲染的只是一个问题,这样你的背景绘制相对于{X,Y}坐标相机对象指定。

This should give you a camera with the behavior that you specified in your question. From here, it's just a matter of implementing your terrain renderer such that your background is drawn relative to the {x, y} coordinate specified by the camera object.

由于在游戏世界中的物体的位置坐标,我们可以把这个位置到摄像机空间:

Given an object's position in game-world coordinates, we can translate that position into camera space:

var worldPosition = new Vector2(x, y);
var cameraSpace = camera.Position - world.Postion;

然后再从摄像机空间到屏幕空间:

And then from camera space into screen space:

var screenSpaceX = (screenWidth / 2) - cameraSpace.X;
var screenSpaceY = (screenHeight / 2) - cameraSpace.Y;

您可以使用对象的屏幕空间的坐标来呈现它。

You can then use an object's screen space coordinates to render it.

这篇关于有背景或摄像头和QUOT;滚动"基于字符内的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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