This page has been translated automatically.
UNIGINE 基础课程
1. 简介
2. 虚拟世界管理
3. 3D模型准备
4. 材质
5. 摄像机和光照系统
7. 制作过场动画与动画序列
8. 准备发布项目
9. 物理系统
10. 优化基础
11. 项目2:第一人称射击游戏
12. PROJECT3: Third-Person Cross-Country Arcade Racing Game
13. PROJECT4: VR Application With Simple Interaction

时间控制(IFps)

Brief overview of the frame duration and separating the application logic from FPS. Making the fan rotation created via the Fan component smooth.简要说明帧时长以及如何将应用逻辑与帧率分离。使通过Fan组件创建的风扇旋转更加平滑。

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:应用帧率可能会因硬件不同而变化(即Update()方法的调用频率会有所不同)。如果我们希望特定动作以固定频率执行而不受帧率影响(例如指示灯每秒闪烁一次等),就必须将应用逻辑与帧率解耦。为此,我们可以使用以下方法返回的缩放乘数(处理上一帧所用的秒数):

  • Engine::getIFps() returns the inverse of the FPS value for your application.Engine::getIFps() 返回应用帧率的倒数
  • Game::getIFps() 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.Game::getIFps() 返回经过缩放的帧率倒数。当需要加速、减速或暂停渲染、物理或游戏逻辑时使用此类

To adjust the transformations, you can use the following code:调整变换可以使用以下代码:

源代码 (C++)
int AppWorldLogic::update() {
	
	// 获取帧率倒数(上一帧完成所需秒数)
	float ifps = Game::getIFps();
	
	// 使节点以每秒0.3个单位的速度上移(而非每帧)
	node->worldTranslate(Math::Vec3(0.0f, 0.0f, 0.3f * ifps));

	return 1;
}

To perform some changes once in a certain period of time you can use the following code:要实现周期性变化可以使用以下代码:

源代码 (C++)
#include <UnigineGame.h>

// 将Unigine命名空间注入全局命名空间
using namespace Unigine;
// AppWorldLogic.cpp

	const float INTERVAL_DURATION = 5;		// 间隔时长(秒)
	float elapsed_time = INTERVAL_DURATION;	// 距离下次变更的剩余时间
	
/* .. */

int AppWorldLogic::update() {
	
	// 获取帧率倒数(上一帧完成所需秒数)
	float ifps = Game::getIFps();

	// 检查是否到达变更时间
	if (elapsed_time < 0.0f)
	{
		
		/* .. 执行变更 .. */

		// 重置计时器
		elapsed_time = INTERVAL_DURATION;
	}

	// 递减计时器
	elapsed_time -= ifps;
	
	return 1;
}
/* .. */

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:当前项目中风扇仍以不确定速度旋转(每帧10度,而帧时间不稳定)。假设我们需要设置每秒转数,只需在现有代码中添加乘数即可实现:

Fan.cpp
#include "Fan.h"
${#HL}$ #include <UnigineGame.h>${HL#}$
// 注册Fan组件
REGISTER_COMPONENT(Fan);

// 导入必要的命名空间
using namespace Unigine;
using namespace Math;

// 组件每帧调用的更新方法
void Fan::update()
{
	// 如果风扇节点未指定,则不执行任何操作
	if (!fan_node)
		return;
	// 按照指定速度旋转节点
	fan_node->rotate(0, speed ${#HL}$ * Game::getIFps() ${HL#}$ , 0);
}

Now the blades rotate precisely at the specified speed regardless of the frame rate.现在扇叶将精确按照指定速度旋转,不受帧率影响。

本页面上的信息适用于 UNIGINE 2.20 SDK.

最新更新: 2025-06-09
Build: ()