球と球の衝突判定

Page creation date :

The page you are currently viewing does not support the selected display language.

3Dゲームでよく使われる衝突判定のひとつです。計算がとても単純で非常に高速に処理できます。モデルの形状が球に近いほど正確な判定が行えます。

今回はティーポットとボックスで判定していますが、Xファイルから読み込んだメッシュでもそのまま置き換えることが可能です。

操作はティーポットを「←」「→」「↑」「↓」キーで「X」「Z」方向に移動できます。二つの球が衝突すると「当たっています!」の文字が表示されます

球と球の衝突判定

今回のメインコードファイルを載せます。

MainSample.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MDXSample
{
    /// <summary>
    /// メインサンプルクラス
    /// </summary>
    public partial class MainSample : IDisposable
    {
        /// <summary>
        /// ティーポット
        /// </summary>
        private Mesh _teapot = null;

        /// <summary>
        /// ティーポット当たり判定球の半径
        /// </summary>
        private float _teapotRadius = 0.0f;

        /// <summary>
        /// ティーポットの当たり判定球実体化用
        /// </summary>
        private Mesh _teapotSphere = null;

        /// <summary>
        /// ティーポットの位置
        /// </summary>
        private Vector3 _teapotPosition = Vector3.Empty;


        /// <summary>
        /// ボックス
        /// </summary>
        private Mesh _box = null;

        /// <summary>
        /// ボックス当たり判定球の半径
        /// </summary>
        private float _boxRadius = 0.0f;

        /// <summary>
        /// ボックスの当たり判定球実体化用
        /// </summary>
        private Mesh _boxSphere = null;

        /// <summary>
        /// ボックスの位置
        /// </summary>
        private Vector3 _boxPosition = new Vector3(-3.0f, 0.0f, -4.0f);


        /// <summary>
        /// 当たり判定フラグ
        /// </summary>
        private bool _hitFlag = false;


        /// <summary>
        /// アプリケーションの初期化
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        /// <returns>全ての初期化がOKなら true, ひとつでも失敗したら false を返すようにする</returns>
        /// <remarks>
        /// false を返した場合は、自動的にアプリケーションが終了するようになっている
        /// </remarks>
        public bool InitializeApplication(MainForm topLevelForm)
        {
            // フォームの参照を保持
            this._form = topLevelForm;

            // 入力イベント作成
            this.CreateInputEvent(topLevelForm);

            try
            {
                // Direct3D デバイス作成
                this.CreateDevice(topLevelForm);

                // フォントの作成
                this.CreateFont();

                // XYZライン作成
                this.CreateXYZLine();
            }
            catch (DirectXException ex)
            {
                // 例外発生
                MessageBox.Show(ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }


            // カメラの位置セット
            this.SetCameraPosition(15.0f, 270.0f, 80.0f);


            // ティーポット作成
            this._teapot = Mesh.Teapot(this._device);

            // ティーポットの包括球を計算するために頂点バッファ取得
            using (VertexBuffer vb = this._teapot.VertexBuffer)
            {
                Vector3 center;

                // 頂点バッファをロックしてストリーム取得
                using (GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.None))
                {
                    // ティーポットの包括球を計算し、半径取得
                    this._teapotRadius = Geometry.ComputeBoundingSphere(vertexData,
                        this._teapot.NumberVertices, this._teapot.VertexFormat, out center);

                    vertexData.Close();
                }

                // 頂点ロック解除
                vb.Unlock();
            }

            // 見やすいように当たり判定球を作成
            this._teapotSphere = Mesh.Sphere(this._device, this._teapotRadius, 16, 16);


            // ボックス作成
            this._box = Mesh.Box(this._device, 2.0f, 2.0f, 2.0f);

            // ボックスの包括球を計算するために頂点バッファ取得
            using (VertexBuffer vb = this._box.VertexBuffer)
            {
                Vector3 center;

                // 頂点バッファをロックしてストリーム取得
                using (GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.None))
                {
                    // ボックスの包括球を計算し、半径取得
                    this._boxRadius = Geometry.ComputeBoundingSphere(vertexData,
                        this._box.NumberVertices, this._box.VertexFormat, out center);

                    vertexData.Close();
                }

                // 頂点ロック解除
                vb.Unlock();
            }

            // 見やすいように当たり判定球を作成
            this._boxSphere = Mesh.Sphere(this._device, this._boxRadius, 16, 16);


            // アルファブレンディング方法を設定
            this._device.RenderState.SourceBlend = Blend.SourceAlpha;
            this._device.RenderState.DestinationBlend = Blend.InvSourceAlpha;


            // ライトの設定
            this.SettingLight();

            return true;
        }

        /// <summary>
        /// デバイスがロストしたとき
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void device_DeviceLost(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// デバイスがリセットしたとき
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void device_DeviceReset(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 更新処理
        /// </summary>
        public void Update()
        {
            // アプリケーションの終了操作
            if (this._keys[(int)Keys.Escape])
            {
                this._form.Close();
                return;
            }

            // カメラの設定
            this.SettingCamera();

            // ティーポットの移動
            if (this._keys[(int)Keys.Left])
            {
                this._teapotPosition.X -= 0.1f;
            }
            if (this._keys[(int)Keys.Right])
            {
                this._teapotPosition.X += 0.1f;
            }
            if (this._keys[(int)Keys.Down])
            {
                this._teapotPosition.Z -= 0.1f;
            }
            if (this._keys[(int)Keys.Up])
            {
                this._teapotPosition.Z += 0.1f;
            }


            // 2つのメッシュの距離を計算
            float meshLength = Vector3.Length(this._boxPosition - this._teapotPosition);

            // 2つのメッシュの距離が2つのメッシュの半径の合計より小さければ当たっている
            this._hitFlag = (meshLength < (this._teapotRadius + this._boxRadius));
        }

        /// <summary>
        /// 描画処理
        /// </summary>
        public void Draw()
        {
            // デバイスが使える状態か確認する
            if (!this.EnsureDevice())
            {
                return;
            }

            // 描画内容を単色でクリアし、Zバッファもクリア
            this._device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, Color.DarkBlue, 1.0f, 0);

            // 「BeginScene」と「EndScene」の間に描画内容を記述する
            this._device.BeginScene();


            // XYZライン描画
            this.RenderXYZLine();


            Material mtrl;


            // 不透明マテリアル使用
            mtrl = new Material();
            mtrl.Diffuse = Color.White;
            mtrl.Ambient = Color.FromArgb(255, 128, 128, 128);
            this._device.Material = mtrl;

            // アルファブレンディングを無効にする
            this._device.SetRenderState(RenderStates.AlphaBlendEnable, false);

            // ティーポット描画
            this._device.SetTransform(TransformType.World, Matrix.Translation(this._teapotPosition));
            this._teapot.DrawSubset(0);

            // ボックス描画
            this._device.SetTransform(TransformType.World, Matrix.Translation(this._boxPosition));
            this._box.DrawSubset(0);


            // 半透明マテリアル使用
            mtrl = new Material();
            mtrl.Diffuse = Color.FromArgb(128, 192, 192, 192);
            mtrl.Ambient = Color.FromArgb(128, 0, 0, 128);
            this._device.Material = mtrl;

            // アルファブレンディングを有効にする
            this._device.SetRenderState(RenderStates.AlphaBlendEnable, true);

            // ティーポット包括球描画
            this._device.SetTransform(TransformType.World, Matrix.Translation(this._teapotPosition));
            this._teapotSphere.DrawSubset(0);

            // ボックス包括球描画
            this._device.SetTransform(TransformType.World, Matrix.Translation(this._boxPosition));
            this._boxSphere.DrawSubset(0);


            // 文字列の描画
            this.RenderText();

            // 描画はここまで
            this._device.EndScene();

            // 実際のディスプレイに描画
            this._device.Present();
        }

        /// <summary>
        /// テキストの描画
        /// </summary>
        private void RenderText()
        {
            this.DrawText("[Escape]終了", 0, 0, Color.White);
            this.DrawText("θ:" + this._lensPosTheta, 0, 12, Color.White);
            this.DrawText("φ:" + this._lensPosPhi, 0, 24, Color.White);
            this.DrawText("Teapot半径 :" + this._teapotRadius.ToString(), 0, 36, Color.White);
            this.DrawText("Box 半径 :" + this._boxRadius.ToString(), 0, 48, Color.White);
            this.DrawText("2つの距離 :" +
                (Vector3.Length(this._teapotPosition - this._boxPosition)).ToString(),
                0, 60, Color.White);
            if (this._hitFlag)
            {
                this.DrawText("当たっています!", 0, 72, Color.Yellow);
            }
            else
            {
                this.DrawText("当たっていません。", 0, 72, Color.LightBlue);
            }
        }

        /// <summary>
        /// リソースの破棄をするために呼ばれる
        /// </summary>
        public void Dispose()
        {
            // 作成したメッシュの破棄
            this.SafeDispose(this._teapot);
            this.SafeDispose(this._teapotSphere);
            this.SafeDispose(this._box);
            this.SafeDispose(this._boxSphere);
            
            // リソースの破棄
            this.DisposeResource();
        }
    }
}
MainSamplePartial.cs ファイルのコードはこちらです。

