Problem with 2d rotation in OpenGL

I have a function to perform sprite rotation :

void Sprite::rotateSprite(float angle){
//Making an array of vertices - 6 for 2 triangles

Vector2<gamePos> halfDims(_rect.w / 2, _rect.h /2);

Vector2<gamePos> bl(-halfDims.x,-halfDims.y);
Vector2<gamePos> tl(-halfDims.x,halfDims.y);
Vector2<gamePos> br(halfDims.x,-halfDims.y);
Vector2<gamePos> tr(halfDims.x,halfDims.y);

bl = rotatePoint(bl,angle) + halfDims;
br = rotatePoint(br,angle) + halfDims;
tl = rotatePoint(tl,angle) + halfDims;
tr = rotatePoint(tr,angle) + halfDims;

/**
1st triangle
**/

//Top right
_dataPointer.vertices[0].setPosition(_rect.x+tr.x,_rect.y+tr.y);

//Top left
_dataPointer.vertices[1].setPosition(_rect.x+tl.x,_rect.y+tl.y);

//Bottom left
_dataPointer.vertices[2].setPosition(_rect.x+bl.x,_rect.y+bl.y);

/**
2nd triangle
**/

//Bottom left
_dataPointer.vertices[3].setPosition(_rect.x+bl.x,_rect.y+bl.y);

//Bottom right
_dataPointer.vertices[4].setPosition(_rect.x+br.x,_rect.y+br.y);

//Top right
_dataPointer.vertices[5].setPosition(_rect.x+tr.x,_rect.y+tr.y);
}

Vector2<gamePos> Sprite::rotatePoint(Vector2<gamePos> pos, float& angle){

Vector2<gamePos> newv;

newv.x = pos.x * cos(angle) - pos.y * sin(angle);
newv.y = pos.y * cos(angle) + pos.x * sin(angle);

return newv;
}


And the result is

Am i doing something wrong ? It happens also when i put small angle (even if i put here angle 1 )

Thanks for help.

• Are you sure the angle is supposed to be in degrees and not radians? Feb 22 '17 at 20:39
• Man, you should really use shaders and matrices to rotate stuff around. Feb 22 '17 at 20:58
• @Bálint Yes I know, but I wanted to try also this alternative.
– Pins
Feb 22 '17 at 22:26
• @Pins this requires you to constantly load in new vertices to the gpu, and that's slow Feb 22 '17 at 23:01
• @Bálint I thought that the thing with vertices is same, because I put every loop objects to spriteBatch(save vbo info + textureID) and buffer the data to GPU. Isn´t it right?
– Pins
Feb 23 '17 at 18:02

Try

angle = angle *(3.14159/180.0)
newv.x = pos.x * cos(angle) - pos.y * sin(angle);
newv.y = pos.y * cos(angle) + pos.x * sin(angle);


Trig functions works with radians so you will have to do the conversion to radians as above. as stated in the comments, It is recommended you convert the (3.14159/180.0) to a constant and use the built in PI constant instead of 3.14159.

• OpenGL doesn't work with anything, the trigonometric functions need radians Feb 22 '17 at 20:57
• And try using built-in constants instead of 3.14159. Those usually go up to 20 decimal places. Feb 22 '17 at 21:09
• @Bálint For the sake of argument - OpenGL does work with something. Old OpenGL functions, alike glRotatef worked with angles in degrees. Modern OpenGL shaders trig functions work with radians. Feb 23 '17 at 13:20
• @Kromster If you count the deprecated stuff, then yes. Feb 23 '17 at 19:05