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

问题描述

我正在查看"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;
}

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

标签: transformsfmltransformationtransformable

解决方案


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

sf::Transform 的文档

github源代码


推荐阅读