MainSamplePartial.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MDXSample
{
    public partial class MainSample
    {
        /// <summary>
        /// メインフォーム
        /// </summary>
        private MainForm _form = null;

        /// <summary>
        /// Direct3D デバイス
        /// </summary>
        private Device _device = null;

        /// <summary>
        /// Direct3D 用フォント
        /// </summary>
        private Microsoft.DirectX.Direct3D.Font _font = null;

        /// <summary>
        /// キーのプレス判定
        /// </summary>
        private bool[] _keys = new bool[256];

        /// <summary>
        /// 1つ前のマウスの位置
        /// </summary>
        private Point _oldMousePoint = Point.Empty;

        /// <summary>
        /// カメラレンズの位置(R)
        /// </summary>
        private float _lensPosRadius = 5.0f;

        /// <summary>
        /// カメラレンズの位置(θ)
        /// </summary>
        private float _lensPosTheta = 300.0f;

        /// <summary>
        /// カメラレンズの位置(φ)
        /// </summary>
        private float _lensPosPhi = 30.0f;

        /// <summary>
        /// XYZライン用頂点バッファ
        /// </summary>
        private VertexBuffer _xyzLineVertexBuffer = null;


        /// <summary>
        /// 入力イベント作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        private void CreateInputEvent(MainForm topLevelForm)
        {
            // キーイベント作成
            topLevelForm.KeyDown += new KeyEventHandler(this.form_KeyDown);
            topLevelForm.KeyUp += new KeyEventHandler(this.form_KeyUp);

            // マウス移動イベント
            topLevelForm.MouseMove += new MouseEventHandler(this.form_MouseMove);
        }

        /// <summary>
        /// キーボードのキーを押した瞬間
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_KeyDown(object sender, KeyEventArgs e)
        {
            // 押されたキーコードのフラグを立てる
            if ((int)e.KeyCode < this._keys.Length)
            {
                this._keys[(int)e.KeyCode] = true;
            }
        }
        /// <summary>
        /// キーボードのキーを放した瞬間
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_KeyUp(object sender, KeyEventArgs e)
        {
            // 放したキーコードのフラグを下ろす
            if ((int)e.KeyCode < this._keys.Length)
            {
                this._keys[(int)e.KeyCode] = false;
            }
        }

        /// <summary>
        /// マウス移動イベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // 回転
                this._lensPosTheta -= e.Location.X - this._oldMousePoint.X;
                this._lensPosPhi += e.Location.Y - this._oldMousePoint.Y;

                // φに関しては制限をつける
                if (this._lensPosPhi >= 90.0f)
                {
                    this._lensPosPhi = 89.9999f;
                }
                else if (this._lensPosPhi <= -90.0f)
                {
                    this._lensPosPhi = -89.9999f;
                }
            }
            // マウスの位置を記憶
            this._oldMousePoint = e.Location;
        }

        /// <summary>
        /// Direct3D デバイスの作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        private void CreateDevice(MainForm topLevelForm)
        {
            // PresentParameters。デバイスを作成する際に必須
            // どのような環境でデバイスを使用するかを設定する
            PresentParameters pp = new PresentParameters();

            // ウインドウモードなら true、フルスクリーンモードなら false を指定
            pp.Windowed = true;

            // スワップ効果。とりあえず「Discard」を指定。
            pp.SwapEffect = SwapEffect.Discard;

            // 深度ステンシルバッファ。3Dでは前後関係があるので通常 true
            pp.EnableAutoDepthStencil = true;

            // 自動深度ステンシル サーフェイスのフォーマット。
            // 「D16」に対応しているビデオカードは多いが、前後関係の精度があまりよくない。
            // できれば「D24S8」を指定したいところ。
            pp.AutoDepthStencilFormat = DepthFormat.D16;

            try
            {
                // デバイスの作成
                this.CreateDevice(topLevelForm, pp);
            }
            catch (DirectXException ex)
            {
                // 例外発生
                throw ex;
            }
        }
        /// <summary>
        /// Direct3D デバイスの作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        /// <param name="presentationParameters">PresentParameters 構造体</param>
        private void CreateDevice(MainForm topLevelForm, PresentParameters presentationParameters)
        {
            // 実際にデバイスを作成します。
            // 常に最高のパフォーマンスで作成を試み、
            // 失敗したら下位パフォーマンスで作成するようにしている。
            try
            {
                // ハードウェアによる頂点処理、ラスタライズを行う
                // 最高のパフォーマンスで処理を行えます。
                // ビデオカードによっては実装できない処理が存在します。
                this._device = new Device(0, DeviceType.Hardware, topLevelForm.Handle,
                    CreateFlags.HardwareVertexProcessing, presentationParameters);
            }
            catch (DirectXException ex1)
            {
                // 作成に失敗
                Debug.WriteLine(ex1.ToString());
                try
                {
                    // ソフトウェアによる頂点処理、ハードウェアによるラスタライズを行う
                    this._device = new Device(0, DeviceType.Hardware, topLevelForm.Handle,
                        CreateFlags.SoftwareVertexProcessing, presentationParameters);
                }
                catch (DirectXException ex2)
                {
                    // 作成に失敗
                    Debug.WriteLine(ex2.ToString());
                    try
                    {
                        // ソフトウェアによる頂点処理、ラスタライズを行う
                        // パフォーマンスはとても低いです。
                        // その代わり、ほとんどの処理を制限なく行えます。
                        this._device = new Device(0, DeviceType.Reference, topLevelForm.Handle,
                            CreateFlags.SoftwareVertexProcessing, presentationParameters);
                    }
                    catch (DirectXException ex3)
                    {
                        // 作成に失敗
                        // 事実上デバイスは作成できません。
                        throw ex3;
                    }
                }
            }

            // デバイスの状態変更イベントを作成
            this._device.DeviceLost += new EventHandler(this.device_DeviceLost);
            this._device.DeviceReset += new EventHandler(this.device_DeviceReset);
        }

        /// <summary>
        /// フォントの作成
        /// </summary>
        private void CreateFont()
        {
            try
            {
                // フォントデータの構造体を作成
                FontDescription fd = new FontDescription();

                // 構造体に必要なデータをセット
                fd.Height = 12;
                fd.FaceName = "MS ゴシック";

                // フォントを作成
                this._font = new Microsoft.DirectX.Direct3D.Font(this._device, fd);
            }
            catch (DirectXException ex)
            {
                // 例外発生
                throw ex;
            }
        }

        /// <summary>
        /// XYZライン作成
        /// </summary>
        private void CreateXYZLine()
        {
            // 6つ分の頂点を作成
            this._xyzLineVertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
                6, this._device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);

            // 頂点バッファをロックして、位置、色情報を書き込む
            using (GraphicsStream data = this._xyzLineVertexBuffer.Lock(0, 0, LockFlags.None))
            {
                // 今回は各XYZのラインを原点(0.0f, 0.0f, 0.0f)からプラス方向に 10.0f 伸びた線を作成
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Red.ToArgb()));
                data.Write(new CustomVertex.PositionColored(10.0f, 0.0f, 0.0f, Color.Red.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Green.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 10.0f, 0.0f, Color.Green.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Blue.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 10.0f, Color.Blue.ToArgb()));

                this._xyzLineVertexBuffer.Unlock();
            }
        }

        /// <summary>
        /// カメラの位置をセット
        /// </summary>
        /// <param name="radius">半径(degree)</param>
        /// <param name="theta">水平方向角度(degree)</param>
        /// <param name="phi">垂直方向角度(degree)</param>
        private void SetCameraPosition(float radius, float theta, float phi)
        {
            this._lensPosRadius = radius;
            this._lensPosTheta = theta;
            this._lensPosPhi = phi;
        }

        /// <summary>
        /// ライトの設定
        /// </summary>
        private void SettingLight()
        {
            // 平行光線を使用
            this._device.Lights[0].Type = LightType.Directional;

            // ライトの方向
            this._device.Lights[0].Direction = new Vector3(1.0f, -1.5f, 2.0f);

            // 光の色は白
            this._device.Lights[0].Diffuse = Color.White;

            // 環境光
            this._device.Lights[0].Ambient = Color.FromArgb(255, 128, 128, 128);

            // 0 番のライトを有効
            this._device.Lights[0].Enabled = true;

            // 0 番のライトを更新
            this._device.Lights[0].Update();
        }

        /// <summary>
        /// カメラの設定
        /// </summary>
        private void SettingCamera()
        {
            // レンズの位置を三次元極座標で変換
            float radius = this._lensPosRadius;
            float theta = Geometry.DegreeToRadian(this._lensPosTheta);
            float phi = Geometry.DegreeToRadian(this._lensPosPhi);
            Vector3 lensPosition = new Vector3(
                (float)(radius * Math.Cos(theta) * Math.Cos(phi)),
                (float)(radius * Math.Sin(phi)),
                (float)(radius * Math.Sin(theta) * Math.Cos(phi)));

            // ビュー変換行列を設定
            this._device.Transform.View = Matrix.LookAtLH(
                lensPosition, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));

            // 射影変換を設定
            this._device.Transform.Projection = Matrix.PerspectiveFovLH(
                Geometry.DegreeToRadian(60.0f),
                (float)this._device.Viewport.Width / (float)this._device.Viewport.Height,
                1.0f, 100.0f);
        }

        /// <summary>
        /// デバイスを保証する
        /// </summary>
        /// <returns></returns>
        private bool EnsureDevice()
        {
            // デバイスが動作可能かチェック
            int deviceResult;
            if (!this._device.CheckCooperativeLevel(out deviceResult))
            {
                switch ((ResultCode)deviceResult)
                {
                    case ResultCode.DeviceLost:
                        // まだリセットできる状態ではないので少し待つ
                        System.Threading.Thread.Sleep(10);
                        return false;
                    case ResultCode.DeviceNotReset:
                        // リセット可能状態

                        // デバイスをリセット
                        this._device.Reset(this._device.PresentationParameters);
                        return true;
                    default:
                        // 原因不明(正確には上記以外)
                        // まだリセットできる状態ではないので少し待つ
                        System.Threading.Thread.Sleep(10);
                        return false;
                }
            }
            return true;
        }

        /// <summary>
        /// XYZライン描画
        /// </summary>
        private void RenderXYZLine()
        {
            // ライトの状態を記憶
            bool oldLightEnable = this._device.RenderState.Lighting;

            // ライトを無効
            if (oldLightEnable)
            {
                this._device.RenderState.Lighting = false;
            }

            // テクスチャー無効
            this._device.SetTexture(0, null);

            // 原点に配置
            this._device.SetTransform(TransformType.World, Matrix.Identity);

            // ストリームセット
            this._device.SetStreamSource(0, this._xyzLineVertexBuffer, 0);

            // 頂点フォーマットセット
            this._device.VertexFormat = CustomVertex.PositionColored.Format;

            // 描画
            this._device.DrawPrimitives(PrimitiveType.LineList, 0, 3);

            // ライトを戻す
            if (oldLightEnable)
            {
                this._device.RenderState.Lighting = oldLightEnable;
            }
        }

        /// <summary>
        /// テキストの描画
        /// </summary>
        /// <param name="text">描画する文字列</param>
        /// <param name="left">文字列の左位置</param>
        /// <param name="top">文字列の上位置</param>
        /// <param name="color">文字の色</param>
        private void DrawText(string text, int left, int top, Color color)
        {
            this._font.DrawText(null, text, left, top, color);
        }

        /// <summary>
        /// リソースの破棄
        /// </summary>
        private void DisposeResource()
        {
            // XYZラインの破棄
            this.SafeDispose(this._xyzLineVertexBuffer);

            // フォントのリソース解放
            this.SafeDispose(this._font);

            // Direct3D デバイスのリソース解放
            this.SafeDispose(this._device);
        }

        /// <summary>
        /// 安全なリソース破棄
        /// </summary>
        /// <param name="resource">破棄するリソース</param>
        private void SafeDispose(IDisposable resource)
        {
            if (resource != null)
            {
                resource.Dispose();
            }
        }
    }
}

