繁体   English   中英

我正在研究 SFML,我真的很想深入了解关于以下内容的转换:对象的位置、旋转、缩放和原点

[英]I'm working on SFML and I really want to know deeply about Transformation that is about: position, rotation, scale and origin, of an object

我正在查看"SFML/Graphics/Transform.h""SFML/Graphics/Transformable.h"标题,但我无法理解一件事。 我将课程最小化,以便你们可以轻松获得它:

// "SFML/Graphics/Transform.h"
class Transform
{
 public:
// Default constructor
Transform();

// Construct a transform from a 3X3 matrix
Transform(float a00, float a01, float a02,
          float a10, float a11, float a12,
          float a20, float a21, float a22);
};

// "SFML/Graphics/Transformable.h"
class Transformable
{
private:
Vector2f mPosition;                             // Position of the object in 2D world.
float mRotation;                                // Orientation of the object, in degress.
Vector2f mScale;                                // Scale of the object.
Vector2f mOrigin;                               // Origin of translation, rotation and scaling.of the object.
mutable Transform mTransform;                  // Combined transformation of the object.
mutable bool      mTransformNeedUpdate;        // Does the transform need to be recomputed?
mutable Transform mInverseTransform;           // Combined transformation of the object.
mutable bool      mInverseTransformNeedUpdate; // Does the transform need to be recomputed?

public:
// Functions to implement transformation
void setPostion(float x, float y);
void setPosition(const Vector2f& position);
void setRotation(float angle);
void setScale(float factorX, float factorY);
void setScale(const Vector2f& factors);
void setOrigin(float x, float y);
void setOrigin(const Vector2f& orgin);

const Vector2f& getPosition() const;
float getRotation() const;
const Vector2f& getScale() const;
const Vector2f& getOrigin() const;

void move(float offsetX, float offsetY);
void move(const Vector2f& offset);
void rotate(float angle);
void scale(float factorX, float factorY);
void scale(const Vector2f& factors);

// What I don't understand
const Transform& getTransform() const;
const Transform& getInverseTransform() const;
};

"SFML/Graphics/Transformable.cpp"中, "getTransform()"函数实现为以下代码:

const Transform& Transformable::getTransform() const
{
    // Recompute the combined transform if needed
    if (mTransformNeedUpdate)
    {
        float angle  = -mRotation * 3.141592654f / 180.f;
        float cosine = static_cast<float>(std::cos(angle));
        float sine   = static_cast<float>(std::sin(angle));
        float sxc    = mScale.x * cosine;
        float syc    = mScale.y * cosine;
        float sxs    = mScale.x * sine;
        float sys    = mScale.y * sine;
        float tx     = -mOrigin.x * sxc - mOrigin.y * sys + mPosition.x;
        float ty     =  mOrigin.x * sxs - mOrigin.y * syc + mPosition.y;

        mTransform = Transform( sxc, sys, tx,
                                -sxs, syc, ty,
                                 0.f, 0.f, 1.f);
        mTransformNeedUpdate = false;
    }

    return mTransform;
}

我的问题是它们如何实现这些值: sxcsycsxssystxty ,以及为什么它们按该顺序将它们放入 3X3 矩阵中。 十分感谢!!!

也许,这两个链接可以帮助你:

sf::Transform 的文档

github源代码

您似乎在问计算机图形中通常如何处理几何变换。

无论是 2d 还是 3d(或者我猜是n d),您都可以通过矩阵乘法来实现缩放和旋转。 也就是说,你有一个对象——一堆点——然后你通过将每个点的坐标——也就是向量——乘以一个变换矩阵来变换它。 这些矩阵具有特定的值。

缩放

要从原点按系数k缩放,矩阵将如下所示:

k 0 
0 k 

回转

围绕原点旋转角度a

cos(a) -sin(a)
sin(a) cos(a)

然后可以将这些 2x2 矩阵乘以 2d 向量。 结果将是一个带有变换坐标的二维向量。

您还可以组合这两个变换,即同时进行旋转和缩放,首先将两个变换相乘,然后使用生成的 2x2 矩阵与向量进行乘积。

翻译

问题是您不能在该系统中将翻译作为产品进行。 你可以通过向量加法来做到这一点。 但是为了均匀有效地进行所有变换,使用了称为齐次坐标的东西。 在该系统中,2d 变换需要点作为 3d 向量和 3x3 矩阵。 3d 变换需要 4d 向量和 4x4 矩阵。

您在 SFML 的源代码中看到的矩阵是 sf::Transform 对象中包含的缩放、旋转和平移信息在齐次坐标中的组合(或组合)。 这些值以正确的方式排列在矩阵中,只需将矩阵的乘积应用于向量即可进行转换。 sf::Vector2 实际上包含 3 个组件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM