【问题标题】:change default origin from top-left to center in XNA在 XNA 中将默认原点从左上角更改为中心
【发布时间】:2013-05-07 15:24:11
【问题描述】:

我想知道是否可以将原点设置为屏幕中心。

例如我通常这样做:

`origin = new vector2D(image.height / 2, image.width /2);

spritebatch.Draw(image, vector.zero,null, Color, 0, origin, none, 0); `

现在图像的原点在中间。我希望能够做一个 vector.zero 并将图像放在屏幕中间而不做我通常做的事情。这可能吗?

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    除非您在谈论屏幕空间,否则坐标系实际上并不是这样工作的。在这种情况下,整个屏幕映射到 [-1,-1](左上角)- [1,1](右下角)。这没什么用,所以不要这样做。

    你需要做的是让自己成为一个精灵类,在其中你可以找到你要绘制的精灵(纹理)的来源:

    public class Sprite
    {
        static Vector2 WorldOrigo = new Vector2(400, 240); //center of a 800x480 screen
    
        Texture2D Texture { get; set; }
        Vector2 Origin { get; set; }
    
        public Sprite(Texture2D texture)
        {
            Texture = texture;
            Origin = new Vector2(texture.Width / 2, texture.Height / 2);
        }
    
        public void Draw(Vector2 position, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, WorldOrigo + position - Origin, Color.White);
        }
    }
    

    请注意,这只是一个示例。你的精灵可能会有动画等代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多