/// <summary>
/// ティーポット
/// </summary>
private Mesh _teapot = null;

/// <summary>
/// ティーポット当たり判定球の半径
/// </summary>
private float _teapotRadius = 0.0f;

/// <summary>
/// ティーポットの当たり判定球実体化用
/// </summary>
private Mesh _teapotSphere = null;

/// <summary>
/// ティーポットの位置
/// </summary>
private Vector3 _teapotPosition = Vector3.Empty;

操作対象のティーポットのメッシュを用意します。

後のコードでモデルの「包括球」というものを計算しますが、その球の半径をフィールドとして持つようにします。

実際には必要ないのですが、サンプルとして球が見えたほうが分かりやすいので球のメッシュも用意します。

あとはティーポットを移動させるので、その位置情報を持ちます。


/// <summary>
/// ボックス
/// </summary>
private Mesh _box = null;

/// <summary>
/// ボックス当たり判定球の半径
/// </summary>
private float _boxRadius = 0.0f;

/// <summary>
/// ボックスの当たり判定球実体化用
/// </summary>
private Mesh _boxSphere = null;

/// <summary>
/// ボックスの位置
/// </summary>
private Vector3 _boxPosition = new Vector3(-3.0f, 0.0f, -4.0f);

ティーポットと衝突させるオブジェクトとしてボックスを作成します。必要なデータはティーポットと同じです。ボックスは移動させないので、先に初期位置を指定しています。


