MonoGame/XNA旋转图像

Tec*_*rat 2 c# xna sprite image-rotation monogame

这段代码是为了绘制塔.广场位置是广场的左上角.TILE_SIZE只是方形的尺寸.

SpriteBatch.Draw(TowerImage, new Rectangle(square.X * TILE_SIZE, square.Y * TILE_SIZE, TILE_SIZE, TILE_SIZE), null, Color.White, myTower.Rotation, 
    new Vector2(TILE_SIZE - 35, TILE_SIZE - 35), SpriteEffects.None, (float)0.0);
Run Code Online (Sandbox Code Playgroud)

这段代码是我确定旋转的方式

public void FaceTarget(Vector2 center, Vector2 enemyCenter)
        {
            Vector2 direction = center - enemyCenter;
            direction.Normalize();

            this.Rotation = (float)Math.Atan2(-direction.X, direction.Y);
        }
Run Code Online (Sandbox Code Playgroud)

我这样做基于:

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Rotation.php

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Direction_to_Angle.php

旋转真的很奇怪,这是它看起来如何正常:

http://puu.sh/bkd0E.png

但是当它旋转时它会像这样:

http://puu.sh/bkd6K.png

最后,当它往下看时,它完全离开路径,它不是由它的中心旋转,但整个图像正在移动它为什么这样做?

http://i.stack.imgur.com/4PuiZ.jpg

只有第一张图像实际上是正确位置的塔

看起来它是左上角的旋转,我不知道为什么.有人可以帮忙吗?

Nah*_*nni 5

显然,你的精灵正在考虑作为旋转原点的一个Vector2.Zero(或Vector2(0,0))点.这意味着Texture2D文件的左上角.

我看到你在Draw方法中设置原点TILE_SIZE - 35让我想知道,是一块70像素W/H的方块吗?

如果用替换减法会发生什么new Vector2(TowerImage.Width / 2, TowerImage.Height / 2)

我将在这个网站上给你一个例子,它可以很容易地解释如何在鼠标位置之后旋转图像:

更新方法:

MouseState mouse = Mouse.GetState();
mousePosition = new Vector2(mouse.X, mouse.Y);

Vector2 direction = mousePosition - position;
direction.Normalize();

rotation = (float)Math.Atan2(
              (double)direction.Y, 
              (double)direction.X);
Run Code Online (Sandbox Code Playgroud)

绘制方法:

spriteBatch.Begin();

spriteBatch.Draw(
     rocket,
     position,
     null,
     Color.White,
     rotation,
     new Vector2(
         rocket.Width / 2, 
         rocket.Height / 2),
     1.0f,
     SpriteEffects.None,
     1.0f);

spriteBatch.End();
Run Code Online (Sandbox Code Playgroud)

检查在发布的代码中,旋转角度的计算方法与您的略有不同,但重要的代码是计算方法中的原点Draw.