Thanks mmozeiko. I can't remember exactly what day so I'll download BitTorrent Sync and see if I can find it.
From Bill's and ratchet's explanation I think I understand it - kind of.
I'm now rotating a rectangle around it's origin, however when I try and rotate around the center it isn't working correctly.
I have the below code to rotate the rectangle:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | float angle = gameState->TotalTime;
Vector2 origin = { 0.5f*static_cast<float>(drawBuffer->Width),
0.5f*static_cast<float>(drawBuffer->Height) };
Vector2 xAxis = CreateVector2(100.0f, 0.0f);
xAxis = CreateVector2(xAxis.X * Cos(angle) - xAxis.Y * Sin(angle),
xAxis.Y * Cos(angle) + xAxis.X * Sin(angle));
Vector2 yAxis = Perp(xAxis);
PushCoordinateSystem(
renderGroup,
origin,
xAxis,
yAxis);
|
This works fine. Now if I understand correctly if I want to rotate around the rectangle center, I need to translate the rectangle. So my origin is the screen center and the x axis is 100, 0 with the y being 0, 100 - calculated using the Perp method. So the center of my rectangle should be 50, 50?
If I add this translation into my code it doesn't rotate correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | float angle = gameState->TotalTime;
Vector2 origin = { 0.5f*static_cast<float>(drawBuffer->Width),
0.5f*static_cast<float>(drawBuffer->Height) };
Vector2 xAxis = CreateVector2(100.0f, 0.0f);
xAxis -= CreateVector2(50.0f, 50.0f);
xAxis = CreateVector2(xAxis.X * Cos(angle) - xAxis.Y * Sin(angle),
xAxis.Y * Cos(angle) + xAxis.X * Sin(angle));
xAxis += CreateVector2(50.0f, 50.0f);
Vector2 yAxis = Perp(xAxis);
PushCoord(
renderGroup,
origin,
xAxis,
yAxis);
|
You can see I am subtracting the center of the rectangle from the x axis, rotating, and then adding it back in. You can see the results
here.
The first gif is the rectangle rotating around the origin correctly and the second is my attempt at rotating around the center - which obviously is not working. What am I doing wrong?