C# 单向移动

C# 单向移动,c#,monogame,C#,Monogame,我正在创建一个基于小行星的2D游戏。在那个游戏中,我需要把船推向一个方向 我能画出这艘船,让它掉头。但说到推进,我的问题就来了 我好像没法理解这件事。(整个制作游戏对我来说都是新鲜事^^) player.cs protected Vector2 sVelocity; protected Vector2 sPosition = Vector2.Zero; protected float sRotation; private int speed; public Player(Vector2 sPo

我正在创建一个基于小行星的2D游戏。在那个游戏中,我需要把船推向一个方向

我能画出这艘船,让它掉头。但说到推进,我的问题就来了

我好像没法理解这件事。(整个制作游戏对我来说都是新鲜事^^)

player.cs

protected Vector2 sVelocity;
protected Vector2 sPosition = Vector2.Zero;
protected float sRotation;
private int speed;

public Player(Vector2 sPosition)
        : base(sPosition)
{
    speed = 100;
}

public override void Update(GameTime gameTime)
{
    attackCooldown += (float)gameTime.ElapsedGameTime.TotalSeconds;

    // Reset the velocity to zero after each update to prevent unwanted behavior
    sVelocity = Vector2.Zero;

    // Handle user input
    HandleInput(Keyboard.GetState(), gameTime);

    if (sPosition.X <= 0)
    {
        sPosition.X = 10;
    }

    if (sPosition.X >= Screen.Instance.Width)
    {
        sPosition.X = 10;
    }

    if(sPosition.Y <= 0)
    {
        sPosition.Y = 10;
    }

    if (sPosition.Y >= Screen.Instance.Height)
    {
        sPosition.Y = 10;
    }

    // Applies our speed to velocity
    sVelocity *= speed;

    // Seconds passed since iteration of update
    float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // Multiplies our movement framerate independent by multiplying with deltaTime
    sPosition += (sVelocity * deltaTime);

    base.Update(gameTime);
}

private void HandleInput(KeyboardState KeyState, GameTime gameTime)
{
    if (KeyState.IsKeyDown(Keys.W))
    {
        //Speed up
        speed += 10;
        sVelocity.X = sRotation; // I know this is wrong
        sVelocity.Y = sRotation; // I know this is wrong
    }
    else
    {
        //Speed down
        speed += speed / 2;
    }

    if (KeyState.IsKeyDown(Keys.A))
    {
        //Turn left
        sRotation -= 0.2F;
        if (sRotation < 0)
        {
            sRotation = sRotation + 360;
        }
    }
    if (KeyState.IsKeyDown(Keys.D))
    {
        //Turn right
        sRotation += 0.2F;
        if (sRotation > 360)
        {
            sRotation = sRotation - 360;
        }
    }
}
受保护的向量2 sVelocity;
受保护向量2位置=向量2.0;
保护浮子旋转;
私人整数速度;
公共播放器(矢量2位置)
:底座(位置)
{
速度=100;
}
公共覆盖无效更新(游戏时间游戏时间)
{
攻击冷却时间+=(浮动)gameTime.ElapsedGameTime.TotalSeconds;
//每次更新后将速度重置为零,以防止出现不必要的行为
sVelocity=Vector2.0;
//处理用户输入
HandleInput(Keyboard.GetState(),gameTime);
if(sPosition.X=Screen.Instance.Width)
{
位置X=10;
}
if(sPosition.Y=Screen.Instance.Height)
{
sPosition.Y=10;
}
//将速度应用于速度
sVelocity*=速度;
//更新迭代后已过秒
float deltaTime=(float)gameTime.ElapsedGameTime.TotalSeconds;
//通过与deltaTime相乘,将我们的运动帧率独立相乘
sPosition+=(sVelocity*deltaTime);
更新(游戏时间);
}
私有void HandleInput(键盘状态键状态,游戏时间游戏时间)
{
if(KeyState.IsKeyDown(Keys.W))
{
//加速
速度+=10;
sVelocity.X=sRotation;//我知道这是错的
sVelocity.Y=sRotation;//我知道这是错的
}
其他的
{
//减速
速度+=速度/2;
}
if(KeyState.IsKeyDown(Keys.A))
{
//左转
旋转-=0.2F;
如果(旋转<0)
{
旋转=旋转+360;
}
}
if(键状态IsKeyDown(键D))
{
//右转
旋转+=0.2F;
如果(旋转>360)
{
旋转=旋转-360度;
}
}
}

