94 posts
Change rotation origin
Edited by C_Worm on 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 = ((x * cos(radian)) - (y * sin(radian))); objPosData = (x * cos(radian)) - ((y + h) * sin(radian)); objPosData = ((x + w) * cos(radian)) - ((y + h) * sin(radian)); objPosData = ((x + w) * cos(radian)) - (y * sin(radian)); objPosData = (x * sin(radian)) + (y * cos(radian)); objPosData = (x * sin(radian)) + ((y + h) * cos(radian)); objPosData = ((x + w) * sin(radian)) + ((y + h) * cos(radian)); objPosData = ((x + w) * sin(radian)) + (y * cos(radian)); } 
498 posts
Change rotation origin
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

94 posts
Change rotation origin
Edited by C_Worm on
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 = n0X + x; objPosData = n4X + x; objPosData = n8X + x + w; objPosData = n12X + x + w; objPosData = n1Y + y; objPosData = n5Y + y + h; objPosData = n9Y + y + h; objPosData = n13Y + y } 
Simon Anciaux
1108 posts
Change rotation origin
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 = n0X + x; objPosData = n4X + x; objPosData = n8X + x; objPosData = n12X + x; objPosData = n1Y + y; objPosData = n5Y + y; objPosData = n9Y + y; objPosData = n13Y + y; 
94 posts
Change rotation origin
  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 = n0X + x + (w/2); objPosData = n4X + x + (w/2); objPosData = n8X + x + (w/2); objPosData = n12X + x + (w/2); objPosData = n1Y + y + (h/2); objPosData = n5Y + y + (h/2); objPosData = n9Y + y + (h/2); objPosData = n13Y + y + (h/2); }