<p>In order to dissociate the rotation from the CPU speed, you need to use some sort of timing in your function. I recommend passing in a time value of some sort. The easiest thing is to pass in the number of seconds since the app started. How you do that depends on your particular OS, which you didn't specify. </p>

<p>Once you have a timing constant, you can generate a triangle wave to control the rotation. This formula will oscillate between 0 and 1 over time:</p>

<pre><code>double rotation = 2.0 * fabs(fmod(time, 1.0) - 0.5);
</code></pre>

<p>You can make it go from 0 to 10 by multiplying the result by 10:</p>

<pre><code>rotation *= 10.0;
</code></pre>

<p>Then you plug that into your <code>glRotated()</code> call:</p>

<pre><code>glRotated(rotation, 0, 1, 0);
</code></pre>

<p>If you want to make the motion a little smoother, you could replace the triangle wave with a sine wave, like this:</p>

<pre><code>double rotation = 0.5 * (sin(time * 2.0 * M_PI + M_PI / 2.0) + 1.0);
</code></pre>

CC BY-SA 3.0
2018-03-03T05:47:17.710Z
Thanks. I don't yet know how to obtain the time. I am using Windows 10.
Stackmm
CC BY-SA 3.0
2018-03-03T05:50:50.153Z
I haven't dealt with Windows in a few years, but [this question](https://stackoverflow.com/questions/3729169/how-can-i-get-the-windows-system-time-with-millisecond-resolution) on Stack Overflow seems like it might have some good ideas. At least there are lots of different answers so one of them will probably do the trick.
user1118321
CC BY-SA 3.0
2018-03-03T11:09:18.030Z
If you want high precision time in the win32 API you can use QueryPerformanceCounter and QueryPerformanceFrequency
PaulHK
CC BY-SA 3.0
2018-03-04T01:23:39.743Z
Is it possible to do this using frame rate instead of time?
Stackmm
CC BY-SA 3.0
2018-03-04T01:58:05.843Z
If you know the frame rate and know how many frames you've already processed, then you can calculate the time by multiplying the frame rate by the number of frames.
user1118321
CC BY-SA 3.0
2018-03-04T06:19:32.640Z
1/FPS = time taken per frame, so yes you can use that. However to calculate FPS you would have needed to take a snapshot of some timer anyway ?
PaulHK
CC BY-SA 3.0
2018-03-05T20:10:04.313Z
Thanks. I think the question I am really asking is the following: What I want to do is make sure the speed of my motion is the same regardless of whether I oscillate between 0 and 1 or between 0 and 10. In the response, user1118321 says "You can make it go from 0 to 10 by multiplying the result by 10:..", but this seems to increase the speed of my motion. How do I maintain speed?
Stackmm
CC BY-SA 3.0
2018-03-06T02:28:53.340Z
When you multiply the rotation by 10 you're changing the amplitude of the rotation, but not the frequency. So it will rotate 10 degrees per second instead of 1 degree per second. If you want to also change the frequency to match, you should divide time by 10. Then you'll get 10 degrees per 10 seconds, which is still 1 degree per second.
user1118321
CC BY-SA 3.0
2018-03-03T05:18:42.913Z
2018-03-03T05:25:14.560Z
2018-03-03T05:25:14.560Z
user1118321
<p>I want to write code for a cube rotating 10 degrees back and forth on its y-axis in an OpenGL application. Right now I have this written in my display method, however it depends on processor speed, and it may be more complicated than it needs to be. I would like to make the code independent of processor speed.</p>

<pre><code>GLfloat rot = 10;
GLfloat angle = 0, angle2 = 0, angle3 = 0;

if (angle &lt; rot){
   glRotated(angle, 0, 1,0);                                  
   angle++;
}
else{
   if (angle2 &lt; rot * 2 ){
     glRotated(rot - angle2, 0, 1, 0);      
     angle2++;
    }

else{
    if (angle3 &lt; rot ){
     glRotated(-rot + angle3, 0, 1, 0);     
     angle3++;
    }

else{ angle = 0, angle2 = 0, angle3 = 0; }
</code></pre>

<opengl><transformations>
Back and forth rotation on Y-axis
Stackmm
# Back and forth rotation on Y-axis

(Asked by: username_0 on 2018-03-03)

I want to write code for a cube rotating 10 degrees back and forth on its y-axis in an OpenGL application. Right now I have this written in my display method, however it depends on processor speed, and it may be more complicated than it needs to be. I would like to make the code independent of processor speed.

```
GLfloat rot = 10;
GLfloat angle = 0, angle2 = 0, angle3 = 0;

if (angle < rot){
   glRotated(angle, 0, 1,0);                                  
   angle++;
}
else{
   if (angle2 < rot * 2 ){
     glRotated(rot - angle2, 0, 1, 0);      
     angle2++;
    }

else{
    if (angle3 < rot ){
     glRotated(-rot + angle3, 0, 1, 0);     
     angle3++;
    }

else{ angle = 0, angle2 = 0, angle3 = 0; }

```

**Tags:** opengl, transformations


---

## 1 answers

### Answer by username_1 on 2018-03-03. Score: 1

In order to dissociate the rotation from the CPU speed, you need to use some sort of timing in your function. I recommend passing in a time value of some sort. The easiest thing is to pass in the number of seconds since the app started. How you do that depends on your particular OS, which you didn't specify.

Once you have a timing constant, you can generate a triangle wave to control the rotation. This formula will oscillate between 0 and 1 over time:

```
double rotation = 2.0 * fabs(fmod(time, 1.0) - 0.5);

```

You can make it go from 0 to 10 by multiplying the result by 10:

```
rotation *= 10.0;

```

Then you plug that into your `glRotated()` call:

```
glRotated(rotation, 0, 1, 0);

```

If you want to make the motion a little smoother, you could replace the triangle wave with a sine wave, like this:

```
double rotation = 0.5 * (sin(time * 2.0 * M_PI + M_PI / 2.0) + 1.0);

```

> **Comments:**
> **username_0**: Thanks. I don't yet know how to obtain the time. I am using Windows 10.  
> **username_1**: I haven't dealt with Windows in a few years, but [this question](https://stackoverflow.com/questions/3729169/how-can-i-get-the-windows-system-time-with-millisecond-resolution) on Stack Overflow seems like it might have some good ideas. At least there are lots of different answers so one of them will probably do the trick.  
> **username_2**: If you want high precision time in the win32 API you can use QueryPerformanceCounter and QueryPerformanceFrequency  
> **username_0**: Is it possible to do this using frame rate instead of time?  
> **username_1**: If you know the frame rate and know how many frames you've already processed, then you can calculate the time by multiplying the frame rate by the number of frames.  
> **username_2**: 1/FPS = time taken per frame, so yes you can use that. However to calculate FPS you would have needed to take a snapshot of some timer anyway ?  
> **username_0**: Thanks. I think the question I am really asking is the following: What I want to do is make sure the speed of my motion is the same regardless of whether I oscillate between 0 and 1 or between 0 and 10. In the response, username_1 says "You can make it go from 0 to 10 by multiplying the result by 10:..", but this seems to increase the speed of my motion. How do I maintain speed?  
> **username_1**: When you multiply the rotation by 10 you're changing the amplitude of the rotation, but not the frequency. So it will rotate 10 degrees per second instead of 1 degree per second. If you want to also change the frequency to match, you should divide time by 10. Then you'll get 10 degrees per 10 seconds, which is still 1 degree per second.  

---

