2
\$\begingroup\$

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 Image of problem

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

Thanks for help.

\$\endgroup\$
6
  • \$\begingroup\$ Are you sure the angle is supposed to be in degrees and not radians? \$\endgroup\$
    – user253751
    Feb 22 '17 at 20:39
  • \$\begingroup\$ Man, you should really use shaders and matrices to rotate stuff around. \$\endgroup\$
    – Bálint
    Feb 22 '17 at 20:58
  • \$\begingroup\$ @Bálint Yes I know, but I wanted to try also this alternative. \$\endgroup\$
    – Pins
    Feb 22 '17 at 22:26
  • \$\begingroup\$ @Pins this requires you to constantly load in new vertices to the gpu, and that's slow \$\endgroup\$
    – Bálint
    Feb 22 '17 at 23:01
  • \$\begingroup\$ @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? \$\endgroup\$
    – Pins
    Feb 23 '17 at 18:02
4
\$\begingroup\$

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.

\$\endgroup\$
4
  • 5
    \$\begingroup\$ OpenGL doesn't work with anything, the trigonometric functions need radians \$\endgroup\$
    – Bálint
    Feb 22 '17 at 20:57
  • 3
    \$\begingroup\$ And try using built-in constants instead of 3.14159. Those usually go up to 20 decimal places. \$\endgroup\$
    – Bálint
    Feb 22 '17 at 21:09
  • 3
    \$\begingroup\$ @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. \$\endgroup\$
    – Kromster
    Feb 23 '17 at 13:20
  • \$\begingroup\$ @Kromster If you count the deprecated stuff, then yes. \$\endgroup\$
    – Bálint
    Feb 23 '17 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.