如何在旋转后沿精灵方向移动精灵?

时间:2018-05-23 19:56:25

标签: c# monogame

我能够向上,向下,向左和向右移动船只精灵,并旋转它。然而,当“上升”时,船不会总是向上移动。钥匙被按下,我希望船朝船头方向移动。我以为我可以在update方法中的某个地方粘贴一个vector2变量,它总是指向精灵的尖端,但是我不能围绕如何"硬编码"精灵上的坐标。

我将在下面发布我的船级,我觉得事情有点乱,任何关于清理代码的建议都会受到欢迎。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


//width = 62 pixels
//height = 179 pixels

namespace my_first_game
{
class Ship
{

    private Vector2 position = new Vector2(300, 300);
    private int radius = 50;
    private bool ismoving = false;
    private Dir direction = Dir.down;
    private int health = 3;
    private float speed = 100;
    public float angle = 0;
    public double turntime = .5d;

    private float velocity = 10f;
    private float drifttime = 0f;

   // Constructor for the ship class
   public Ship(int shiphealth)
    {
        shiphealth = health;
    }

    //getter/setter for angle
     public float Get_angle
     {
         get { return angle; }
        set { value = angle; }

     }         

    //get and set health
    public int Health
    {
        get { return health; }
        set { value = health; }
    }

    // get and set the ship's position
    public Vector2 Position
    {
        get{ return position; }
        set { value = position; }
    }

    public void SetAngle (float f)
    {
        angle = MathHelper.ToDegrees(f);
    }

    //update loop for the ship's state
    public void update_ship(GameTime gameTime)
    {
        KeyboardState kstate = Keyboard.GetState();
        float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

        float rotation = 0.02f;

        //Vector2 Direction = new Vector2((float)Math.Cos(angle + 45), (float)Math.Sin(angle));
        //Direction.Normalize();           

        //if keys are pressed, the ship moves until the keys are unpressed

        if (kstate.IsKeyDown(Keys.W))
        {
            direction = Dir.up;
            ismoving = true;
        }
        if (kstate.IsKeyDown(Keys.A))
        {
            direction = Dir.left;
            ismoving = true;
        }
        if (kstate.IsKeyDown(Keys.D))
        {
            direction = Dir.right;
            ismoving = true;
        }
        if (kstate.IsKeyDown(Keys.S))
        {
            direction = Dir.down;
            ismoving = true;
        }
        if(kstate.IsKeyDown(Keys.W) && kstate.IsKeyDown(Keys.D))
        { direction = Dir.up_right;
            ismoving = true;
        }
        if(kstate.IsKeyDown(Keys.W) && kstate.IsKeyDown(Keys.A))
        {
            direction = Dir.up_left;
            ismoving = true;
        }
        if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyDown(Keys.D))
        { direction = Dir.down_right;
            ismoving = true;
        }
        if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyDown(Keys.A))
        {
            direction = Dir.down_left;
            ismoving = true;
        }

            //"ismoving" is used as a flag to move the ship if keys are held
            //angle also rotates the boat when "A" and "D" are pressed
            if (ismoving) {

            switch (direction)
            {
                case Dir.up:
                    position.Y -= speed * dt;
                    ismoving = false;
                    break;

                case Dir.down:

                    position.Y += speed * dt;
                    ismoving = false;
                    break;

                case Dir.left:

                    angle -= rotation;
                    ismoving = false;
                    break;

                case Dir.right:
                    angle += rotation;
                    ismoving = false;
                    break;

                case Dir.up_right:
                    position.Y -= speed * dt;
                    position.X += speed * (float)turntime * dt;
                    angle += rotation;
                    ismoving = false;
                    break;

                case Dir.up_left:
                    position.Y -= speed * dt;
                    position.X -= speed * (float)turntime * dt;
                    angle -= rotation;
                    ismoving = false;
                    break;
                case Dir.down_right:
                    position.Y += speed * dt;
                    position.X += speed * (float)turntime * dt;
                    angle += rotation;
                    ismoving = false;
                    break;
                case Dir.down_left:
                    position.Y += speed * dt;
                    position.X -= speed * (float)turntime * dt;
                    angle -= rotation;
                    ismoving = false;
                    break;

                default: break;
            }
            }   
        }
    }
}

这是我的船的相关绘制方法:

//create a new rectangle around the ship_sprite
        Rectangle ship_rectangle = new Rectangle(0, 0, ship_sprite.Width, 
ship_sprite.Height);

        Vector2 origin = new Vector2(31, 160);


        spriteBatch.Draw(ship_sprite, ship.Position, ship_rectangle, 
Color.White, ship.angle, origin, 1.0f, SpriteEffects.None, 1);

2 个答案:

答案 0 :(得分:1)

我怀疑是否有一个"对"回答这个问题,但我要做的是使用你的"角度"按下向上按钮时确定移动的属性。

我正沿着三角学的思路思考 - 你的半径就是船的速度。然后你要按照(伪代码!)的方式做一些事情:

x += (cos(angle) * speed);
y += (sin(angle) * speed);

这假设Angle = 0对应于正常计划上0度的标准解释(面向右侧)。如果不是这种情况,您需要相应调整。

警告:我自己也没有这样编码,所以这只是我对如何实施的估计。我鼓励你尝试看看不同的方法是如何运作的。

答案 1 :(得分:0)

经过一些修修补补后,我找到了一个适合我的解决方案。

Vector2 direction = new Vector2((float)Math.Cos(angle + 30), 
(float)Math.Sin(angle + 30));

然后当' up'按下键,您可以简单地将船的位置设置为当前位置加上船的角度。这两者的总和可以乘以三角洲时间和船的速度。

if(kstate.IsKeyDown(Keys.W))
{
  position = position + direction * dt * speed;   
}