Register
Handmade Hero»Forums»Code»Change rotation origin
50 posts
Change rotation origin
3 weeks, 5 days ago Edited by C_Worm on March 15, 2020, 4:33 p.m. Reason: Initial post
Hey!

I made a function that rotates my 4 vertices in a rectangle.

However the origin seemes to be (0, 0), which is the top left corner.

How could i change the origin to be in the middle of the rectangle?


rotate():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void rotate(float radian)
{
	objPosData[0]  	= ((x * cos(radian)) 	  - 	(y * sin(radian)));
	objPosData[4]  	= (x * cos(radian)) 	  - 	((y + h) * sin(radian));
	objPosData[8]  	= ((x + w) * cos(radian)) - 	((y + h) * sin(radian));
	objPosData[12] 	= ((x + w) * cos(radian)) - 	(y * sin(radian));
	
	objPosData[1]  	= (x * sin(radian)) 	  + (y * cos(radian));
	objPosData[5]  	= (x * sin(radian)) 	  + ((y + h) * cos(radian));
	objPosData[9]  	= ((x + w) * sin(radian)) + ((y + h) * cos(radian));
	objPosData[13] 	= ((x + w) * sin(radian)) + (y * cos(radian));
}
481 posts
Change rotation origin
3 weeks, 5 days ago
first translate the object such that the rotation point is at the (0, 0)

then do the rotation

then do the reverse translate of step 1

50 posts
Change rotation origin
3 weeks, 4 days ago Edited by C_Worm on March 15, 2020, 9:01 p.m.
Hmmm now the rectangle rotates around its origin, however the rectangle is shrinking and expanding in size aswell..... i can't find out why.

Why is this?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	void rotate(float radian)
	{
		float baseX = 0 - w/2;
		float baseY = 0 - h/2;
		float baseW = baseX + w;
		float baseH = baseY + h;

		float n0X  = (baseX * cos(radian)) - (baseY * sin(radian));    
		float n4X  = (baseX * cos(radian)) - (baseH * sin(radian));
		float n8X  = (baseW * cos(radian)) - (baseH * sin(radian));
		float n12X = (baseW * cos(radian)) - (baseY * sin(radian));     

		float n1Y  = (baseX * sin(radian)) + (baseY * cos(radian));        
		float n5Y  = (baseX * sin(radian)) + (baseH * cos(radian));
		float n9Y  = (baseW * sin(radian)) + (baseH * cos(radian));
		float n13Y = (baseW * sin(radian)) + (baseY * cos(radian));         

		objPosData[0]  	= n0X  + x;
		objPosData[4]  	= n4X  + x;
		objPosData[8]  	= n8X  + x + w;
		objPosData[12] 	= n12X + x + w;

		objPosData[1]  	= n1Y  + y;
		objPosData[5]  	= n5Y  + y + h;
		objPosData[9]  	= n9Y  + y + h;
		objPosData[13] 	= n13Y + y
	
	
    }
Simon Anciaux
742 posts
Change rotation origin
3 weeks, 3 days ago
I believe you don't need to add the width and height at the end:
1
2
3
4
5
6
7
8
9
objPosData[0]  	= n0X  + x;
objPosData[4]  	= n4X  + x;
objPosData[8]  	= n8X  + x;
objPosData[12] 	= n12X + x;

objPosData[1]  	= n1Y  + y;
objPosData[5]  	= n5Y  + y;
objPosData[9]  	= n9Y  + y;
objPosData[13] 	= n13Y + y;
50 posts
Change rotation origin
3 weeks, 3 days ago
Final Solution was this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	void rotate(float radian)
	{

		float baseX = 0 - w/2;
		float baseY = 0 - h/2;
		float baseW = baseX + w;
		float baseH = baseY + h;

		float n0X  = (baseX * cos(radian)) - (baseY * sin(radian));    
		float n4X  = (baseX * cos(radian)) - (baseH * sin(radian));
		float n8X  = (baseW * cos(radian)) - (baseH * sin(radian));
		float n12X = (baseW * cos(radian)) - (baseY * sin(radian));     

		float n1Y  = (baseX * sin(radian)) + (baseY * cos(radian));        
		float n5Y  = (baseX * sin(radian)) + (baseH * cos(radian));
		float n9Y  = (baseW * sin(radian)) + (baseH * cos(radian));
		float n13Y = (baseW * sin(radian)) + (baseY * cos(radian));         

		objPosData[0]  	= n0X   + x + (w/2);	
		objPosData[4]  	= n4X   + x + (w/2);	
		objPosData[8]  	= n8X   + x + (w/2);	
		objPosData[12] 	= n12X  + x + (w/2);

		objPosData[1]  	= n1Y   + y + (h/2);	
		objPosData[5]  	= n5Y   + y + (h/2);	
		objPosData[9]  	= n9Y   + y + (h/2);	
		objPosData[13] 	= n13Y  + y + (h/2);
	}