2

I would like to make a 2D game engine which is heavily focused on vectors. I would also like use a transformation matrix to translate, rotate and scale sprites that I would like to load in somehow.

I'm trying to do this without an external library in case anyone was wondering.

Does anyone know if I'm even remotely in the right direction with this? Applying a transform to an images coordinates? And what data to I input and output from my matrix exactly? I understand what the matrices are, but my experience with this is limited and I don't really know the next step.

It's all a bit abstract at the minute, and I wish I could explain it better.

All my source code: mainComponent, vector2f, matrix3f and transform.

Outline of what is in the classes so far: mainComponent is fairly obvious, just Main() at the minute. Vector2f has basic math, getters and setters for 2D vectors(float). Matrix3f has an initialization of translation, rotation and scale matrices and a multiply method. And I have no idea what Transform is really doing, all I know is that in there, my matrices get set, take their inputs and multiply together. Dont really know what to do to attach it to something on a screen, although I can make a basic BufferedImage pixel array thing that might be what I want.

The next problem is loading in sprites :/

tshepang
  • 12,111
  • 21
  • 91
  • 136
Sam Walls
  • 185
  • 1
  • 4
  • 12
  • You multiply the original coordinates of a feature (v.g. a vertex) by the transformation matrix and get the new coordinates of the feature. – SJuan76 Nov 14 '13 at 18:54
  • right, so i have to turn my coordinates into a 3x1 homogeneous matrix so it can be multiplied? (and vice versa) – Sam Walls Nov 14 '13 at 19:00
  • Yes that's right. Usually you just use matrices as the coordinates, without conversions. – SJuan76 Nov 14 '13 at 19:03
  • You could also multiply all of your points at once. So if you have 256x256 points, you'll have a matrix multiplication of the transformation matrix (3x3) by the point matrix (3x65536). This will result in a new point matrix where each column is your new point location. – sdasdadas Nov 14 '13 at 19:06

1 Answers1

3

Theory

A translation matrix looks like this:

[1 0 tx]
[0 1 ty]
[0 0  1]

where tx is the translation on the x-axis, and ty is the translation on the y-axis.

Now, this is a 3x3 matrix. So we need to multiply it by a 3x_ vector. But all of our points are in two dimensions.

So we use homogenous coordinates, meaning given a point (x,y), we represent it as the vector:

[x,y,1]

Now, we can multiply the translation matrix by the vector:

[1 0 tx]
[0 1 ty]   x  [x,y,1]'  = [x + tx, y + ty, 1]'
[0 0  1]

If we drop the homogenous value, we receive our new coordinate.

Application

So how do we apply a transformation to an image. It's quite simple, just go through all of the points in the image and multiply them by your transformation matrix to receive their new location.

If we apply a rotation matrix to the pixel at (12, 14) with a color of red:

[cos(theta)  sin(theta)  0]
[-sin(theta) cos(theta)  0]    x [12, 14, 1]' = [x', y', 1]
[0           0           1]

Now we find the position (x',y') in the image and set it to red. Repeat until you've transformed all of the pixels in this way.

sdasdadas
  • 23,917
  • 20
  • 63
  • 148