2011-06-07 88 views
1

我碰到一些问题。我想ge geords可以旋转缩放的任何东西。它类似于Riemers指南,但他正在碰撞两个精灵,而我只需要那些alpha为零的点。C#XNA 4.0在窗口中缩放旋转的精灵位置

更好地看清来源:

public Color[,] TextureTo2DArray(Texture2D texture) // to get color array 
    { 
     Color[] colors1D = new Color[texture.Width * texture.Height]; 
     texture.GetData(colors1D); 

     Color[,] colors2D = new Color[texture.Width, texture.Height]; 
     for (int x = 0; x < texture.Width; x++) 
      for (int y = 0; y < texture.Height; y++) 
       colors2D[x, y] = colors1D[x + y * texture.Width]; 

     return colors2D; 
    } 

有了颜色是很容易的,但这里是一部分,我拿点:

public Vector2 TexturePos(Color[,] Color, Matrix matrix) 
     { 

      int width1 = Color.GetLength(0); 
      int height1 = Color.GetLength(1); 


      for (int x = 0; x < width1; x++) 
      { 
       for (int y = 0; y < height1; y++) 
       { 
        Vector2 pos1 = new Vector2(x, y); 

        if (Color[x, y].A > 0) 
        { 
         Vector2 screenPos = Vector2.Transform(pos1, matrix); 
         return screenPos; 
        } 
       } 
      } 

      return new Vector2(-1, -1); 
     } 

而对于矩阵我使用这个:

 Matrix matrix = 
      Matrix.CreateTranslation(new Vector3(origin, 0)) * 
      Matrix.CreateRotationZ(MathHelper.ToRadians(rotation))* 
      Matrix.CreateScale(scale) * 
      Matrix.CreateTranslation(new Vector3(pos, 0)); 

雪碧是矩形的,但我得到了圆形运动:我旋转它(旋转+ = 0.5),增加引力并使它碰撞一些y值:

Pos.Y += 5; 
if (Position.Y >= 200) 
BoxPos.Y -= 5; 

而我得到它旋转为一个圆形碰撞一条线,而不是一个矩形。

这是正常的吗?也许我需要一些修正来源?

+0

你在做什么TexturePos方法?这个方法究竟在做什么?此外,执行碰撞的实际代码缺失... – 2011-06-08 20:35:02

+0

该方法应该获取像素(在sprite中)的位置,该像素不是transperent,而是旋转,缩放(取决于精灵)。我只想一次获得一个精灵的界限。 – giger 2011-06-08 22:18:50

+0

关于矩阵乘法 - 你使用2个不同的值在旋转之前/之后进行翻译,怎么回事? – 2011-06-10 05:32:31

回答