如何旋转,缩放,并在C#中一次性全部转换矩阵? [英] How to rotate, scale, and translate a matrix all at once in C#?

查看:849
本文介绍了如何旋转,缩放,并在C#中一次性全部转换矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是什么,应该是一个简单的矩阵的问题,但我的矩阵的理解是比较有限的。这里的情景:我有1px的精灵1px的,我想扩大一些量x和y(每边不同量),然后我想旋转一些角度精灵,然后我希望能够精确定位整个事情(从左上角或中心,没什么区别对我来说)。

Okay, this is something that should be a simple matrix question, but my understanding of matrices is somewhat limited. Here's the scenario: I have a 1px by 1px sprite that I want to scale by some amount x and y (different amounts on each side), and then I want to rotate that sprite by some angle, and then I want to be able to precisely position the whole thing (from the top left or the center, makes no difference to me).

到目前为止,我的代码是隐约接近,但它往往是关闭通过这取决于我传递的角度一些随机量

So far my code is vaguely close, but it tends to be off by some random amount depending on the angle I pass in.

我会认为这将做到这一点:

I would think that this would do it:

        Point center = new Point( 50, 50 );
        float width = 60;
        float height = 100;
        float angle = 0.5;
        Vector3 axis = new Vector3( center.X, center.Y, 0 );
        axis.Normalize();
        Matrix m = Matrix.Scaling( width, height, 0 ) *
            Matrix.RotationAxis( axis, angle ) *
            Matrix.Translation( center.X, center.Y, 0 );



但它往往收缩的旋转行的方式按比例缩小,尽管我认为这是它定位。排序的权

But it tends to shrink the scale of the rotated line way down, even though I think it's positioning it sort of right.

我也试过这样的:

  Matrix m = Matrix.Transformation2D( new Vector2( center.X, center.Y ), 0f,
      new Vector2( width, height ), new Vector2( center.X, center.Y ),
      angle, Vector2.Zero );



行是完全正确的,具有确切合适的大小和形状,但我不能放置它正确的。如果我使用平移向量在通话结束以上,或者如果我设置使用Sprite.Draw的位置,无论是作品的权利。

The line looks exactly right, with the exact right size and shape, but I can't position it correctly at all. If I use the translation vector at the end of the call above, or if I set a position using Sprite.Draw, neither works right.

这是所有SlimDX。我在做什么错了?

This is all in SlimDX. What am I doing wrong?

推荐答案

我刚刚通过学习矩阵变换,但使用XNA的痛苦了。

I just went through the pain of learning matrix transformation, but using XNA.

我发现这些文章 了解发生了什么非常帮助。该方法调用是在XNA非常相似,这一切背后的理论应适用于你,甚至与SlimDX。

I found these articles to be very helpful in understanding what happens. The method calls are very similar in XNA and the theory behind it all should apply to you, even with SlimDX.

从你的代码一眼,我想你应该翻译在一开始,到原点,然后再在结束翻译,到最终位置,虽然我还是一个新手的一点点在这一点。

From glancing at your code, I think you should be translating at the start, to the origin, and then translating again at the end, to the final position, though I'm still a little bit of a newbie at this as well.

我会做它的顺序是:


  • 翻译成原产地

  • 缩放

  • 旋转

  • 转换到需要的位置

  • Translate to origin
  • Scale
  • Rotate
  • Translate to desired location

原因用于转换到原点首先是旋转从源的基础。因此旋转一些有关某一点时,放置在原点该点旋转前

The reason for translating to the origin first is that rotations are based from the origin. Therefore to rotate something about a certain point, place that point on the origin before rotating.

这篇关于如何旋转,缩放,并在C#中一次性全部转换矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