Advertisement

Can't properly rotate in my sprite batch.

Started by January 01, 2019 04:08 PM
2 comments, last by MidLevelGameDev 6 years, 1 month ago

Simple problem compared to what is usually posted around here, but I'm trying to draw a rotated textured rectangle in my sprite batch:


    if(sprt->tex != tex) {
        flush();
        sprt->tex = tex;
    }

    glm::vec2 oldHsize = hsize;
    hsize.x = oldHsize.x * cos(angle) - oldHsize.y * sin(angle);
    hsize.y = oldHsize.x * sin(angle) + oldHsize.y * cos(angle);

    data.push_back({{pos.x - hsize.x, pos.y - hsize.y}, {0, 1}});
    data.push_back({{pos.x + hsize.x, pos.y - hsize.y}, {1, 1}});
    data.push_back({{pos.x - hsize.x, pos.y + hsize.y}, {0, 0}});
    data.push_back({{pos.x + hsize.x, pos.y - hsize.y}, {1, 1}});
    data.push_back({{pos.x - hsize.x, pos.y + hsize.y}, {0, 0}});
    data.push_back({{pos.x + hsize.x, pos.y + hsize.y}, {1, 0}});
}

hsize is half the size of the sprite being drawn. But it seems to draw it completely incorrectly. If I slowly increase the value of angle, here's what it looks like (in the attachments).

Hi @Midnightas,

on your code, you're applying the same value for the + and the - steps from the location; you'll need to do the transformation separately for each point of your square.

If you draw it out on paper, you'll see what I mean (I'm using the DX Toolkit's spritebatch code for my sprite batch so I don't have the code handy sorry).

Steve

Advertisement

Yes, that was it, thanks!

This topic is closed to new replies.

Advertisement