cocosで方向を取るときのtips
2点間の方向を取るときはこんな感じ
※math.hを読み込む必要があります。
/* * 現在地と目的地からDirectionを求めます。 */ int HogeView::calcDirection(cocos2d::Point from, cocos2d::Point dist) { auto x = dist.x - from.x; auto y = dist.y - from.y; int angle = atan2(y, x) / M_PI * 180; // 第3象限, 第4象限に対して if (y < 0 && x < 0) { angle = 360 + angle; } return angle / 90; }
1|0
ーーー◯ーーー
2|3
distが◯から返り値の方向に有ることがわかる。