1

私が作成している単純な 2D ゲームでは、マトリックスを使用して z 軸を中心にスプライトを回転させようとしています。スプライトを回転させようとすると、スプライトの原点ではなく、画面の原点 (下、左) を中心に回転しているように見えるため、明らかに何か間違っています。私のクワッドはすでに原点にあるので混乱しているので、変換する必要はないと思いました->回転して元に戻します。コード スニペットと小さなビデオまたは誤った変換を次に示します。

void MatrixMultiply(
MATRIX      &mOut,
const MATRIX    &mA,
const MATRIX    &mB);
/*!***************************************************************************
@Function           TransTransformArray
@Output         pTransformedVertex  Destination for transformed vectors
@Input              pV                  Input vector    array
@Input              nNumberOfVertices   Number of vectors to transform
@Input              pMatrix             Matrix to transform the vectors of input vector (e.g. use 1 for position, 0 for normal)
@Description        Transform all vertices in pVertex by pMatrix and store them in
                pTransformedVertex
                - pTransformedVertex is the pointer that will receive transformed vertices.
                - pVertex is the pointer to untransformed object vertices.
                - nNumberOfVertices is the number of vertices of the object.
                - pMatrix is the matrix used to transform the object.
*****************************************************************************/
void TransTransformArray(
VECTOR3     * const pTransformedVertex,
const VECTOR3   * const pV,
const int     nNumberOfVertices,
const MATRIX    * const pMatrix);


RenderQuad CreateRenderQuad(
    const Texture2D & texture,
    float x,
    float y,
    float scaleX, 
    float scaleY,
    float rotateRadians,
    int   zIndex,
    const Color & color,
    const Quad2 & textureCoord,
    const char * name
) {
    MATRIX mT;
    MATRIX mS;
    MATRIX concat;  
    MATRIX mR;

    MatrixTranslation(mT, x, y, 0.0f);
    MatrixRotationZ(mR, rotateRadians);
    MatrixScaling(mS, scaleX, scaleY, 1.0f);

    VECTOR3 quad[] = {
        {-0.5f, 0.5f, 0.f}, //tl
        {0.5f, 0.5f, 0.f}, //tr
        {-0.5, -0.5f, 0.0f}, //bl
        {0.5f, -0.5f, 0.0f}, //br
    };

    MatrixMultiply(concat, mR, mT);
    MatrixMultiply(concat, concat, mS);
    // apply to all the points in the quad
    TransTransformArray(quad, quad, 4, &concat);

== 更新:

構造体とレンダリング コードは次のとおりです。

oolongengine code.google.com/p/oolongengine/source/browse/trunk/Oolong%20Engine2/Math/Matrix.cpp のマトリックス クラスを使用しています。

すべてのクワッドを変換し、後で OpenGL を使用してレンダリングします。これが私のデータ構造体とレンダリングコードです:

typedef struct _RenderData {
    VECTOR3        vertex;
    RenderColor3D      color;
    RenderTextureCoord textureCoord;
    float              zIndex;
    GLuint             textureId;
} RenderData;

typedef struct _RenderQuad {
    //! top left
    RenderData  tl;
    //! top right
    RenderData  tr;
    //! bottom left
    RenderData  bl;        
    //! bottom right
    RenderData  br;

    float zIndex;

    Texture2D * texture; // render quad draws a source rect from here

    ESpriteBlendMode blendMode;

} RenderQuad ;

/// Draw
class QuadBatch {
   GLushort *              m_indices;
   const Texture2D *       m_texture;
   GLuint                   m_vbos[2];
   RenderData *            m_vertices; 
};

QuadBatch::Draw () {
    int offset = (int)&m_vertices[startIndex];

            // vertex
            int diff = offsetof( RenderData, vertex);
            glVertexPointer(3, GL_FLOAT, kRenderDataSize, (void*) (offset + diff) );

            // color
            diff = offsetof( RenderData, color);
            glColorPointer(4, GL_FLOAT, kRenderDataSize, (void*)(offset + diff));

            // tex coords
            diff = offsetof( RenderData, textureCoord);
            glTexCoordPointer(2, GL_FLOAT, kRenderDataSize, (void*)(offset + diff));

            // each quad has 6 indices

            glDrawElements(GL_TRIANGLES, vertexCount * elementMultiplier, GL_UNSIGNED_SHORT, m_indices);
4

1 に答える 1

2

「回転」は、定義上、原点(0,0,0)の周りにあります。別の回転軸が必要な場合は、移動コンポーネントを適用する必要があります。軸aの周りに回転Rを適用するとします。任意のベクトルxに適用する変換は次のとおりです。

x-> a + R(x-a)= Rx +(a-Ra)

(これは消化するのにいくらか凝視する必要があるかもしれません)。したがって、回転を適用した後(観察したように、原点を中心に回転します)、定数ベクトル(a --Ra)を追加する必要があります。

[編集:]この答えは言語とプラットフォームにとらわれません-数学はどこを見ても同じです。特定のライブラリには、変換を適用するためのさまざまな構造とAPIが含まれています。たとえば、DirectXとOpenGLはどちらも、4x4の行列変換を維持して、回転と変換を単一の行列乗算に統合します(同次座標と呼ばれる装置を介して)。

于 2010-07-10T09:28:42.377 に答える