如何在特定点进行平移、旋转和缩放?

How to translate, rotate and scale at a particular point?

我想平移、旋转和缩放所有这些从指定点(原点)开始的变换。我现在用的就是这个(试了很多组合还是想不通!看来又回到学习线性代数的状态了)

我正在使用 GLM。

看看我正在使用的代码:-

GLfloat rx = sprite->origin.x / width;
GLfloat ry = sprite->origin.y / height;

translate(model, vec3( -sprite->origin.x, -sprite->origin.y, 0.0f ));
translate(model, vec3( rx * scale[0], ry * scale[1], 0.0f ));
rotate(model, radians(rotation), vec3( 0.0f, 0.0f, 1.0f ));
translate(model, vec3( -(rx * scale[0]), -(ry * scale[1]), 0.0f ));
scale(model, vec3( width * scale[0], height * scale[1], 1.0f ));
translate(model, vec3( position[0], position[1], 0.0f ));

Origin是物体的起点,平移到中心旋转再平移回来。

缩放它并将对象平移到指定位置。

我的意思是,这段代码有什么问题?

我找到了答案,

按以下顺序进行:-

  1. 将对象翻译到位置,例如vec3(position, 0.0f).

  2. 然后旋转对象

  3. 将点平移回原点,例如,vec3(-originx * scale, -originy * scale, 0.0f).

  4. 最后,缩放对象


解释:

将物体在二维坐标中的位置作为0, 0(作为上面第一个点的例子中提到的变量position ).

可以看到物体会平移(移动)到该位置。

Note: Don't forget to multiply it with your scale or it can yield wrong results if your scale is more than 1.

现在是旋转对象的时候了。

缩放是从左上角开始的,所以你不能从中间开始缩放。

接下来,只需缩放对象即可。

就这些了。