以特定角度移动精灵

时间:2013-02-18 19:11:38

标签: c# xna angle sin cos

在我的XNA游戏中,我可以使用A和D键旋转我的精灵,一切正常。但对我的问题:

如何计算X和Y来移动精灵在sprite的角度方向,我有这个:

 float x = (float)Math.Cos(MathHelper.ToRadians(getRotation()));
 float y = (float)Math.Sin(MathHelper.ToRadians(getRotation()));

但这根本不起作用。我想每次按住W键时将精灵移动到+4像素。

3 个答案:

答案 0 :(得分:1)

几乎就在那里:

sprite.x += ( float ) ( Math.Cos( MathHelper.ToRadians( getRotation( ) ) ) * speedX );
sprite.y += ( float ) ( Math.Sin( MathHelper.ToRadians( getRotation( ) ) ) * speedY );

答案 1 :(得分:0)

正如@BlackBear指出的那样,你必须将旋转乘以你想要移动的距离。

当你处理这类事情时,有一个点(或矢量)类封装基础知识 - 加法,缩放,旋转,角度确定等 - 并根据矢量可视化运动。

例如,给出一个功能性的Point2D类:

// define movement vector as +4 units in 'Y' axis
// rotated by player facing
Point2D MoveVector = new Point2D(0, 4).Rotate(Player.FacingAngle);

// adjust player position by movement vector:
Player.Position.Add(MoveVector);

更好的想法可能是让玩家包含一个方向向量,只要面对需要映射到2D坐标就可以使用。然后运动变为:

Player.Position.Add(Player.FacingVector * MoveSpeed);

这对于产生子弹之类的东西很方便,因为你可以将它们的初始面向矢量设置为玩家的面向矢量等等,从而节省了链条上的旋转操作。

你得到额外的好处,你的代码得到更多的自我记录:P

编辑 - 一堆代码:

这是我的Point2D实现的一部分:

public class Point2D
{
    public double X;
    public double Y;

    public Point2D(double X = 0, double Y = 0)
    {
        this.X = X;
        this.Y = Y;
    }

    // update this Point2D by adding other
    public Point2D AddEquals(Point2D other)
    {
        X += other.X;
        Y += other.Y;
        return this;
    }

    // return a new Point2D that is the sum of this and other
    public Point2D Add(Point2D other)
    {
        return new Point2D(X + other.X, Y + other.Y);
    }

    public Point2D Multiply(double scalar)
    {
        return new Point2D(X * scalar, Y * scalar);
    }

    // rotate by angle (in radians)
    public Point2D Rotate(double angle)
    {
        double c = Math.cos(angle), s = Math.sin(angle);
        double rx = X * s - Y * c;
        double ry = Y * s + X * c;
        return new Point2D(rx, ry);
    }

    public Point2D RotateDegrees(double angle)
    {
        return Rotate(angle * Math.PI / 180);
    }

    public double Distance(Point2D other)
    {
        double dx = other.X - X, dy = other.Y - Y;
        return Math.Sqrt(dx * dx + dy * dy);
    }
}

你可以充实其他行动。这些代码就足够了:

public class Player
{
    public Point2D location;
    // facing angle in degrees
    public double facing;

    public void TurnLeft(double degrees)
    {
        facing = (facing + 360 - degrees) % 360;
    }

    public void TurnRight(double degrees)
    {
        facing = (facing + degrees) % 360;
    }

    public void MoveForward(double distance)
    {
        // calculate movement vector
        Point2D move = new Point2D(0, distance).RotateDegrees(facing);

        // add to position
        this.location.AddEquals(move);
    }
}

命名在某些地方有点笨拙,但它应该给你一个想法。

答案 2 :(得分:0)

//Define what is forward when rotation is 0.
var forward = new Vector2(1f,0f);

//Create a rotation-matrix that can rotate the forward-vector
var rotater = Matrix.CreateRotationZ(MathHelper.ToRadians(getRotation()));

//rotate and normalize vector (normalizing makes the length = 1)
forward = Vector2.TransformNormal(forward, rotater);

//move in rotated forward direction times 4
Position += forward * 4f; 

但我建议使用每秒像素值并乘以deltatime:

Position += forward * pixelsPerSecond * (float)gameTime.ElapsedGameTime.TotalSeconds;
相关问题