简体   繁体   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

I am looking at the "SFML/Graphics/Transform.h" and "SFML/Graphics/Transformable.h" headers and I can't understand one thing.我正在查看"SFML/Graphics/Transform.h""SFML/Graphics/Transformable.h"标题,但我无法理解一件事。 I minimize the classes so you guys can get it easily:我将课程最小化,以便你们可以轻松获得它:

// "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;
};

In the "SFML/Graphics/Transformable.cpp" the "getTransform()" function is implemented as the following code:"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;
}

My question is how they implement these values: sxc , syc , sxs , sys , tx , ty like that and why they put them into the 3X3 matrix by that order.我的问题是它们如何实现这些值: sxcsycsxssystxty ,以及为什么它们按该顺序将它们放入 3X3 矩阵中。 Thank you a lot!!!十分感谢!!!

Maybe, this two links can help you:也许,这两个链接可以帮助你:

The doc of sf::Transform sf::Transform 的文档

The github source code github源代码

It seems you're asking how geometric transformations are generally handled in computer graphics.您似乎在问计算机图形中通常如何处理几何变换。

Be it in 2d or 3d (or n d I guess), you can implement scaling and rotation by way of matrix multiplication.无论是 2d 还是 3d(或者我猜是n d),您都可以通过矩阵乘法来实现缩放和旋转。 That is, you have an object -a bunch of points- and you transform it by multiplying the coordinates of each point -aka vectors- by a transform matrix.也就是说,你有一个对象——一堆点——然后你通过将每个点的坐标——也就是向量——乘以一个变换矩阵来变换它。 Those matrices have particular values.这些矩阵具有特定的值。

Scaling缩放

To scale from the origin by a factor k , the matrix will be like this:要从原点按系数k缩放,矩阵将如下所示:

k 0 
0 k 

Rotation回转

Rotation of angle a around the origin:围绕原点旋转角度a

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

These 2x2 matrices can then be multiplied by a 2d vector.然后可以将这些 2x2 矩阵乘以 2d 向量。 The result will be a 2d vector with the transformed coordinates.结果将是一个带有变换坐标的二维向量。

You can also combine these two transforms, ie do rotation and scaling all at once, by first multiplying the two transforms together, and use the resulting 2x2 matrix to do the product with a vector.您还可以组合这两个变换,即同时进行旋转和缩放,首先将两个变换相乘,然后使用生成的 2x2 矩阵与向量进行乘积。

Translation翻译

The problem is you cannot do translation as a product in this system.问题是您不能在该系统中将翻译作为产品进行。 You could do it by way of vector addition.你可以通过向量加法来做到这一点。 But to do all transforms uniformly and efficiently, something called homogeneous coordinates are used.但是为了均匀有效地进行所有变换,使用了称为齐次坐标的东西。 In that system, 2d transformations need points as 3d vectors and 3x3 matrices.在该系统中,2d 变换需要点作为 3d 向量和 3x3 矩阵。 3d transforms need 4d vectors and 4x4 matrices. 3d 变换需要 4d 向量和 4x4 矩阵。

The matrix you see in SFML's source is the combination (or composition) of the scale, rotation and translation information contained in the sf::Transform object, in homogeneous coordinates.您在 SFML 的源代码中看到的矩阵是 sf::Transform 对象中包含的缩放、旋转和平移信息在齐次坐标中的组合(或组合)。 The values are arranged in the matrix in the right way to do the transformations just by applying the product of the matrix to a vector.这些值以正确的方式排列在矩阵中,只需将矩阵的乘积应用于向量即可进行转换。 sf::Vector2 actually contains 3 components. sf::Vector2 实际上包含 3 个组件。

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

相关问题 子对象变换位置,旋转,缩放比例是否偏离原点? - Child object transform position,rotation,scale incorrect away from origin? 使用局部比例和旋转比例缩放对象 - Scaling an object with a local scale and a rotation CSS图像旋转变换 - CSS Image Rotation Transformation Python-Pygame-旋转不是很流畅吗? [我知道为什么,只是想不出解决办法] - Python - Pygame - Rotation not very smooth? [I know why, just can't think of solution] 计算横向拉伸对象的比例和位置 - Calculating scale and position for stretching an object sideways 当父/主体进行比例转换时,获取元素上的触摸位置 - Get touch position on element when parent/body has scale transformation 转换 - 当我将 html 内容转换为 pdf 时,旋转和缩放不起作用 - transform - rotate and scale is not working when i convert html content to pdf 如何根据来自不同脚本的变量缩放对象? - How do i scale an object based on a variable from a different script? 这是一个关于 nifi jolt 转换的问题,用于将基于键属性的 json 数组分组到新数组中 - This a question about nifi jolt transformation for grouping json arrays based on a key attribute into new array CSS Animation\\Transformation 中的旋转方向 - Direction of rotation in CSS Animation\Transformation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM