對於簡單的2D遊戲,我正在嘗試使用矩陣圍繞Z軸旋轉精靈。我顯然做錯了,因爲當我試圖旋轉我的精靈時,它看起來像圍繞着屏幕原點(底部,左邊)旋轉而不是精靈原點。我很困惑,因爲我的四連桿已經在原點了,所以我不認爲我需要翻譯 - >旋轉並翻譯回來。這裏有一個代碼段和一個小視頻或erroneous transformation矩陣轉換問題 - 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我轉換所有的四邊形,然後使用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);
它不是「scewing」,它只是圍繞X軸繞着錯誤的軸旋轉精靈。 如果你想有人幫助你,那麼你需要提供更多信息你的API(Direct3D/OpenGL),你的矩陣庫(如果它不是你自己的),構造旋轉矩陣的例程代碼,例程代碼它可以轉換四元組,渲染四元組的代碼,也可以轉換兩個矩陣相乘的代碼。 – SigTerm 2010-07-10 05:33:31
我不認爲它圍繞X軸旋轉。圍繞x和y軸旋轉的行爲與預期相同(精靈像旋轉的硬幣一樣左右旋轉)。如果你喜歡,我可以發佈視頻演示。 – 2010-07-10 06:04:24