XNA - объект не двигается правильно - PullRequest
1 голос
/ 29 октября 2010

Хорошо, у меня есть этот корабль, который пользователь управляет клавишами со стрелками.

        if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
            mAngle -= 0.1f;
        else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
            mAngle += 0.1f;

        if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
        {
           // mSpeed.Y = SHIP_SPEED;
           // mDirection.Y = MOVE_UP;

            velocity.X = (float)Math.Cos(mAngle) * SHIP_SPEED;
            velocity.Y = (float)Math.Sin(mAngle) * SHIP_SPEED;
        }
        else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
        {
            mSpeed.Y = SHIP_SPEED;
            mDirection.Y = MOVE_DOWN;
        }

Следующие 2 метода находятся в другом классе.

//Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
    public void Update(GameTime theGameTime, Vector2 velocity, float theAngle)
    {
        Position += velocity * (float)theGameTime.ElapsedGameTime.TotalSeconds;
        shipRotation = theAngle;
    }

    //Draw the sprite to the screen
    public void Draw(SpriteBatch theSpriteBatch)
    {
        Vector2 Origin = new Vector2(mSpriteTexture.Width / 2, mSpriteTexture.Height / 2);

        theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
            Color.White, shipRotation, Origin, Scale, SpriteEffects.None, 0);
    }

Проблема в том, что программа думает, что передняя часть корабля - это одно из боковых крыльев, поэтому, когда я нажимаю вверх, она отклоняется в сторону Я не знаю, почему это так.

Предложения

Ответы [ 3 ]

1 голос
/ 15 марта 2011

Легко, поиграйте с этими двумя строчками:

        velocity.X = (float)Math.Cos(mAngle) * SHIP_SPEED;
        velocity.Y = (float)Math.Sin(mAngle) * SHIP_SPEED;

Например, переключите sin и cos и сделайте одну из них отрицательной:

        velocity.X = -(float)Math.Sin(mAngle) * SHIP_SPEED;
        velocity.Y = (float)Math.Cos(mAngle) * SHIP_SPEED;

Если она идет назад сейчасПросто переключите отрицательное значение:

        velocity.X = (float)Math.Sin(mAngle) * SHIP_SPEED;
        velocity.Y = -(float)Math.Cos(mAngle) * SHIP_SPEED;
0 голосов
/ 29 октября 2010

Затем измените свой исходный спрайт так, чтобы он соответствовал тому, что "программа" считает "передней частью корабля".

0 голосов
/ 29 октября 2010

Если я правильно понимаю вашу проблему, вы можете решить ее, просто повернув текстуру своего корабля. Просто попробуйте добавить 90 или -10 к вашему shipRotation в методе Draw:

theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
        Color.White, shipRotation + 90, Origin, Scale, SpriteEffects.None, 0);

или

theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
        Color.White, shipRotation - 90, Origin, Scale, SpriteEffects.None, 0);

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...