我是靠得很近,还是离右边很远?

旋转是一个角度,
sVelocity
是一个速度。你需要三角学

例如,您可以使用类似的方法(我没有测试符号的正确性):

这能解决你的问题吗

编辑:你的“减速”公式是错误的。您当前正在使用
speed
添加
speed/2
,您应该随身携带一些东西:

speed = speed / 2; // note the "=", not "+="
此外,最好使用以下内容:

if (speed > 0) { 
    speed -= 5;
} else {
    speed = 0;
}

sRotation
是一个角度,
sVelocity
是一个速度。你需要三角学

例如,您可以使用类似的方法(我没有测试符号的正确性):

这能解决你的问题吗

编辑:你的“减速”公式是错误的。您当前正在使用
speed
添加
speed/2
,您应该随身携带一些东西:

speed = speed / 2; // note the "=", not "+="
此外,最好使用以下内容:

if (speed > 0) { 
    speed -= 5;
} else {
    speed = 0;
}

我试过了。但它仍然不起作用。好像飞船在画面外快速移动,我试着用1000除以,但没有改变。嗯。。。也许cos和sin使用弧度,而你使用度?另外,经过几次迭代后,你能在调试程序中获得
速度
的值吗?我有一种感觉它可能有很大的价值。。。也许你可以在一些测试中把它固定在1。我如何检查我使用的是哪一个?速度在负某物到正某物之间跳跃。在+某物和-某物之间的速度不好。您现在应该将其修复,并使用较小的值(大约5)。尝试用
speed=5
替换
speed+=10
,在其他情况下用
speed/=2
替换
speed=0
。不是很顺利,只是一个测试。只是试了一下。但它仍然不起作用。好像飞船在画面外快速移动,我试着用1000除以,但没有改变。嗯。。。也许cos和sin使用弧度,而你使用度?另外,经过几次迭代后,你能在调试程序中获得
速度
的值吗?我有一种感觉它可能有很大的价值。。。也许你可以在一些测试中把它固定在1。我如何检查我使用的是哪一个?速度在负某物到正某物之间跳跃。在+某物和-某物之间的速度不好。您现在应该将其修复,并使用较小的值(大约5)。尝试用
speed=5
替换
speed+=10
,在其他情况下用
speed/=2
替换
speed=0
。不是平稳的,而是一个测试。你在每一帧中都做
sVelocity*=speed
:如果
speed
10
,而
sVelocity
(0,1)
,你将很快以
(01000000000000000)
的速度移动(也就是说:在3帧中,你将以每帧100万个单位的速度移动)。我怀疑line是你意外移动的罪魁祸首…@DanPuzey他还有
sVelocity=Vector.Zero
每帧。可能是速度值本身失去了控制。@dureuill:啊,我错过了重置。你可能是对的-控制过快的速度将有助于了解旋转的情况。你在每一帧中都做
sVelocity*=速度
:如果
速度
10
并且
sVelocity
(0,1)
你将很快以
(01000000000000000)
(即:在3帧中,每帧移动100万个单位)。我怀疑直线是你意外移动的最大罪魁祸首…@DanPuzey他也有
sVelocity=Vector.Zero
每帧。可能是
速度
值本身失控。@dureuill:啊,我错过了重置。你可能是对的-控制过快将有助于看到什么旋转正在进行。