I am trying to make a more basic version of tiny tanks using SDL and OpenGL. To draw the textures to the screen I am using glBindTexture using GL_QUADS. It uses a 3 by 3 Matrix to know it's position, scale and rotation. For Example:
This is meant to have the position of (64,128) and the scale of 32 by 32. If I draw this to the screen it will draw the texture to the screen fine. The problem comes when I try to rotate that image. I want it so that if I rotate it 90 degrees it looks the same just rotated but with my current code it the quad collapses in on itself then flips upside down and back to front and grows. I am unsure if this is a problem with my code to add rotation to the matrix or if the problem lies with the the points (corners) for the texture not taking in the matrix information in the right way.
For adding rotation to the Matrix I pass in a float to rotate it using
(Coordinates are in the form of row then column)
1 2 3 4 5
constfloat co = cosf(Angle);
constfloat si = sinf(Angle);
m_00 += co; m_01 += si;
m_10 -= si; m_11 += co;
For the setting the corners of the Quad I am using:
I know this is not in a very understandable format but if anyone can work out what I am meant to do to rotate my image and if after that I could use some help with moving the image by what it thinks as up not the worlds up. (So a tank that has been rotated 90 degrees clockwise gets told to go forward it will be going to the worlds right but given the same function to move forward but only 45 degree clockwise rotation will make it go up right)
Thank you if you read this far, I hope that someone can assist me with the problems that I am having.
If you have two linear transformations (in your case are affine transformations, but I'm pretty sure the same principle applies) f and g, and you want to create a third one h such that h(x)=f(g(x)), then what you need to do is compute h's associated matrix by doing Mh=Mf.Mg, where . is matrix multiplication (if you don't know what matrix multiplication is, look it up. It's not intuitive).
You're getting an incorrect h because you're adding your matrices rather than multiplying them.
For the special case of
g=
(x0,0,0)
(0,x1,0)
(x2,x3,0)
f=
(x4,x5,0)
(x6,x7,0)
(0,0,1)
Note: You want the 1 on Mf's last row to avoid zeroing out the last row of the product. It would be a good idea to add it to Mg as well, otherwise it won't be safe to use it as a left hand multiplicand.
h=f.g=
(x0*x4,x1*x5)
(x0*x6,x1*x7)
(x2,x3,1)
If none of this made any sense, I recommend a book on linear algebra.