Handmade Hero»Forums»Code
55 posts
Rotating and scaling rectangles
I love the recent videos about the renderer in particular, but in my code I can't find what's causing the following bug. I made a short recording. In the screen recording I rotate a rectangle with the screen center as origin, x-axis is (cos angle, sin angle), and the y-axis the perpendicular. I verified that I compute the correct dot products in the draw rectangle function and check that the result for every edge is < 0 etc. In between the 0, 90, 180, and 270 degrees angles the rectangle changes shape... Anyone have an idea what I'm doing wrong?

Thank you very much
Andrew Bromage
183 posts / 1 project
Research engineer, resident maths nerd (Erdős number 3).
Rotating and scaling rectangles
It's hard to say without looking at code, but could you humour me and verify that the x axis and y axis are actually perpendicular? Like, take their dot product and make sure that the result is close to zero.
55 posts
Rotating and scaling rectangles
Edited by elle on
I printed the dot product of the x-axis and y-axis in the game_update_and_render loop to the console each frame and it's always 0.

Update: I removed the edge tests in the render function and clamped the u and v values to [0, 1] and this makes it work as expected. I don't know what I should change here, though:

Vector2 p = {(float)x, (float)y};
Vector2 v = p - origin;

float edge0 = dotProduct(v, -perpendicular(xAxis));
float edge1 = dotProduct(v - xAxis, -perpendicular(yAxis));
float edge2 = dotProduct(v - xAxis - yAxis, perpendicular(xAxis));
float edge3 = dotProduct(v - yAxis, perpendicular(yAxis));

(x and y are the values in the loop.)

if ((edge0 < 0) &&
(edge1 < 0) &&
(edge2 < 0) &&
(edge3 < 0)) {
// draw code
}

If I change this test to if (1) {}, the rectangle draws and rotates correctly. :S

Update 2; I also tried the following to verify if the perpendicular function is wrong but the results are the same:

float edge0 = dotProduct(v, -yAxis);
float edge1 = dotProduct(v - xAxis, xAxis);
float edge2 = dotProduct(v - xAxis - yAxis, yAxis);
float edge3 = dotProduct(v - yAxis, -xAxis);

Update3: I disabled each edge test separately and apparently they all play a part in the problem or the data passed to the edges is wrong...

How can I debug this? :(
55 posts
Rotating and scaling rectangles
Edited by elle on
Omg! I found out the issue. I only incremented the pointer of the destination pixel inside the if clause of the edge test.