This page has been translated automatically.
Unigine Basics
1. Introduction
2. Managing Virtual Worlds
3. Preparing 3D Models
4. Materials
5. Cameras and Lighting
7. Making Cutscenes and Recording Videos
8. Preparing Your Project for Release
9. Physics
10. Optimization Basics
11. PROJECT2: First-Person Shooter
12. PROJECT3: Third-Person Cross-Country Arcade Racing Game
13. PROJECT4: VR Application With Simple Interaction

Working With Time (IFps)

Brief overview of the frame duration and separating the application logic from FPS. Making the fan rotation created via the Fan component smooth.

The application frame rate may vary (i.e. the Update() method will be called more or less frequently) depending on the hardware. If we want a specific action to be performed at a certain frequency regardless of the frame rate (e.g. an indicator blinks once per second, etc.), then we must untie the application logic from the FPS. To do this, we can use the scaling multiplier (time in seconds taken to process the last frame) returned by the following methods:

  • Engine.IFps returns the inverse of the FPS value for your application.
  • Game.IFps returns the scaled inverse FPS value. This class is to be used when you want to speed up, slow down, or pause rendering, physics, or game logic.

To adjust the transformations, you can use the following code:

Source code (C#)
private void Update()
{

	// getting an inverse FPS value (the time in seconds it took to complete the last frame)
	float ifps = Game.IFps;

	// moving the node up by 0.3 units every second instead of every frame
	node.WorldTranslate(new Vec3(0.0f, 0.0f, 0.3f * ifps));
}

To perform some changes once in a certain period of time you can use the following code:

Source code (C#)
const float INTERVAL_DURATION = 5;
float elapsed_time = INTERVAL_DURATION;

private void Update()
{

	// getting an inverse FPS value (the time in seconds it took to complete the last frame)
	float ifps = Game.IFps;

	// checking if it's time to make changes
	if (elapsed_time < 0.0f)
	{

		/* .. DO SOME CHANGES .. */

		// resetting elapsed time counter
		elapsed_time = INTERVAL_DURATION;
	}

	// decreasing elapsed time counter
	elapsed_time -= ifps;
}

Practice#

In our project the fan is still rotating at an indeterminate speed (by 10 degrees per frame, and frame time is an unstable value). Let's assume that we want to set the rotation speed in revolutions per second. This can be easily achieved by adding a multiplier into the existing code:

Source code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Fan : Component
{
	public Node fan_node = null;
	public float speed = 720.0f;
	private void Update()
	{
		// if the fan node is not assigned, do nothing
		if(!fan_node)
			return;
		// rotate the node with the specified speed
		fan_node.Rotate(0, speed ${#HL}$ * Game.IFps${HL#}$, 0);
	}
}

Now the blades rotate precisely at the specified speed regardless of the frame rate.

Last update: 2024-12-13
Build: ()