Arduboyで狙い撃つぜ!(atan2テーブルの作り方) その4

今回はM-KAI氏の書かれたものを参考に、atanテーブルのサイズを半分にして検索回数を減らすことにした。
http://web.archive.org/web/20021220053234/http://www02.geocities.co.jp:80/SiliconValley-Bay/8572/algobib/lv029.htm


atanテーブルのサイズは前回の半分、0〜45度を用意する。

# <64方向>
#     16
#     |
# 32--+-- 0(64)
#     |
#     48

import math

for angle in range(8):
    r = (angle + 0.5) / 64 * 2 * math.pi
    x = math.cos(r)
    y = math.sin(r)
    print(str(y / x) + ',')


0〜45度のデータがあれば、場合分けで360度の値を復元できるということなんですね。

#include <Arduboy2.h>

Arduboy2 arduboy;

#define BOSS_SHOT   10

typedef struct CharaData {
  float x, y;
  float vx, vy;
  int16_t life;
};

CharaData player;

CharaData boss;
CharaData bossShot[BOSS_SHOT];

uint16_t counter;

const float sinTbl[] PROGMEM = {
  0.0,
  0.0980171403295606,
  0.19509032201612825,
  0.29028467725446233,
  0.3826834323650898,
  0.47139673682599764,
  0.5555702330196022,
  0.6343932841636455,
  0.7071067811865475,
  0.7730104533627369,
  0.8314696123025451,
  0.8819212643483549,
  0.9238795325112867,
  0.9569403357322089,
  0.9807852804032304,
  0.9951847266721968,
};

float getSin(int16_t angle)
{
  float result = 1.f;
  uint8_t idx = angle & 0x1f;

  if (idx < 16) {
    result = pgm_read_float(sinTbl + idx);
  } else if (idx > 16) {
    result = pgm_read_float(sinTbl + 32 - idx);
  }

  return (angle & 0x3f) > 32 ? -result : result;
}

float getCos(int16_t angle)
{
  return getSin(16 + angle);
}

// 64方向の1/8のデータ
const float atanTbl[] PROGMEM = {
  0.049126849769467254,
  0.14833598753834742,
  0.25048696019130545,
  0.3578057213145241,
  0.4729647758913199,
  0.5993769336819237,
  0.7416505462720353,
  0.9063471690191469,
};

int16_t getAtan2(float y, float x)
{
  int16_t idx = 16; // 90度
  if (x == 0.f && y == 0.f) return idx; // 同じ位置の時は90度とみなす

  float ax = abs(x);
  float ay = abs(y);

  if (ax > ay) {
    // 比を求める
    float ratio = ay / ax;
    // 求めた比に近い値をテーブルから探す
    for (idx = 0; idx < 8; ++idx) {
      if (ratio < pgm_read_float(atanTbl + idx)) break;
    }
  } else {
    float ratio = ax / ay;
    for (idx = 16; idx > 8; --idx) {
      if (ratio < pgm_read_float(atanTbl + 16 - idx)) break;
    }
  }

  // idxは第1象限の値なので、実際の象限の値に調整する
  if (x < 0) idx = 32 - idx;
  if (y < 0) idx = 64 - idx;

  return idx;
}

void setup()
{
  arduboy.begin();
  arduboy.setFrameRate(60);
  arduboy.clear();

  player.x = 20;
  player.y = 64 / 2;

  boss.x = 128 / 2;
  boss.y = 64 / 2;
}

void loop()
{
  if (!arduboy.nextFrame()) return;
  arduboy.clear();

  if (arduboy.pressed(LEFT_BUTTON)) {
    player.x -= 1.f;
  } else if (arduboy.pressed(RIGHT_BUTTON)) {
    player.x += 1.f;
  }
  if (arduboy.pressed(UP_BUTTON)) {
    player.y -= 1.f;
  } else if (arduboy.pressed(DOWN_BUTTON)) {
    player.y += 1.f;
  }
  arduboy.fillRect(player.x - 8.f, player.y - 4.f, 16, 8);

  arduboy.drawTriangle(boss.x, boss.y - 8.f, boss.x - 8.f, boss.y + 8.f, boss.x + 8.f, boss.y + 8.f);

  if (++counter % 10 == 0) {
    for (int8_t i = 0; i < BOSS_SHOT; ++i) {
      CharaData *p = &bossShot[i];
      if (p->life == 0) {
        p->life = 1;
        p->x = boss.x;
        p->y = boss.y + 2.f;

        // プレイヤの方向を求める
        int16_t r = getAtan2(player.y - p->y, player.x - p->x);

        // 弾の移動方向を求める
        p->vx = getCos(r);
        p->vy = getSin(r);

        break;
      }
    }
  }

  for (int8_t i = 0; i < BOSS_SHOT; ++i) {
    CharaData *p = &bossShot[i];
    if (p->life > 0) {
      p->x += p->vx;
      p->y += p->vy;

      if (p->x < 0.f || 128.f < p->x || p->y < 0.f || 64.f < p->y) {
        p->life = 0;
      }

      arduboy.fillCircle(p->x, p->y, 2);
    }
  }

  arduboy.display();
}