Doom 如何从现有的正弦查找表中确定其余弦查找表?

dca*_*ujo 3 c math kotlin

我正在努力理解 Chocolate Doom 如何分配其余弦表。注释解释说,为了计算余弦表,它在正弦表上进行了 PI/2 移位,但我不明白它是如何使用 C 完成的。

这是 Chocolate Doom 的原始源代码tables.htables.c

#define FINEANGLES 8192
const fixed_t finesine[10240] =
{
    25,75,125,175,226,276,326,376,
    ...
}
const fixed_t *finecosine = &finesine[FINEANGLES/4];
Run Code Online (Sandbox Code Playgroud)

这就是我试图在 Kotlin 中实现的目标

val fineSine: Array<Fixed_t> = arrayOf(25,75,125,175,226,276,326,376, ...)
val fineCosine: Array<Fixed_t> = arrayOf() // This is where I'm stuck
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 5

一个完整的圆是 2pi 弧度。所以要抵消 pi/2,你需要走四分之一的路。因此,如果您FINEANGLES在圆圈中有值,则可以偏移FINEANGLES/4.

但是为了访问需要的FINEANGLESfinecosine finesine至少需要FINEANGLES + FINEANGLES/4值(假设没有逻辑可以环绕)。

一个简化的例子:

#define FINEANGLES 4
float finesine[] = {0, 1, 0, -1, 0};
float *finecosine = &finesine[FINEANGLES/4];  // {1, 0, -1, 0}
Run Code Online (Sandbox Code Playgroud)

  • 啊我明白了!我现在明白了。“finesine”数组的长度为 10240,因此“8192 + (8192/4)”将等于 10240。因此 Doom 将访问索引 2048 处的“finesine”数组,并将引用传递给“finecosine”。非常感谢! (2认同)