/// <summary>
/// 当たり判定フラグ
/// </summary>
private bool _hitFlag = false;

衝突しているかを文字で描画するために、衝突フラグを持っておきます。


// ティーポット作成
this._teapot = Mesh.Teapot(this._device);

ティーポットメッシュを作成しています。Mesh クラスであれば、Xファイルから読み込んだモデルでもかまいません。


// ティーポットの包括球を計算するために頂点バッファ取得
using (VertexBuffer vb = this._teapot.VertexBuffer)
{
    Vector3 center;

    // 頂点バッファをロックしてストリーム取得
    using (GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.None))
    {
        // ティーポットの包括球を計算し、半径取得
        this._teapotRadius = Geometry.ComputeBoundingSphere(vertexData,
            this._teapot.NumberVertices, this._teapot.VertexFormat, out center);

        vertexData.Close();
    }

    // 頂点ロック解除
    vb.Unlock();
}

ここにメッシュの包括球というものを計算しますが、包括球とはメッシュの全ての頂点を含むことが出来る球のことです。

これを計算するには、メッシュの頂点バッファを使用します。メッシュの頂点バッファは「Mesh.VertexBuffer」プロパティで取れるので、これを使用することにします。

頂点バッファを取得したらロックを掛け、頂点データを受け取ります。「Geometry.ComputeBoundingSphere」メソッドで包括球の中心位置球の半径が得られるのでそれを取得します。

