[英]Child object transform position,rotation,scale incorrect away from origin?
[英]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;
}
我的問題是它們如何實現這些值: sxc
、 syc
、 sxs
、 sys
、 tx
、 ty
,以及為什么它們按該順序將它們放入 3X3 矩陣中。 十分感謝!!!
您似乎在問計算機圖形中通常如何處理幾何變換。
無論是 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.