AppleSeeker's Tech Blog
Welcome to AppleSeeker's space
posts - 84,  comments - 767,  trackbacks - 6

为了使静止的正方形更加有趣,我们来快速浏览一下让其绕着屏幕旋转的方法。

要实现这一点,首先需要跟踪旋转的角度。添加一个类级别的float变量,将其命名为_angle,并在每次更新时增加5度

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    this.Exit();
    _angle += MathHelper.ToRadians(5);
    base.Update(gameTime);
}

我们需要更新世界矩阵(有关世界矩阵的全部细节会在“理解矩阵转换”一节中探讨)才能将该角度应用到正方形中。因为需要旋转该正方形,所以要为其提供一个旋转矩阵。XNA的Matrix类提供了用于创建这种矩阵的多种方法,在本例中我们选择的是CreateRotationZfunction函数。此函数接受单个参数(旋转角度),并返回一个可供使用的矩阵。

更新后的绘制旋转正方形的代码

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    // Set the world matrix so that the square rotates
    _effect.World = Matrix.CreateRotationZ(_angle);
    foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
    {
        // Apply the pass
        pass.Apply();
        // Draw the square
        GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _vertices, 0, 2);
    }
    base.Draw(gameTime);
}

注意调用DrawUserPrimitives函数实际上完全不会使正方形发生改变;真正使对象旋转的是效果的状态而非绘制指令。这与基于精灵的渲染方法截然不同。

posted on 2011-06-17 21:09 AppleSeeker(冯峰) 阅读(192) 评论(0) 编辑 收藏
Free Hit Counter

MSN:appleseeker@hotmail.com
Mail:appleseeker@gmail.com MVP


【新品特价】SPORTICA 防水商务10春新款内里绒夹克外套03409081
[YT001]深层去斑美白去斑套餐 去黑淡痘印 最好最热产品日销千套
昵称:AppleSeeker(冯峰)
园龄:6年6个月
粉丝:33
关注:3

<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

搜索

 

常用链接

最新随笔

我的标签

随笔分类(124)

随笔档案(84)

Mobile Blog

Recommend Blog

  • 李占卫

Recommend Site

  • Green IT

积分与排名

  • 积分 - 230256
  • 排名 - 352

最新评论

阅读排行榜

评论排行榜

推荐排行榜