渡すパラメータは「頂点データ」「頂点数」「頂点フォーマット」「取得用の境界球の座標の中心」です。最後の「中心座標」は今回使用しませんが、渡さないといけないので適当に宣言して使っています。

使い終わった頂点バッファは必ずロック解除します。


// 見やすいように当たり判定球を作成
this._teapotSphere = Mesh.Sphere(this._device, this._teapotRadius, 16, 16);

当たり判定球が見やすいように、取得した半径から球を作成しています。面の分割数は適当に指定してください。あんまり少ないとティーポットが球からはみ出ます。(ポリゴン数が少ないと正確な球を表現できないため。もちろん見た目だけなので計算上は問題ありません


// ボックス作成
this._box = Mesh.Box(this._device, 2.0f, 2.0f, 2.0f);

// ボックスの包括球を計算するために頂点バッファ取得
using (VertexBuffer vb = this._box.VertexBuffer)
{
    Vector3 center;

    // 頂点バッファをロックしてストリーム取得
    using (GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.None))
    {
        // ボックスの包括球を計算し、半径取得
        this._boxRadius = Geometry.ComputeBoundingSphere(vertexData,
            this._box.NumberVertices, this._box.VertexFormat, out center);

        vertexData.Close();
    }

    // 頂点ロック解除
    vb.Unlock();
}

