- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我找到了一个函数来获取自 Mac 启动以来的毫秒数:
U32 Platform::getRealMilliseconds()
{
// Duration is a S32 value.
// if negative, it is in microseconds.
// if positive, it is in milliseconds.
Duration durTime = AbsoluteToDuration(UpTime());
U32 ret;
if( durTime < 0 )
ret = durTime / -1000;
else
ret = durTime;
return ret;
}
问题是大约 20 天后 AbsoluteToDuration 一直返回 INT_MAX,直到 Mac 重新启动。
我尝试使用下面的方法,它有效,但看起来 gettimeofday 需要更多时间并且稍微减慢游戏速度:
timeval tim;
gettimeofday(&tim, NULL);
U32 ret = ((tim.tv_sec) * 1000 + tim.tv_usec/1000.0) + 0.5;
是否有更好的方法来获取自某个纪元(最好是自应用程序启动以来)以来经过的毫秒数?
谢谢!
最佳答案
您真正的问题是您试图将正常运行时间(以毫秒为单位)值放入 32 位整数中。如果您这样做,您的值(value)将始终在 49 天或更短时间内回到零(或饱和),无论您如何获得该值(value)。
一种可能的解决方案是使用 64 位整数来跟踪时间值;这样清算的日子就会推迟几百年,这样你就不用担心这个问题了。这是它的 MacOS/X 实现:
uint64_t GetTimeInMillisecondsSinceBoot()
{
return UnsignedWideToUInt64(AbsoluteToNanoseconds(UpTime()))/1000000;
}
... 或者如果您不想返回 64 位时间值,次优的做法是在程序启动时记录当前的毫秒值,然后始终减去该值从你返回的值。这样,在您自己的程序运行至少 49 天之前,事情不会中断,我认为这对于游戏来说不太可能。
uint32_t GetTimeInMillisecondsSinceProgramStart()
{
static uint64_t _firstTimeMillis = GetTimeInMillisecondsSinceBoot();
uint64_t nowMillis = GetTimeInMillisecondsSinceBoot();
return (uint32_t) (nowMillis-_firstTimeMillis);
}
关于c++ - 在 Mac OS 上测量 deltatime 毫秒数的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10854118/
我的 deltaTIme 遇到了麻烦,基本上我想做的就是每秒向变量添加 1。 private int num=0 private float deltaTime=Gdx.graphics.getDel
我试图将一个简单的矩形从当前位置移动到 Canvas 上单击的位置。当我提供恒定速度时,矩形就会出现并且移动得很好。但是当我将其乘以增量时间时,矩形不再出现。我使用 requestAnimationF
我想创建一个基于速度移动的 2d 游戏。正如目前的代码一样,我正在使用增量时间来使帧速率之间的速度变化保持一致。然而,在跳跃时,在 144hz 显示器上运行时,每次跳跃的跳跃高度略有不同(最多约
我是 Unity 的新手,我多次看到需要添加 Time.deltaTime。什么情况下应该加?我知道这是为了在框架计算机快速刷新时不会有多余的电量。 例如,在下一种情况下,我是否需要添加Time.de
我正在尝试使用“阻尼”属性逐渐降低物体的速度。之后,我想使用速度移动对象的位置。它看起来像这样: velocity.x *= velocity.damping; velocity.y *= veloc
我正在构建一个 HTML5 Canvas 游戏,它使用 requestAnimationFrame 来渲染 Canvas 中的每一帧。要以基于秒而不是帧速率的速率移动动画,我正在构建一个类似于 Uni
我正在尝试移动一个对象,使其在视觉上以相同的速度移动,而不管帧速率如何波动。根据我的理解,我应该使用 Time.deltaTime 来确定在每次 Update 迭代期间移动多远。我的更新代码是这样的:
在我的游戏中,我想每隔几秒调用一个函数,从数组中创建随机对象。目前我正在像这样通过 SKAction 移动这些对象: func createRandmoObject() { let rando
关于何时应该或不应该将数量乘以 Time.deltaTime ,似乎有很多令人困惑/矛盾的建议。所以我想知道, 与 Unity 物理系统有关,你应该什么时候乘以 Time.deltaTime ? 我知
我正在尝试创建一个 LSTM 来对数据序列进行分类。 我将使用的每个训练输入的数据结构是: [[ [deltaX,deltaY,deltaTime], [deltaX,deltaY,deltaTime
我想知道使用我的程序的 deltaTime 是否是个好主意(程序运行的当前时间与自游戏循环的最后一次迭代以来程序运行的时间之间的变化(以毫秒为单位) ) 来控制敌人的健康损失。 所以不要这样做: ..
之前在我的游戏主循环中,时间被管理在 60 FPS 和相应的 Delay 时间延迟。 Sprite 序列动画如下: if(++ciclos > 10){ siguienteSprite++;
所以我是虚幻引擎 4 的新手,我认为发布在他们网站上的文档是一个很好的起点。但是,我在编码部分的一些事情上苦苦挣扎(我有相当不错的 C++ 知识)。这是我遇到问题的代码摘录: void AFloati
TL,DR:在协程中使用 Time.deltaTime 作为 yield 的等待时间是否安全? 通常为了避免 Update() 内部不必要的逻辑以实现短暂的功能,我会改为运行协程。例如,在我的游戏中,
我正在尝试实现一种在游戏中创建计时器的方法。这是我当前的代码;我如何创建一个简单的计时器来检查 1 秒是否已经过去? public class Time { private static lo
我找到了一个函数来获取自 Mac 启动以来的毫秒数: U32 Platform::getRealMilliseconds() { // Duration is a S32 value. /
我程序的 fps 不正确。当我使用 RivaTuner 统计数据计算我的应用程序的 fps 时,它给出了例如 3000 fps。但是我的程序计算出一个完全不同的数字,比如 500。它一直在上下波动,而
我正在尝试计算 deltaTime 中经过的秒数,但我不确定该怎么做,因为我的 deltaTime 不断打印 0.0166 或 0.0167。 这是我的代码: override func update
下面是我想强调的差异的一个简单示例。 使用协程: public float repeatRate = 5f; void Start() { StartCoroutine("RepeatSome
所以我有这个小程序,我的播放器可以来回弹跳。他向左移动看起来像这样: if ((runright == false) && (birdyDead == false)) { float cur
我是一名优秀的程序员,十分优秀!