「AABB」の版間の差分
imported>Administrator (ページの作成:「'''AABB''' (語源:Axis Aligned Bounding Box)とは、「軸に平行な直方体」という意味であり、3Dゲームなどで衝突判定に用いる見えな...」) |
imported>Administrator |
||
(2人の利用者による、間の5版が非表示) | |||
1行目: | 1行目: | ||
'''AABB''' (語源:Axis Aligned Bounding Box)とは、「軸に平行な直方体」という意味であり、3Dゲームなどで[[衝突判定]]に用いる見えない四角形の領域を指す。日本語では「豆腐」などとも呼ばれる。 | '''AABB''' (語源:Axis Aligned Bounding Box)とは、「軸に平行な直方体」という意味であり、3Dゲームなどで[[衝突判定]]に用いる見えない四角形の領域を指す。日本語では「豆腐」などとも呼ばれる。 | ||
− | + | 3Dモデルの衝突判定を[[レイキャスト]]などを使ってクソ真面目に[[ポリゴン]]単位で行うと非現実的なくらい物凄い負荷になる。そこでモデルを覆うような「常にXYZ軸に平行」な立方体)を動的に作り出し、AABBどうしで当たり判定を行えば、いわゆる2DなアクションゲームのX軸Y軸の衝突判定処理にZ軸の処理を加えるだけでよく非常に軽い処理となる。[[モデル]]単位のAABBでは大雑把すぎるという場合は[[ボーン]]単位などでAABBを用意して衝突判定を行うという手法が使われる。 | |
ゲーム以外の3Dアプリでも「[[マウス]]でモデルを掴むように動かす操作」を実現するためにAABBを利用しているのがほとんどである。 | ゲーム以外の3Dアプリでも「[[マウス]]でモデルを掴むように動かす操作」を実現するためにAABBを利用しているのがほとんどである。 | ||
− | [[物理演算エンジン]] | + | [[物理演算エンジン]]での衝突判定では[[レイキャスト]]などを使ってクソ真面目に計算していることが多いため、そのような場合の高速化には表示用のモデルとは別に見えないローポリゴンモデルを被せるように用意して物理演算エンジンに喰わせることもある。これはAABBとは別の技術だが、実のところ物理演算エンジン内部でも物凄く小さなAABBを大量に生成して衝突判定を行っている実装も多い。 |
+ | |||
+ | == 実装例 == | ||
+ | [[MonoGame]]での実装例。[[デバッグ]]はしていない。 | ||
+ | <source lang="csharp"> | ||
+ | using System; | ||
+ | using Microsoft.Xna.Framework; | ||
+ | |||
+ | // FromとToを結ぶ立方体 | ||
+ | public struct AABB | ||
+ | { | ||
+ | public Vector3 From; | ||
+ | public Vector3 To; | ||
+ | |||
+ | public Vector3 Center | ||
+ | { | ||
+ | get | ||
+ | { | ||
+ | return this.From + this.Harf; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public Vector3 Harf | ||
+ | { | ||
+ | get | ||
+ | { | ||
+ | return this.Width * 0.5f; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public Vector3 Width { | ||
+ | get | ||
+ | { | ||
+ | return this.To - this.From; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public AABB(Vector3 from, Vector3 to) | ||
+ | { | ||
+ | this.From = new Vector3(Math.Min(from.X, to.X), Math.Min(from.Y, to.Y), Math.Min(from.Z, to.Z)); | ||
+ | this.To = new Vector3(Math.Max(from.X, to.X), Math.Max(from.Y, to.Y), Math.Max(from.Z, to.Z)); | ||
+ | } | ||
+ | |||
+ | public bool Contains(AABB target) | ||
+ | { | ||
+ | if ((this.Harf.X + target.Harf.X) < Math.Abs(this.Center.X - target.Center.X)) return false; | ||
+ | if ((this.Harf.Y + target.Harf.Y) < Math.Abs(this.Center.Y - target.Center.Y)) return false; | ||
+ | if ((this.Harf.Z + target.Harf.Z) < Math.Abs(this.Center.Z - target.Center.Z)) return false; | ||
+ | |||
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
== 関連項目 == | == 関連項目 == | ||
12行目: | 64行目: | ||
[[category: 3DCG]] | [[category: 3DCG]] | ||
+ | [[category: 衝突判定]] |
2017年11月13日 (月) 03:04時点における最新版
AABB (語源:Axis Aligned Bounding Box)とは、「軸に平行な直方体」という意味であり、3Dゲームなどで衝突判定に用いる見えない四角形の領域を指す。日本語では「豆腐」などとも呼ばれる。
3Dモデルの衝突判定をレイキャストなどを使ってクソ真面目にポリゴン単位で行うと非現実的なくらい物凄い負荷になる。そこでモデルを覆うような「常にXYZ軸に平行」な立方体)を動的に作り出し、AABBどうしで当たり判定を行えば、いわゆる2DなアクションゲームのX軸Y軸の衝突判定処理にZ軸の処理を加えるだけでよく非常に軽い処理となる。モデル単位のAABBでは大雑把すぎるという場合はボーン単位などでAABBを用意して衝突判定を行うという手法が使われる。
ゲーム以外の3Dアプリでも「マウスでモデルを掴むように動かす操作」を実現するためにAABBを利用しているのがほとんどである。
物理演算エンジンでの衝突判定ではレイキャストなどを使ってクソ真面目に計算していることが多いため、そのような場合の高速化には表示用のモデルとは別に見えないローポリゴンモデルを被せるように用意して物理演算エンジンに喰わせることもある。これはAABBとは別の技術だが、実のところ物理演算エンジン内部でも物凄く小さなAABBを大量に生成して衝突判定を行っている実装も多い。
実装例編集
using System;
using Microsoft.Xna.Framework;
// FromとToを結ぶ立方体
public struct AABB
{
public Vector3 From;
public Vector3 To;
public Vector3 Center
{
get
{
return this.From + this.Harf;
}
}
public Vector3 Harf
{
get
{
return this.Width * 0.5f;
}
}
public Vector3 Width {
get
{
return this.To - this.From;
}
}
public AABB(Vector3 from, Vector3 to)
{
this.From = new Vector3(Math.Min(from.X, to.X), Math.Min(from.Y, to.Y), Math.Min(from.Z, to.Z));
this.To = new Vector3(Math.Max(from.X, to.X), Math.Max(from.Y, to.Y), Math.Max(from.Z, to.Z));
}
public bool Contains(AABB target)
{
if ((this.Harf.X + target.Harf.X) < Math.Abs(this.Center.X - target.Center.X)) return false;
if ((this.Harf.Y + target.Harf.Y) < Math.Abs(this.Center.Y - target.Center.Y)) return false;
if ((this.Harf.Z + target.Harf.Z) < Math.Abs(this.Center.Z - target.Center.Z)) return false;
return true;
}
}