// 見やすいように当たり判定球を作成
this._boxSphere = Mesh.Sphere(this._device, this._boxRadius, 16, 16);

ボックスに関しては、ティーポットと同じ要領で作成します。


// アルファブレンディング方法を設定
this._device.RenderState.SourceBlend = Blend.SourceAlpha;
this._device.RenderState.DestinationBlend = Blend.InvSourceAlpha;

衝突判定用の球を半透明で表示するのであらかじめアルファブレンディング方法を決めておきます。


// ティーポットの移動
if (this._keys[(int)Keys.Left])
{
    this._teapotPosition.X -= 0.1f;
}
if (this._keys[(int)Keys.Right])
{
    this._teapotPosition.X += 0.1f;
}
if (this._keys[(int)Keys.Down])
{
    this._teapotPosition.Z -= 0.1f;
}
if (this._keys[(int)Keys.Up])
{
    this._teapotPosition.Z += 0.1f;
}

キーボードのカーソルキー「↑↓←→」でティーポットの位置を移動できるようにしています。


// 2つのメッシュの距離を計算
float meshLength = Vector3.Length(this._boxPosition - this._teapotPosition);

// 2つのメッシュの距離が2つのメッシュの半径の合計より小さければ当たっている
this._hitFlag = (meshLength < (this._teapotRadius + this._boxRadius));

