ravi.joshi53's picture

Simple animation which moves objects in 2D

Recently I moved from OpenGL to OpenTK. I wanted to show animation on the textures. Basically I have the textures loaded, now I wanted to move them in a 2D plane. OpenGL has a very beautiful function glutTimerFunc, which can help me easily (A sample code can be found here). I am looking for such kind of function here.

Somebody please suggest me a way.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

In OpenTK, you have two options:

GameWindow example:

var total_time = 0.0;
var position = new Vector3();
var gw = new GameWindow();
gw.VSync = VSyncMode.Adaptive;
gw.RenderFrame += (sender, e) =>
    total_time += e.Time;
    // Move the object in a circle
    position += new Vector3(
    // Rest of your rendering code goes here
ravi.joshi53's picture

Thanks Fiddler,

I am using GameWindow and my application always runs in full screen mode. The events UpdateFrameand RenderFramelooks good to me. However, In glutTimerFunc, we can set the frequency of the timer, which regularly triggers the function in specified milliseconds. Indirectly this helps me to set the game speed (or animation speed). Can you please explain the same behavior with some sample code here using openTK ?

the Fiddler's picture

Yes, with GameWindow this is quite straightforward. The GameWindow.Run() takes two arguments:

  • The first specifies how often UpdateFrame is called in a second (i.e. update/logic fps)
  • The second specifies how often RenderFrame is called in a second (i.e. render fps)

Usually, you want a fixed UpdateFrame rate (e.g. 60 or 120 fps) and a variable RenderFrame rate (because your application will run on different GPUs, some more and some less powerful). You can enable VSync to limit RenderFrame rate to the monitor's refresh rate.

The following sample code shows how to set the timing for GameWindow:

var gw = new GameWindow();
gw.UpdateFrame += (sender, e) => { /* update objects */ };
gw.RenderFrame += (sender, e) => { /* render objects */ };
gw.Run(); // case 1: variable rate, UpdateFrame and RenderFrame events raised as fast as possible
gw.Run(60); // case 2 (recommended!): fixed UpdateFrame rate at 60 fps, variable RenderFrame rate
gw.Run(60, 30); // case 3: fixed UpdateFrame rate at 60 fps, fixed RenderFrame rate at 30 fps

When using a variable rate, use e.Time to get the elapsed time in seconds:

gw.UpdateFrame += (sender, e) =>
    var elapsed = e.Time; // seconds since last UpdateFrame event (variable)

When using a fixed rate, the elapsed time is constant:

gw.UpdateFrame += (sender, e) =>
    var elapsed = gw.TargetUpdatePeriod; // 0.01666666 seconds for gw.Run(60) (constant)

If you want to see the different approaches in practice, run Examples.exe -> OpenTK -> GameWindow States. Use the '[' and ']' keys to decrease / increase the UpdateFrame rate and '<' and '>' to decrease / increase the RenderFrame rate. Press 'V' to enable / disable vsync.

ravi.joshi53's picture

Cool.. I got it...

thanks a lot Fiddler.