Problem with rotation

Started by
1 comment, last by h3ro 16 years, 2 months ago
Hallo, I am trying to add rotation to my 2d "engine", but I have some problem The sprite I am trying to draw gets very distorted when i try to rotate it. If i try to draw it at 90 degrees, I only get a single line of pixels drawn with a 90 degree slope. 0, 360 and 180 degrees work perfect. The sprite is located at 0,0 when i rotate it. Here is my code:

void rotatPixelList(std::vector<pixel> &list, int rotation)
{
	double PI =  3.14159265;

	double r = rotation * PI / 180;

	for(int i = 0; i < list.size(); i++)
	{
		list.xPos = list.xPos * cos(-r) - list.yPos * sin(-r);
		list.yPos = list.yPos * cos(-r) + list.xPos * sin(-r);
	}
}

Regards, Ole Kristian
Advertisement
In the for loop, you're modifying the value of xPos in the first line, and then using the new value in the second line where you should have used the original value. Make a copy of xPos before modifying it and use that instead.
Thanks for pointing that out. The rotation works fine now.

But there is one more problem. My writeToScreen() function uses integers for position (as it writes to the actual pixel position) and because of that I get some weired results. Some places I get no colour as the position is rounded off to the wrong position. So I end up with some pixels with nothing in them, and some with two or more.

Hope my managed to make some sense.

Regards,
Ole Kristian

This topic is closed to new replies.

Advertisement