Super 2d Transformations

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

8.

Transformations in 2 Dimensions

Vector and matrix operations are used to transform the images represented by
vectors. Any image can be considered to be made up of points (think of the pixels on a
monitor screen), and every one of these points has a position vector. For purposes of
demonstration, we can confine ourselves to the vertices of a figure- the corner points.

Consider the shape delineated by the points with position vectors , , , and

. I’ll refer to this as the house.

There are five basic transformations of any 2D figure,


1. Translation
2. Reflection
3. Scaling or stretching
4. Rotation
5. Shearing or deformation

1. Translation entails moving all points in a figure by the same amount in a straight
line. This just means that we add the same number of units to each component of each
position vector making up the figure. For instance, to move the house down five units

and left 6 units, add the vector To each position vector of the house.

1
Visually;

The vector can be considered as a force that pushes the house down and left. The
operation of vector addition therefore amounts to simple movement in a plane. The
original computer games (Pong, Space Invaders) relied on very little more than this,
with the same shapes moving around on a flat background, and no changes of
perspective.

Exercise 1. Consider the shape defined by the points , , , , a vaguely runic-


looking letter P. Draw this on graph paper,

2
then apply the translation .

2. Reflection

Any line can be drawn across the plane, and the figure reflected in it, but reflection
across the axes is particularly important.

The matrix for reflection across the x axis is:

The y-coordinate of any vector we multiply by this has its sign flipped, while the x
coordinate is unchanged. For our house;

And visually:

3
Games like Pacman, Donkey Kong and the earliest Mario Brothers got by mostly on
translation and reflection.

Similarly the matrix

causes reflection across the y axis.

Exercise 2. Reflect the runic P of Exercise 1 across the y axis.

3. Scaling (Stretching)

Stretching a figure amounts to multiplying each component in a vector by a given


amount- the same amount if you want uniform scaling (enlargement), different
amounts for differential scaling.

The matrix for uniform scaling by a factor of 2 is:

For the house;

4
(All these results are the same as if we’d just multiplied the vectors by the scalar 2,
but the matrices have room for additional information and operations in them. We’ll
come to that.)

The matrix for a differential stretching by a factor of 3 in, say, the x direction only is;

For the house;

5
Much of the Battleground game’s 3D effect was achieved by allowing tanks to change
size depending on their virtual proximity. Apart from this, not much more than
translation and reflection was used.

Exercise 3. Differentially stretch the runic P by a factor of 2 in the y direction and 3


in the x direction.

4. Rotation

Rotation consists of taking a point in the plane and turning the whole plane around

that point. The origin Is the most convenient point for demonstration. As we might
expect, since the rotation must go through a particular angle, the entries in the
transformation matrix are trigonometric.

The matrix for a rotation of 60 anticlockwise about the origin is

So on the first point this makes

6
Applying the same matrix to the other points,

The Elite game revolutionised graphics, using techniques involving 3D


transformations (as in the next section). However, the 2D transformations already
listed, with rotation simulating the roll of the 1st-person craft, would be enough for a
2D approximation of Elite.

7
Exercise 4. Rotate the runic P by 120 anticlockwise around the origin.

5. Shearing or deformation

The real use of shearing (or deformation) comes with 3D, but the 2D case illustrates
the principle. The transformation changes the angles of the figure while stretching it
in a given direction.

The matrix for a shear by a factor of 2 parallel to the y-axis is

Applying this to the points of the house;

8
Similarly, the matrix for a shear by a factor of 2 parallel to the x-axis is

Exercise 5. Shear the runic P by a factor of 2 parallel to the x-axis.

You might also like