0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

回転する矩形と点の衝突判定【備忘録】

Posted at

シンプルな実装

変数

float box_x; // 矩形のX座標
float box_y; // 矩形のY座標
float box_width; // 矩形の幅
float box_height; // 矩形の高さ
float box_angle; // 矩形の回転角度(180° = π)
float point_x; // 点のX座標
float point_y; // 点のY座標

判定計算

float s = -sinf(box_angle);
float c = cosf(box_angle);
float dx = point_x - box_x;
float dy = point_y - box_y;
float xx = dx * c - dy * s;
float yy = dx * s + dy * c;
bool result = (0 <= xx && xx < box_width && 0 <= yy && yy < box_height);

アンカーやサイズを含む実装

変数

float box_x; // 矩形のX座標
float box_y; // 矩形のY座標
float box_width; // 矩形の幅
float box_height; // 矩形の高さ
float box_angle; // 矩形の回転角度(180° = π)
float box_anchor_x; // 矩形のアンカー(回転の基点)X座標
float box_anchor_y; // 矩形のアンカー(回転の基点)Y座標
float box_size; // 矩形のサイズ(100% = 1.0)
float point_x; // 点のX座標
float point_y; // 点のY座標

判定計算

float s = -sinf(box_angle);
float c = cosf(box_angle);
float dx = point_x - box_x;
float dy = point_y - box_y;
float xx = dx * c - dy * s + box_anchor_x * box_size;
float yy = dx * s + dy * c + box_anchor_y * box_size;
bool result = (0 <= xx && xx < (box_width * box_size) && 0 <= yy && yy < (box_height * box_size));
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?