gpt4 book ai didi

c++ - 矩阵变换问题——Z轴旋转是倾斜的

转载 作者:行者123 更新时间:2023-11-30 04:38:19 25 4
gpt4 key购买 nike

对于我正在制作的简单 2d 游戏,我尝试使用矩阵围绕 z 轴旋转 Sprite 。我显然做错了什么,因为当我尝试旋转我的 Sprite 时,它看起来像是在围绕屏幕原点(底部,左侧)而不是 Sprite 原点旋转。我很困惑,因为我的四边形已经在原点了,所以我认为我不需要平移 -> 旋转并向后平移。这是一个代码片段和一个小视频或 erroneous transformation

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);

最佳答案

'旋转',顾名思义,就是围绕原点 (0,0,0) 旋转。如果你想要一个不同的旋转轴,你必须应用一个翻译组件。假设您想围绕轴 a 应用旋转 R。应用于任意 vector x 的变换是:

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

(这可能需要盯着看才能消化)。因此, 应用旋转后 - 正如您观察到的那样,旋转围绕原点旋转 - 您必须添加常量 vector (a - Ra)。

[编辑:] 这个答案与语言和平台无关——不管你看哪里,数学都是一样的。特定的库包含不同的结构和 API 来应用转换。例如,DirectX 和 OpenGL 都维护 4x4 矩阵变换,以将旋转和平移统一为单个矩阵乘法(通过称为齐次坐标的装置)。

关于c++ - 矩阵变换问题——Z轴旋转是倾斜的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3217951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com