不同系统上的浮点和数学精度
我想在项目中实现游戏录制功能,该功能只会在关卡开始时记录玩家输入和 RNG 种子。然后我可以把这样的唱片放在我的电脑上播放,以测试它的有效性。
我只关心不同 Flash Player 版本、操作系统或 CPU(或任何可能受影响的其他内容)之间可能出现的一些数字差异。该项目将为 Flash Player 10.0.0+ 编写。我关心什么:
- 数字运算:乘法、除法;位运算(也可能是位移位);加法和减法;模
- 数学类:sin、cos 和 atan2; 进行舍入
- 通过旋转和缩放对 localToGlobal/globalToLocal
我不会使用 hitTest、getObjectsUnderPoint、hitTestPoint、getBounds 等内容,所有碰撞都是几何碰撞。
那么,使用上面提到的任何东西是否有可能在不同的系统上产生不同的结果?如果是这样,我该怎么做才能避免它们?
I want to implement a gameplay recording feature in a project, which would only record player input and seed of the RNG at the beginning of the level. Then I could take such record and play it on my computer in order to test it for validity.
I'm only concerned with some numerical differences which might appear between different Flash Player version, Operating Systems or CPUs (or whatever else that might be affected). The project would be written for Flash Player 10.0.0+. What stuff I am concerned with:
- Operations on Numbers: Multiplying, dividing; bit operations (possibly bit shifting too); addition and subtraction; modulo
- Math class: sin, cos and atan2; rounding
- localToGlobal/globalToLocal with rotations and scaling
I won't be using stuff like hitTest, getObjectsUnderPoint, hitTestPoint, getBounds and so on, all collisions will be geometrical.
So, are there any chances that using any of the pointed things above will yield different results on different systems? If so, what can I do to avoid them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个有趣的问题......
这不是一个“这个游戏在多个平台上玩相同的游戏吗”,而是“用户输入的记录在模拟时会产生完全相同的输出”的问题。
我的直觉会说“别担心,闪存虚拟机会抽象掉差异”,但随着我的思考更多,有些领域可能会出现问题。
首先,我不会记录任何基于时间的内容。用户在 1.21 秒时敲击按键可能很难预测该情况是发生在一帧的计算之前还是之后,特别是在录制或播放计算机处于负载状态时。尝试使用用户输入来调整补间时间可能会失败。
浮点精度应该没问题。定义何时舍入的算法在 IEEE-754 中有详细记录,并且所有 VM 都使用 64 位数字,无论它们运行在什么操作系统上。我猜数学运算同样可以理解。
我认为避免 hitTest 之类的东西是很好的。我想理论上它们可能会受到是否使用硬件加速的影响。但我不是这方面的专家,所以也许不是。
现在 localToGlobal/globalToLocal...我只是不知道。他们可能存在理论上的硬件加速问题,但我倾向于对此表示怀疑。
所以我想我没有给出任何真正的答案。
That's an interesting question...
It's not a "will this game play the same on multiple platforms", it's "will a recording of user inputs produce the exact same output when simulated" question.
My gut would say "don't worry about it the flash VM abstracts the differences away", but then as I think more, there are some areas that might be a problem.
First, I wouldn't record anything time-based. A user hitting a key at 1.21 seconds in might be tough to predict whether that happens before or after a frame's worth of computation, especially if either the recording or playback computer was under load. Trying to time tweens with user input is probably a recipe for failure.
Accuracy of floating point should be ok. The algorithms that define when to round are well documented in IEEE-754, and all VM's use 64 bit Numbers regardless of OS they're running on. I'm guessing the math operations are equally understood.
I think it's good to avoid hitTest and whatnot. I imagine they theoretically could be influenced by whether or not hardware acceleration is being used. But I'm not an expert there, so maybe not.
Now localToGlobal/globalToLocal... I just don't know. They might have that theoretical hardware acceleration problem, but I tend to doubt it.
So I guess I didn't give any real answers.
三角函数将不起作用!您必须创建以下各项的自定义实现:acos、asin、atan、atan2、cos、exp、log、pow、sin 和 sqrt。显然,随机()。
我仍在测试 Number 类。我不能确定是否加/减/等。在每台机器上都将保持一致。
Trig functions WILL NOT WORK! You must create custom implementations of the following: acos, asin, atan, atan2, cos, exp, log, pow, sin, and sqrt. And obviously, random().
I'm still in the process of testing the Number class. I can't say for sure whether additon/subtraction/etc. will be consistent on every machine.
在不同的计算机上,事情的表现方式不太可能(尽管有可能)明显不同。即使他们这样做了,这也将是一个非常罕见的事件,我不建议担心,除非它对游戏玩法绝对至关重要。
It is very unlikely (although possible) that things will behave in a noticeably different way on different computers. Even if they did, it would be a very rare event and not something I would recommend worrying about unless it is absolutely crucial to gameplay.