Advertisement

simple rotation question

Started by August 01, 2002 06:51 PM
6 comments, last by Damocles 22 years, 6 months ago
I feel kinda stupid for not knowing how to do this, but after several different tries, and several spectacular failures, I give up and will let someone who knows basic maths help me Basically, I want to rotate four set points around a central point. Each point is represented as an x/y coord, it could be either relative to the centre or absolute, it makes no difference. I also have a rotation value (the degrees) and the rotation speed. But when I threw those values into a series of sin/cos arguments, the best I could get it to do was flip over the x/y axis instead of rotating around the z So okay, someone show me how simple this is and I can die a happy yet ignorant man
------------------------------------------[New Delta Games] | [Sliders]
x' = cos(a) * x - sin(a) * y
y' = sin(a) * x + cos(a) * y
z' = z

where a is your angle, usually in radians
[EDIT no post is short enough as to contain no errors
---------------------------
I may be getting older, but I refuse to grow up

[edited by - Dreamforger on August 1, 2002 10:39:41 PM]
I may be getting older, but I refuse to grow up
Advertisement
Let x,y be the point you want to rotate by an angle theta, and cx,cy be the point you want it rotated about. X'',Y'' is the resultant coordinate.

Then,

X'' = cos(theta) * (x-cx) - sin(theta) * (y-cy) + cx
Y'' = sin(theta) * (x-cx) + cos(theta) * (y-cy) + cy

Well I seem to have royally screwed up...

m_vFocusPt.x = Cos(CamTheta) * m_vEyePt.x - Sin(CamTheta) * m_vEyePt.z

m_vFocusPt.x = Sin(CamTheta) * m_vEyePt.x + Cos(CamTheta) * m_vEyePt.z

I tried changing the camtheta to degrees and radians, and I''ve tried the code with m_vfocuspt instead of m_vEyePt..

Any Help?

yes, yes I am stupid.
yes, yes I am stupid.
Well, I''m getting closer now thanks to yellowjon, but it''s still not right. What I''m doing is using OpenGL quads, and I feed in the eight coords (2 pairs) to draw (in essence) a 2d quad that spins. But insatead what Im getting are quads that flip on their x/y coords and now spin around a point.

Here''s the code I use. I use rotX/rotY as the origin coord. Basetl_x, Basetl_y through Basebr_x and Basebr_y are the coord pairs for the four corners of the quad.

The first section is to set the central origin, in here xpos/ypos are the upper left coord of the quad, w/h are width and height:


  void Actor::setAutoCentre(){	rotX=xpos+(w*0.5f);	rotY=ypos+(h*0.5f);	Basetl_x = rotX-(w*0.5f);	Basetl_y = rotY-(h*0.5f);	Basetr_x = rotX+w*0.5f;	Basetr_y = rotY-(h*0.5f);	Basebl_x = rotX-(w*0.5f);	Basebl_y = rotY+h*0.5f;	Basebr_x = rotX+w*0.5f;	Basebr_y = rotY+h*0.5f;}  


and this second function rotates then draws the quad:


  if (oldRotation!=rotation)		{			float r = deg2rad(rotation);			tl_x=cosf(r) * (Basetl_x-rotX) - sinf(r) * (Basetl_y-rotY) + rotX;			tl_y=sinf(r) * (Basetl_x-rotX) - cosf(r) * (Basetl_y-rotY) + rotY;			bl_x=cosf(r) * (Basebl_x-rotX) - sinf(r) * (Basebl_y-rotY) + rotX;			bl_y=sinf(r) * (Basebl_x-rotX) - cosf(r) * (Basebl_y-rotY) + rotY;			tr_x=cosf(r) * (Basetr_x-rotX) - sinf(r) * (Basetr_y-rotY) + rotX;			tr_y=sinf(r) * (Basetr_x-rotX) - cosf(r) * (Basetr_y-rotY) + rotY;			br_x=cosf(r) * (Basebr_x-rotX) - sinf(r) * (Basebr_y-rotY) + rotX;			br_y=sinf(r) * (Basebr_x-rotX) - cosf(r) * (Basebr_y-rotY) + rotY;			oldRotation=rotation;		}		renderSprite4f(tl_x, tl_y, tr_x, tr_y,				br_x, br_y, bl_x, bl_y, textureID);  


renderSprite4f is a function of mine that takes the 4 pairs of coords and a texture ID and draws the quad accordingly. I''m guessing my problem lies in the rotation maths, but I used the same as what yellowjon recommended
------------------------------------------[New Delta Games] | [Sliders]
In your second function, when you calculate the y coordinates, the cosine should have a positive sign in front of it.
Advertisement
In your second function, when you calculate the y coordinates, the cosine should have a positive sign in front of it.
D''OH! That was stupid. Must be time to get my eyes checked Danke yellowjon
------------------------------------------[New Delta Games] | [Sliders]

This topic is closed to new replies.

Advertisement