Detecting Collision 衝突判定
A human can tell if two figures overlap or not at a glance. But it is difficult to solve this generally with a computer, and different problems often require different tricks.
2つの図形が重なっているかどうかは人間ならば一目でわかります。ところがこれをコンピュータで一般的に解くのは難しく、問題によって様々なトリックが必要になります。
Various methods of checking whether figures overlap is used to detect collision between objects in physics simulations and games. You may rely on the functions of existing tools and libraries in actual projects. But knowing the basics will not only help you understand these tools, but will also enable you to quickly implement only the functions you need yourself.
図形が重なっているかを調べる様々な手法は物理シミュレーションやゲームなどで物体同士の衝突判定に使われます。実際のプロジェクトでは既存のツールの機能やライブラリなどに頼ることも多いと思いますが、基本的なことを知っておくと、これらのツールを理解する役にたつだけでなく、自分で必要な機能だけを素早く実装できるようになります。
Circles
円
One of the simplest is the circle-point collision detection. Since all points on the edge of a circle are at the distance of the radius from its center, checking if a certain point is inside a circle is just to check the distance between the point and the center of the circle.
Two circles are overlapping if the distance between their centers is equal to or less than the sum of their radii.
最も簡単なものの1つは円と点の衝突判定です。円は境界線上の全ての点が中心から半径の距離にあるので、ある点が円の内側にあるかどうかはその点と中心の距離を調べるだけで判定できます。
2つの円は、中心どうしの距離がそれぞれの半径の和に等しいか小さければ重なっていることになります。
The distance between two points can be obtained by the following equation.
2点間の距離は下記の式で求められます。
Line and circle
直線と円
To detect a collision between a circle and a straight line, we can find the distance between the center of the circle and the straight line and check whether it is equal to or less than the radius.
円と直線の衝突を検出するには、円の中心と直線との距離を求め、それが半径以下かどうかを調べれば良いでしょう。
The distance between the center of a circle and a line is the length of a perpendicular line drawn from the center down to the line.
円の中心と直線の距離は、中心から直線に下ろした垂線の長さになります。
The distance between the center of a circle and a line is the length of a perpendicular line drawn from the center down to the line. There can be different ways to find this length, but in this example, let’s form a right triangle by placing a couple of arbitrary points A and B on the line. If we call the center of the circle P, the length of the distance we want can be expressed by the following equation.
円の中心と直線の距離は、中心から直線に下ろした垂線の長さになります。この長さを求める方法は色々考えられますが、ここでは直線の上に適当に点 AとB を打って直角三角形を作ります。円の中心をPとすると求める距離は下記の式で表せます。
AP and AB are vectors and the angle between them is θ. The equation is transformed using a two-dimensional version of cross product.
APとABはベクトル、その間の角度がθです。式の変形には二次元版のクロス積を使いました。
Rectangles
長方形
Collisions of rectangles with sides parallel to the x and y-axes can be determined by comparing the coordinates of the left, right, top, and bottom.
辺がx軸、y軸に並行な長方形の衝突は左端、右端、上端、下端の座標を比べれば判定できます。
Convex polygons
凸多角形
A convex polygon is a polygon that has no concavities, or to be more precise, a polygon such that the line segment connecting two points within the polygon is always contained within the polygon.
凸多角形とは凹んだところがない多角形、もう少し厳密にいうとその多角形の中の2点を結ぶ線分が必ずその多角形の内部に含まれるような多角形です。
Similarly to rectangles, collision between convex polygons can be determined by examining the overlap using vectors perpendicular to each side of the polygon as axes.
凸多角形の間の衝突は、多角形の各辺に垂直なベクトルを軸として長方形の時と同様に重なりを調べることで判定できます。
-
Let one of the polygons be and the other
どちらかの図形を、もう一方をとする -
Select one edge of and let a vector perpendicular to that edge be the axis.
の一辺を選びその辺に垂直なベクトルを軸とする -
Project all the vertices of onto (calculate the dot product), and let be the minimum value and be the maximum value.
の全ての頂点をに投影(内積を計算)し、最小の値を、最大の値をとする -
Project all the vertices of B onto (calculate the dot product), and let be the minimum value and be the maximum value.
Bの全ての頂点をに投影(内積を計算)し、最小の値を最大の値をとする -
If , then A and B overlap on the axis
であればAとBは軸上で重なっている -
Repeat 2-5 for all edges of . If and overlap for all axes, then and are in collision.
Aの全ての辺に対し2-5を繰り返し、とが全ての軸に対して重なっていればとは衝突している
Though only triangles are used in the example above, this method can be applied to any convex polygon. The collision detection of a concave polygon can be converted into a combination of convex polygons by dividing them with straight lines.
ここでは三角形を例に取りましたがこの手法は凸多角形であれば何角形でも適応できます。凹多角形の衝突判定は直線で分割することで凸多角形の組み合わせに変換することができます。

