vertex rotation help please

Started by
2 comments, last by too_many_stars 9 years, 7 months ago

Hi guys,

Having some issues with vertex rotation. Each vertex is just a 2d vector with an x,y component. Currently the shape is a simple rectangle, which is meant to rotate aroun its center


void update(float dt,float angle){
		
		/*
		x = ((x - x_origin) * cos(angle)) - ((y_origin - y) * sin(angle)) + x_origin
		y = ((y_origin - y) * cos(angle)) - ((x - x_origin) * sin(angle)) + y_origin
		*/
		

		v1.x=((v1.x-center.x)*cos(angle))-((center.y-v1.y)*sin(angle))+center.x;
		v1.y=((center.y-v1.y)*cos(angle))-((v1.x-center.x)*sin(angle))+center.y;

		v2.x=((v2.x-center.x)*cos(angle))-((center.y-v2.y)*sin(angle))+center.x;
		v2.y=((center.y-v2.y)*cos(angle))-((v2.x-center.x)*sin(angle))+center.y;

		v3.x=((v3.x-center.x)*cos(angle))-((center.y-v3.y)*sin(angle))+center.x;
		v3.y=((center.y-v3.y)*cos(angle))-((v3.x-center.x)*sin(angle))+center.y;

		v4.x=((v4.x-center.x)*cos(angle))-((center.y-v4.y)*sin(angle))+center.x;
		v4.y=((center.y-v4.y)*cos(angle))-((v4.x-center.x)*sin(angle))+center.y;

		
	}

When I run it, it just hangs up, and displays nothing. Any help would be greatly appreciated.

Thanks

Advertisement

You add the center position to undo the shift of the enter to the world origin, done as the last operation. So I assume that the first operation is to subtract the center of interest from the vertex position. If so, then the sign of every second term with the cosines is wrong. Try out:


w1.x=((v1.x-center.x)*cos(angle))-((center.y-v1.y)*sin(angle))+center.x;
w1.y=((v1.y-center.y)*cos(angle))-((v1.x-center.x)*sin(angle))+center.y;

w2.x=((v2.x-center.x)*cos(angle))-((center.y-v2.y)*sin(angle))+center.x;
w2.y=((v2.y-center.y)*cos(angle))-((v2.x-center.x)*sin(angle))+center.y;

w3.x=((v3.x-center.x)*cos(angle))-((center.y-v3.y)*sin(angle))+center.x;
w3.y=((v3.y-center.y)*cos(angle))-((v3.x-center.x)*sin(angle))+center.y;

w4.x=((v4.x-center.x)*cos(angle))-((center.y-v4.y)*sin(angle))+center.x;
w4.y=((v4.y-center.y)*cos(angle))-((v4.x-center.x)*sin(angle))+center.y;

EDIT: Oops, my original suggestion suggestion also suffered from the problem mentioned by Álvaro below, so I've edited it to use other target variables.

Notice that by the time you get to your second line of code, the value of v1.x has already changed. This is a mistake that most of us have made at some point or another. Using my current programming style, the point before and after the rotation would be different variables, and then this problem doesn't happen.


When I run it, it just hangs up, and displays nothing.

It sounds like you need to learn how to debug your code and arrive at more useful descriptions of the problem. Like "when presented with this particular input, the code generates this output, but I expected this other output".

Hi guys,

Thanks for the help, after I got home from work yesterday I managed to get the program to work. The main issue was my drawLine(Vec v1, Vec v2) function, which was hanging up the program.

Thanks again for your help Alvaro and Heagarr!

Mike

This topic is closed to new replies.

Advertisement