
This thread is locked; no one can reply to it. 
1
2

Position of point when rotated 
Vasco Freitas
Member #6,904
February 2006

Given the distance of a point from a rotation axis in coordinates (for example, point A is 10 x units and 20 y units from axis) when the rotation angle is 0, how do I calculate the position of the point for any rotation angle (in radians)? I've tried "x_pos = axis_x + x_distance * cos(angle); y_pos = axis_y + y_distance * sin(angle);" but this only seems to work in some cases 
Epsi
Member #5,731
April 2005

I do it this way: (probably not the best method, but it works) I use the vector length of pivot.x,pivot.y > blit_point.x,blit_point_y and the angle of the vector (8.0 and 4.0 dec degree in that case) let's say "18". head_pos_ is the coordinate of the pivot of the head bitmap. eyes_x and y are the "blitting point's" coord float eyes_x = head_pos_x + int(18.0 * cosinus(head_angle 90.0)) + int(8.0 * cosinus(head_angle)); float eyes_y = head_pos_y + int(18.0 * sinus(head_angle 90.0)) + int(4.0 * sinus(head_angle));
___________________________________ piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from." 
Archon
Member #4,195
January 2004

Assuming 0 degrees is 'right', +Rotation would be counterclockwise. 
Ceagon Xylas
Member #5,495
February 2005

I had this code right for a long time, but didn't really understand how radians worked. Remember to increment in decimals and add in conditions such as rotation+=0.0036; if(rotation>M_PI*2) rotation=0; if(rotation<0) rotation=M_PI*2; Without limiters your line or whatever you're drawing/calculating goes CRAZY =P And, Epsi, what's that 90.0 for? To get it into the 'right' plane? I mean, so that 0 isn't pointing right, but instead, point up? 
Epsi
Member #5,731
April 2005

Quote: And, Epsi, what's that 90.0 for? To get it into the 'right' plane? I mean, so that 0 isn't pointing right, but instead, point up? yup, or at least if I remember correctly. What I did was a little dirty hack (the angle is not even the same on x/y axis), so I'll be happy if another solution shows up ___________________________________ piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from." 
Evert
Member #794
November 2000

For rotation of a point around the origin over an angle , use To rotate around an arbitrary point, translate to that point first, then rotate, then translate back. It wasn't entirely clear to me what you wanted to do, hope this helps. Quote:
if(rotation>M_PI*2) rotation=0; That's quite wrong: there's nothing wrong with angles outside this range. 
Archon
Member #4,195
January 2004

Mine should be correct: For 0 degrees is 'up' +Rotation would be clockwise > or just make the 'distance' negative in the x calculation for counterclockwise. For 0 degrees is 'down' Default: clockwise For 0 degrees is 'left' Default: clockwise 
Vasco Freitas
Member #6,904
February 2006

Thank you for all your responses, I'll try them later today. But there is one problem with most of your solutions, that I have to calculate the distance, and for that I have to use sqrt() which is slow. Evert's solution doesn't use the distance but I've never done matrix multiplications 
Ceagon Xylas
Member #5,495
February 2005

You mean use sqrt() for acquiring the magnitude? 
Johan Halmén
Member #1,550
September 2001

Evert's matrix is just 1337. It's ok to use following: x' = x*cos(v)  y*sin(v) I usually reinvent this wheel by dividing the point (x, y) to two "vectors" (x, 0) and (0, y). ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
XG
Member #856
December 2000

Quote: sqrt(x+x*y+y)? You fail it. sqrt(x*x + y*y).  
Matt Smith
Member #783
November 2000

Felipe Maia
Member #6,190
September 2005

Evert's method is the best. You should always use vectors and not angles. Ever wondered why phisics is mainly based in vectors? Because they work very well. You can also include the translations in Evert's matrix, making it a 3x3 matrix. I would write how, but I don't know how to use the mock up well... 
XG
Member #856
December 2000

Note: homogeneous coordinates.  
Felipe Maia
Member #6,190
September 2005

The w should be 1 
Vasco Freitas
Member #6,904
February 2006

Well I tried the matrix multiplication but it didn't work. Maybe it's because the coordinate and/or angle system is different? Mine is like this: 
Felipe Maia
Member #6,190
September 2005

Aye, you're inverting the y axis, due to screen mapping, just switch the y by y. 
Vasco Freitas
Member #6,904
February 2006

Felipe Maia
Member #6,190
September 2005

You need this: 
Vasco Freitas
Member #6,904
February 2006

Still doesn't work... I feel that there is something very wrong because the position of the point changes much more than it should (it goes all over the screen when it should be near the center). Anyway, OFFSET_X should be the x distance of the point to the axis right? And what you said is to change xPos (the x position of the axis) to (xPos  OFFSET_X) and yPos to (yPos  OFFSET_Y) right? Maybe the problem has to do with the way I'm doing things. What I'm trying to do is set the position of a gun on a ship. The center of the ship is the rotation axis, and when the screen is rendered, everything is rotated except the ship, that is always motionless. I can't just set the gun coordinates because it's rotated, so I have to compensate for that rotation so the gun is always at the same position on the ship. 
Felipe Maia
Member #6,190
September 2005

The offsext_x I meant was the center of the ship, the rotation point. 
XG
Member #856
December 2000

Quote: The w should be 1 That depends on whether you are transforming a point or a direction.  
Vasco Freitas
Member #6,904
February 2006

Felipe Maia said: The offsext_x I meant was the center of the ship, the rotation point. So in that case, what's xPos? The position of the point I want to rotate, or the distance of that point to the axis (in x)? 
Felipe Maia
Member #6,190
September 2005

Let's say that you want to rotate the square attached using the blue point as the rotation center. [edited} 
Archon
Member #4,195
January 2004

Quote: So in that case, what's xPos? I'm guessing it's the position of the other object... 

1
2
