「AABB」の版間の差分
imported>Administrator 編集の要約なし |
imported>Administrator 編集の要約なし |
||
| 6行目: | 6行目: | ||
[[物理演算エンジン]]での衝突判定ではクソ真面目に計算していることが多いため、そのような場合の高速化には表示用のモデルとは別に見えないローポリゴンモデルを被せるように用意して物理演算エンジンに喰わせることもある。これはAABBとは別の技術だが、実のところ物理演算エンジン内部でも物凄く小さな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(MathHelper.Min(from.X, to.X), MathHelper.Min(from.Y, to.Y), MathHelper.Min(from.Z, to.Z)); | |||
this.To = new Vector3(MathHelper.Max(from.X, to.X), MathHelper.Max(from.Y, to.Y), MathHelper.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> | |||
== 関連項目 == | == 関連項目 == | ||