围绕枢轴旋转坐标? (俄罗斯方块) [英] Rotating cordinates around pivot? (tetris)

查看:152
本文介绍了围绕枢轴旋转坐标? (俄罗斯方块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计自己的俄罗斯方块克隆,但在形状旋转方面遇到了一些小问题.我有一个二维数组,代表一个10 x 20的游戏网格和各个形状对象,这些对象在初始化时会包含形状从网格开始下降的位置的坐标.因此,例如,当用户将形状向下移动时,每个坐标的y值都会减小,并且此更改会反映在网格上.

I'm attempting to design my very own tetris clone but have run into a little problem with shape rotations. I have a 2 dimensional array representing a 10 x 20 game grid and individual shape objects which when initialised contain coordinates of where on the grid the shape will start falling from. So for example, when the user moves the shape down each coordinate's y value gets decremented and this change is reflected on the grid.

我似乎无法弄清楚的是使用此实现处理形状旋转的有效方法.有什么方法可以使用围绕指定枢轴的这些坐标矩阵?

What I can't seem to figure out is an efficient way to handle shape rotations using this implementation. Is there any way to maybe use a matrix these coordinates around a designated pivot?

任何建议将不胜感激

谢谢.

推荐答案

如果经典的旋转矩阵有效,则取决于 SRS 为例.

If classic rotation matrices work, will depend on the rotation system you want to use. I will use SRS as an example.

围绕原点逆时针旋转的旋转矩阵为:

The rotation matrix for counter-clockwise rotation around the origin is:

[0 -1]
[1  0]

现在,假设您有一个坐标[[0,1),(1,1),(2,1),(3,1)]的列表,它们表示I块在其初始位置:

Now, suppose you have a list of coordinates [(0, 1), (1, 1), (2, 1), (3, 1)] representing the I-block in its initial position:

 0123
0....
1####
2....
3....

请注意,我不使用笛卡尔坐标系,而是通常的屏幕坐标,从左上角开始.要正确旋转块,首先必须考虑y轴的翻转.旋转矩阵将变为:

Note that I don't use a cartesian coordinate system, but the usual screen coordinates, starting in the top left. To rotate the block properly, you first have to account for the flip of the y-axis. The rotation matrix then becomes:

[ 0 1]  ->  x_new = y_old
[-1 0]  ->  y_new = -x_old

接下来,要绕枢轴点旋转,在旋转之前,必须先移动坐标,以使枢轴点成为原点(在下面称为sb),然后在旋转后将它们移回(在下面称为sa ):

Next, to rotate around a pivot-point, before rotating, you have to shift the coordinates so that the pivot-point becomes the origin (called sb below) and shift them back after rotating (called sa below):

x_new = sa_x + (y_old - sb_x)
y_new = sa_y - (x_old - sb_y)

通常您会拥有sb = sa,但是对于tetris块,枢轴点有时位于两个单元之间的网格上(对于I和O块),有时在一个单元的中心(对于所有其他块)

Normally you would have sb = sa, but for tetris blocks the pivot-point is sometimes on the grid between two cells (for I- and O-blocks) and sometimes at the center of a cell (for all other blocks).

事实证明

sa_x = 0
sb_x = 0
sa_y = 1
sb_y = me - 2

其中me是要旋转的块的最大范围(即2、3或4),适用于所有块.综上所述,您将得到:

where me is the maximum extent (i.e. 2, 3, or 4) of the block to rotate, works for all blocks. So to sum up, you get:

x_new = y_old
y_new = 1 - (x_old - (me - 2))

顺时针旋转类似,但是如果您为块方向缓存所有坐标,则只需一个方向.

Clockwise rotation is similar, but if you cache the coordinates for all for block orientations you will only need one direction.

对于其他旋转系统,shift变量的其他值可能会起作用,但是您可能必须再次移动块,具体取决于块的当前方向(比较 DTET旋转了解我的意思.

For other rotation systems other values of the shift variables might work, but you might have to shift the piece again, depending on the current orientation of the block (compare SRS rotation to DTET rotation of the I-block, to see what I mean).

这篇关于围绕枢轴旋转坐标? (俄罗斯方块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