event.deltaTime

类型 数字
事件 陀螺仪
修改 2024.3703 版
关键字 陀螺仪、deltaTime
另请参阅 system.setGyroscopeInterval()

概述

自上次陀螺仪事件以来的以秒为单位的时间。

陷阱

陀螺仪测量不会在 system.setGyroscopeInterval() 函数指定的确切时间记录。也就是说,你不能假设已配置的 10 Hz(即 100 毫秒)间隔会在正好相隔 100 毫秒时生成测量。这些测量可能会在稍晚一些时间记录。这是 iOS 和 Android 操作系统的限制。为了补偿这一问题,我们提供了 event.deltaTime 来帮助让你的旋转计算尽可能准确。

你可以使用 event.deltaTime 来通过下面的方程计算自从上次陀螺仪测量以来经过的大概旋转距离

增量弧度 = 旋转速率 * 增量时间

请注意这是一个近似值,因为自上次陀螺仪测量以来设备不太可能以恒定速度旋转。这意味着按时间累加的距离误差会逐渐增大。通过 system.setGyroscopeInterval() 函数增大测量更新间隔将有助于减少该误差。

示例

-- Called when a new gyroscope measurement has been received
local function onGyroscopeDataReceived( event )
    -- Calculate approximate rotation traveled via delta time
    -- Remember that rotation rate is in radians per second
    local deltaRadians = event.zRotation * event.deltaTime
    local deltaDegrees = deltaRadians * (180/math.pi)
end
 
-- Set up the above function to receive gyroscope events if the sensor exists
if system.hasEventSource( "gyroscope" ) then
    Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived )
end