【发布时间】:2016-04-28 12:28:51
【问题描述】:
我正在计算速度,它工作正常。但是我怎样才能找到速度变量的平均值呢?速度值大约每秒变化一次(当 GPS 值更新时)。当他(她)按下“完成”按钮时,我需要显示用户平均速度
感谢您的帮助
这是我的示例代码:
void Update()
{
timer += Time.deltaTime;
if (lonA != Input.location.lastData.longitude || latA != Input.location.lastData.latitude)
{
CalculateDistances(lonA, latA, Input.location.lastData.longitude, Input.location.lastData.latitude); // last distance and overall distanceS
lonA = Input.location.lastData.longitude;
latA = Input.location.lastData.latitude;
lastTime = timer;
timer = 0;
speed0 = speed;
CalculateSpeed();
}
}
public static float Radians(float x)
{
return x * Mathf.PI / 180;
}
public void CalculateDistances(float firstLon, float firstLat, float secondLon, float secondLat)
{
float dlon = Radians(secondLon - firstLon);
float dlat = Radians(secondLat - firstLat);
float distance = Mathf.Pow(Mathf.Sin(dlat / 2), 2) + Mathf.Cos(Radians(firstLat)) * Mathf.Cos(Radians(secondLat)) * Mathf.Pow(Mathf.Sin(dlon / 2), 2);
float c = 2 * Mathf.Atan2(Mathf.Sqrt(distance), Mathf.Sqrt(1 - distance));
lastDistance = 6371 * c * 1000;
}
void CalculateSpeed()
{
speed = lastDistance / lastTime * 3.6f;
speedText.text = Mathf.RoundToInt(speed).ToString();
}
【问题讨论】:
-
如果您知道他们总共花了多长时间以及他们走了多远,您可以使用这些来找到它,而不是使用实际的速度变量
-
将overallDistance除以overallTime?
-
宾果游戏,距离/时间 = 旅途的平均速度
-
感谢 Alfie Goodacre