Rotating cordinates around pivot? (tetris)

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?

Any suggestions will be greatly appreciated,

Thank you.

-------------Problems Reply------------

Sure, look up "affine transform". But in your case what you've got is exactly four possible rotations of an object in discrete angles -- there's no 70.3° rotation, it's just 0, 90°, 180°, 270°. So why not precompute?

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]

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


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

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)

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

It turns out that

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

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.

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

This is classic linear algebra. You're looking for a rotation matrix, except all your angles are right angles so you can precalculate the sines and cosines.

Wikipedia: Rotation matrix

To do it around a point, you have to subtract the center value first (making that reference point the center origin) then apply the matrix, and add the original center position back.

I assume you have finished this by now. I’m not a programmer but I do remember doing this a Uni. We just had 4 different objects (with different rotations) for each piece. Eg the “L” shape has piece 1,2,3,4. If your active piece in 3 and you rotate clockwise then you load piece 4, rotate clockwise again and load piece 1.

Category:matrix Views:2 Time:2009-09-22

Related post

  • Rotate an Image pivot in the Center - Android 2011-06-14

    I want to rotate an Image pivoting in the center. I want to use this when i rotate the image I need to load dynamic Text as menu. Like a Jog. Any one pls help me for rotating an image --------------Solutions------------- To rotate an image you can do

  • Quaternion rotation around a pivot point in XNA 2012-03-15

    Situation: I have a 3d model which I am attempting to rotate and orbit around a pivot point. Let's for now say the model is a pencil, and it's pivot point is a Vector3(0, 0, 0). The pencil nib should always be touching the pivot point, and it should

  • Raphael SVG VML Implement Multi Pivot Points for Rotation 2011-01-13

    Over the last two days I've effectively figured out how NOT to rotate Raphael Elements. Basically I am trying to implement a multiple pivot points on element to rotate it by mouse. When a user enters rotation mode 5 pivots are created. One for each c

  • How to Rotate a 2D Array of Integers 2009-03-14

    I am programming a Tetris clone and in my game I store my tetromino blocks as 4x4 arrays of blocks. I now need to be able to rotate the integer positions in the arrays so that I get a rotated tetris block. I cannot simply rotate the texture because a

  • Rotate a 2d matrix to the right 2010-04-25

    I want a 2d matrix to rotate to the right, it compiles fine but when I try to the run it says that the array index is out of bounds exception. For example I want {{10,20,30},{40,50,60}} to rotate into {{40,10},{50,20},{60,30}} import java.util.*; pub

  • opengl rotations for a human 2011-03-31

    I currently can rotate around a pivot point by first translating to the pivot point then performing the rotation and finally translating back to the origin. I do that easily enough for the shoulder in my example. However I cannot figure out how to al

  • wpf manipulation rotation 2011-04-19

    I found this nice example, and it works perfectly. I am really bad with maths, so rotating and adding inertia is really easy this way. My problem is that I need to normalize rotation angle to 0 to 360. So after complete one spin in one direction to o

  • XNA Rotate Camera Around It's CreateLookAt "Target" 2011-05-06

    How to rotate camera around it's CreateLookAt() "Target" Vector3 position as pivot point, so the camera will "rotate around" that pivot point and always "facing" to that pivot point. Please kindly answer, thank you --------------Solutions------------

  • QGraphicsItem's - selection & rotation 2011-07-04

    I'd like to implement application which allows user to select few QGraphicsItems and then rotate them as a group. I know that I could add all items into one QGraphicsItemGroup but I need to keep Z-value of each item. Is it possible? I also have secon

  • Should I translate first or rotate first? 2012-04-03

    I'm trying to create a simple scene where I can walk around, with the criteria of being able to pan around and walk around with the keys. However, in my draw scene function, when I translate my scene than rotate, the panning around doesn't work prope

  • Clockwise rotating line 2012-04-28

    I am given a pivot point (by the integer coordinates) and a line that goes through that point and makes an x degree with the horizontal. The line makes a 360 degrees clockwise rotation around the pivot point with speed s= 0.001 degree per second. I a

  • perspective in Flash 2009-12-21

    I have an 2D image that I want to draw in true 3D, and spin around its centre. I'm using Actionscript 3 code (actually Haxe, no IDE), and I'm struggling to discover the values by experimentation. I have a DisplayObject. If I use rotateZ = 45, the obj

  • Writing functional specifications for games 2010-02-02

    I've been reading Joel on Software recently and so I've become pretty sold on the idea of writing functional specifications before getting too involved in development. I'm about to have a go at creating a simple 2D game (not too simple mind), and I c

  • How to get main page from any part of Windows Phone 7 application? 2010-10-16

    Is there any way of getting main page object from any of its child controls? As a possible solution I see here bubbling through parents and stopping as soon as a parent is of PhoneApplicationPage type. It is fine for me, but what if I need to do that

  • How do I use the Android compass orientation to aim an opengl camera? 2011-03-11

    I'm using libgdx to develop a basic 3d game for android and I'm having difficulty properly orienting the camera given three rotation angles provided from the compass (azimuth - rotation about Z, roll - rotation about Y, pitch - rotation about X). I'v

  • How to find the angle between 2 pairs of points? 2012-03-26

    I am doing some multi-touch support that allow 2 fingers to rotate a photos. There are four points: 2 for previous fingers and 2 for current finger positions. I calculated a new point by subtract the 2 previous fingers, and the second new point was c

  • Shape Animation issue 2012-06-04

    I am using PPT2010 and I am trying to get a shape(in this case an arrow) to rotate on a pivot using animation.So for example my background is a blank compass and I want the arrow within the compass to rotate on the pivot of the middle of the compass.

  • Rotate tetris blocks at runtime 2011-10-28

    I have a class tetronimo (a tetris block) that has four QRect types (named first, second, third, fourth respectively). I draw each tetronimo using a build_tetronimo_L type functions. These build the tetronimo in a certain direction, but as in tetris

  • Calculate rotation between two Vector2s around a pivot 2011-01-09

    Hello all. After a good long Sunday google I am going to have to hang my head in shame and ask the question... What I have is a pivot vector2, a "Previous" vector2 and a "Current" vector2. I would like to be able to calculate the rotation in radians

Copyright (C), All Rights Reserved.

processed in 0.180 (s). 11 q(s)