Roy Heijmans
Roy Heijmans

Reputation: 77

2d rotation opengl

Here is the code I am using.

#define ANGLETORADIANS 0.017453292519943295769236907684886f // PI / 180
#define RADIANSTOANGLE 57.295779513082320876798154814105f   // 180 / PI

rotation = rotation *ANGLETORADIANS;

cosRotation = cos(rotation);
sinRotation = sin(rotation);

for(int i = 0; i < 3; i++)
{
    px[i] = (vec[i].x + centerX) * (cosRotation - (vec[i].y + centerY)) * sinRotation;
    py[i] = (vec[i].x + centerX) * (sinRotation + (vec[i].y + centerY)) * cosRotation;
    printf("num: %i, px: %f, py: %f\n", i, px[i], py[i]);
}

so far it seams my Y value is being fliped.. say I enter the value of X = 1 and Y = 1 with a 45 rotation you should see about x = 0 and y = 1.25 ish but I get x = 0 y = -1.25.

Also my 90 degree rotation always return x = 0 and y = 0.

p.s I know I'm only centering my values and not putting them back where they came from. It's not needed to put them back as all I need to know is the value I'm getting now.

Upvotes: 0

Views: 308

Answers (2)

SinisterMJ
SinisterMJ

Reputation: 3509

Your brackets are wrong. It should be

px[i] = ((vec[i].x + centerX) * cosRotation) - ((vec[i].y + centerY) * sinRotation);
py[i] = ((vec[i].x + centerX) * sinRotation) + ((vec[i].y + centerY) * cosRotation);

instead

Upvotes: 1

acraig5075
acraig5075

Reputation: 10756

Your bracket placement doesn't look right to me. I would expect:

px[i] = (vec[i].x + centerX) * cosRotation - (vec[i].y + centerY) * sinRotation;
py[i] = (vec[i].x + centerX) * sinRotation + (vec[i].y + centerY) * cosRotation;

Upvotes: 2

Related Questions