久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

讓背景或相機(jī)“滾動(dòng)"基于字符位置

Having the Background or Camera quot;Scrollquot; based on charcter position(讓背景或相機(jī)“滾動(dòng)基于字符位置)
本文介紹了讓背景或相機(jī)“滾動(dòng)"基于字符位置的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在開(kāi)發(fā)一款具有自上而下視圖的 RPG 游戲.我想將圖片加載到角色正在行走的背景中,但到目前為止,我還沒(méi)有弄清楚如何正確重繪背景以使其滾動(dòng)".我發(fā)現(xiàn)的大多數(shù)示例都是自動(dòng)滾動(dòng)的.

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.

我希望相機(jī)始終以角色為中心,直到背景圖像到達(dá)其邊界,然后角色將移動(dòng)而無(wú)需在另一個(gè)位置重新繪制圖像.

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.

推薦答案

你的問(wèn)題有點(diǎn)不清楚,但我想我明白了.讓我們看看您的要求.

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

  1. 您有一個(gè)頂置攝像頭,可以直接俯視二維平面.我們可以將其表示為一個(gè)簡(jiǎn)單的 {x, y} 坐標(biāo)對(duì),對(duì)應(yīng)于相機(jī)正在查看的平面上的點(diǎn).
  2. 攝像頭可以跟蹤某些對(duì)象的運(yùn)動(dòng),可能是玩家,但更普遍的是游戲世界中的任何事物.
  3. 相機(jī)必須保持在游戲世界的有限范圍內(nèi).

這很容易實(shí)現(xiàn).從廣義上講,在您的 Update() 方法中的某個(gè)地方,您需要執(zhí)行一些步驟來(lái)滿足這些要求中的每一個(gè):

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

換句話說(shuō):如果我們有一個(gè)目標(biāo)對(duì)象,將我們的位置鎖定到它的位置;但請(qǐng)確保我們不會(huì)越界.

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() 也很容易實(shí)現(xiàn).假設(shè)您有一個(gè)對(duì)象 world,其中包含一個(gè) Bounds 屬性,該屬性以像素為單位表示世界范圍:

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

這可確保攝像頭與世界邊緣的距離不會(huì)超過(guò)屏幕的一半.為什么是半屏?因?yàn)槲覀儗⑾鄼C(jī)的 {x, y} 定義為相機(jī)正在注視的點(diǎn),這意味著它應(yīng)該始終位于屏幕的中心.

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.

這應(yīng)該會(huì)為您提供一個(gè)具有您在問(wèn)題中指定的行為的相機(jī).從這里開(kāi)始,只需實(shí)現(xiàn)地形渲染器,以便相對(duì)于相機(jī)對(duì)象指定的 {x, y} 坐標(biāo)繪制背景.

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.

給定對(duì)象在游戲世界坐標(biāo)中的位置,我們可以將該位置轉(zhuǎn)換為相機(jī)空間:

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;

然后從相機(jī)空間進(jìn)入屏幕空間:

And then from camera space into screen space:

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

然后您可以使用對(duì)象的屏幕空間坐標(biāo)來(lái)渲染它.

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

這篇關(guān)于讓背景或相機(jī)“滾動(dòng)"基于字符位置的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Is there a C# library that will perform the Excel NORMINV function?(是否有執(zhí)行 Excel NORMINV 函數(shù)的 C# 庫(kù)?)
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權(quán)列表中選擇 x 個(gè)隨機(jī)元素(無(wú)需替換))
Create a summary description of a schedule given a list of shifts(給定輪班列表,創(chuàng)建時(shí)間表的摘要描述)
C# Normal Random Number(C# 普通隨機(jī)數(shù))
Standard deviation of generic list?(通用列表的標(biāo)準(zhǔn)偏差?)
AsyncCTP: Creating a class that is IAwaitable(AsyncCTP:創(chuàng)建一個(gè) IAwaitable 的類(lèi))
主站蜘蛛池模板: 精品亚洲国产成人av制服丝袜 | 午夜精品久久久久久久99 | 日韩精品不卡 | 伦一理一级一a一片 | 一级片免费在线观看 | 国产视频福利 | 日韩三级影院 | 一区二区国产精品 | 欧美日韩在线免费 | 五月色综合 | 国产91免费 | 性色av一区二区 | 日皮视频在线观看 | 一级做a视频 | 日本加勒比在线 | 久久精品视频一区二区 | 三级网站在线 | 精品欧美一区二区三区久久久 | 国产日韩欧美 | 成人免费网站黄 | 蜜臀久久99精品久久久久宅男 | 国产一区二区网站 | 手机看片欧美 | 成人性色生活片 | 国产精品成人国产乱一区 | 99久久久国产精品 | 九九九色 | 一级特黄aaaaaa大片 | 欧产日产国产69 | 精品免费视频 | 黄色网址av | 欧美视频三区 | 日韩福利视频 | 五月亚洲 | 这里只有精品在线观看 | 免费黄视频网站 | 高潮毛片又色又爽免费 | 亚洲专区一区 | 中文有码在线 | 欧美资源在线 | 精品自拍视频 |