さて、今回の Tips の目的でもある「球と球の衝突判定」です。

やっていることはとても簡単で「2つのメッシュの包括球半径の和」と「2つのメッシュの中心座標の距離」を求めて、「2つのメッシュの中心座標の距離」が「2つのメッシュの包括球半径の和」よりも小さければあったっているということになります。文章だとちょっと分かりづらいかと思いますので、下の図を見ていただければ一目瞭然です。

衝突判定方法

真ん中のパターンを当たりにするかしないかは各自決めてください。


Material mtrl;


// 不透明マテリアル使用
mtrl = new Material();
mtrl.Diffuse = Color.White;
mtrl.Ambient = Color.FromArgb(255, 128, 128, 128);
this._device.Material = mtrl;

// アルファブレンディングを無効にする
this._device.SetRenderState(RenderStates.AlphaBlendEnable, false);

// ティーポット描画
this._device.SetTransform(TransformType.World, Matrix.Translation(this._teapotPosition));
this._teapot.DrawSubset(0);

// ボックス描画
this._device.SetTransform(TransformType.World, Matrix.Translation(this._boxPosition));
this._box.DrawSubset(0);


// 半透明マテリアル使用
mtrl = new Material();
mtrl.Diffuse = Color.FromArgb(128, 192, 192, 192);
mtrl.Ambient = Color.FromArgb(128, 0, 0, 128);
this._device.Material = mtrl;

// アルファブレンディングを有効にする
this._device.SetRenderState(RenderStates.AlphaBlendEnable, true);

// ティーポット包括球描画
this._device.SetTransform(TransformType.World, Matrix.Translation(this._teapotPosition));
this._teapotSphere.DrawSubset(0);

// ボックス包括球描画
this._device.SetTransform(TransformType.World, Matrix.Translation(this._boxPosition));
this._boxSphere.DrawSubset(0);

今回は描画中心の話しではないので、特に変わった部分はありません。

不透明のオブジェクト」と「半透明の球」をレンダリングするので、先に不透明のオブジェクトをレンダリングしてください。これは3Dプログラミングのお約束みたいなもので、逆の順序で描画すると、Zバッファの影響でオブジェクトが描画されないことがあるためです。


if (this._hitFlag)
{
    this.DrawText("当たっています!", 0, 72, Color.Yellow);
}
else
{
    this.DrawText("当たっていません。", 0, 72, Color.LightBlue);
}

当たり判定状態を文字で表示しています。