Public Group

# Problems with rotation code

This topic is 3777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everybody, I have the following code to rotate a point around the Y axis of the origin: //rotate point "x, z" around Y at the origin by "a" degrees void Player::rotateAroundY(float &x, float &z, float a) { float cosa = cos(a); float sina = sin(a); float ax = x; float az = z; x = ((az * sina) + (ax * cosa)); z = ((az * cosa) - (ax * sina)); }; This is how it is being called in my test code (Simply to draw out the hitbox visibly so I can test my collision detection (which works, aside from factoring in this broken rotation)) glBegin(GL_LINE_LOOP); for (vertexVector::iterator vertex = m_helihitpoints.begin(); vertex != m_helihitpoints.end(); vertex++) { float xa = vertex->x; float ya = vertex->y; float za = vertex->z; rotateAroundY(xa, za, m_yRotation); glVertex3f(xa, ya, za); } glEnd(); (m_helihitpoints is a vector containing the vertices of the hitbox, m_yRotation is a value between 0 and 359.999.. etc.) Okay so the problems are as follows: Firstly, the points seem to rotate smoothly although at some 60 times the desired amount, (that is, it performs one whole rotation for every increment of m_yRotation by 6 (not the desired 1 rotation for 360 degrees)) and secondly (probably tied in) I would have thought that 0 degrees and 360 degrees would provide the same result? As it stands, when m_yRotation loops back from 0 to 359.99 or forward from 359.99 to 0, the resulting rotation jumps about 30 degrees in an instant... Maths isn't my strongest point, and I'm sure I'm either doing it wrong or it's something trivial. Sorry if this hasn't been the best explanation, been working on it for a bit too long now and my brain has kind of gone kaput. Please don't hesitate to ask any questions if you don't understand what I'm going on about!! Many thanks, Ross

##### Share on other sites
The trigonometric functions takes the angle in radians, not degrees, so there's 2*pi or approximately 6.28 radians for a full circle.

##### Share on other sites
Just an idea -
Look into using either matrices or quaternions to do rotations. There's a few reasons to do this:

1) Quaternions give you very smooth rotations in 3D space, and are faster to apply to a vertex than calculating all those sines and cosines.
2) Matrix transformations are still faster than sines and cosines, but not as fast or smooth as quaternions (in some cases). But you get both translation and rotation in one operation, in 3D or 2D space. You also get scaling. All in one operation. Plus, you can later do them with a fragment shader, which is SUPER fast.

##### Share on other sites
Thanks very much for your help guys!
I do plan on digging deeper into these things (And brushing up on my matrix maths skills in particular). At the moment I have three concurrent projects (incl. my dissertation) on the go, so I'm a bit tight for time but I'm loving the work in graphics :D

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002315
×