游戏概述:


2D
世界,玩家和相机是分开的




我正在尝试制作一个游戏,其中摄影机跟随玩家并遵循中间路线(因此先移动x +然后x-会导致少许摄影机移动),但我已经做到了,所以摄影机具有自己的加速度,但是我不知道如何减速。

相关的值

List<double> offset = new List<double>() { 0, 0 }; //Top left map to top left window (World is related to this)
List<double> smiley = new List<double>() { 0, 0 - 5 }; //Player position inside the map
List<double> smileyPos = new List<double>() { 400, 288 + 11 }; //Where the centre of the screen is for the player

List<double> scvel = new List<double>() { 0, 0 }; //Stores how many pixels to move per frame
int maxvel = 8; //Maximum amount of pixels to move per frame
double acc = 0.5; //acceleration (only)




            if (offset[0]+smiley[0] < smileyPos[0])
            {
                if (scvel[0] <= maxvel)
                {
                    scvel[0] += acc;
                }
            }
            if (offset[0]+smiley[0] > smileyPos[0])
            {
                if (scvel[0] >= -maxvel)
                {
                    scvel[0] -= acc;
                }
            }
            if (offset[0] + smiley[0] == smileyPos[0])
            {
                scvel[0] = 0;
            }
            if (offset[1] + smiley[1] < smileyPos[1])
            {
                if (scvel[1] <= maxvel)
                {
                    scvel[1] += acc;
                }
            }
            if (offset[1] + smiley[1] > smileyPos[1])
            {
                if (scvel[1] >= -maxvel)
                {
                    scvel[1] -= acc;
                }
            }
            offset[0] += scvel[0];
            offset[1] += scvel[1];


输出:

Camera smoothly moves to the player but orbits the player


我想要的输出:

Camera smoothly eases to the player position with a slight delay to counteract any bumps


我做摄像机运动的唯一角度是45度的倍数-我将如何修改此代码以使摄像机直接进入播放器并轻松进出?







offset = Vector2.Lerp(offset, new Vector2((float)(-smiley[0]+smileyPos[0]), (float)(-smiley[1]+smileyPos[1])), (float)0.05);


并将List<int> offset更改为Vector2 offset

最佳答案

Looks like a perfect use for Vector2.Lerp(position, goal, amount)

position ==对象当前所在的位置。

目标==您希望对象顺利到达的位置。

数量==它覆盖此框架的方式的百分比。

因此,将数量设置为0到1之间的某个值。如果将其设置为.5,则它将覆盖每帧到目标的距离的一半。如果您考虑一下,由于每帧的距离是不断减小的一半,因此实际上,每帧的移动量会减少,从而使其在接近目标时会减速。

您可以通过反复试验找到最佳的amount。很可能是一个小于0.1的低数字。

关于c# - XNA仓位之间的成本核算/放宽,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28479978/

10-13 06:00