ログイン | アカウントの作成
CONTACT | HOME | BBS | Wiki | Blog | Tools |


wikiトップページ

最近更新されたページ
Bit演算比較
型の処理速度比較
Arrayの複製速度比較
PHP Linuxで実行する
flash / Adobe Air
Flash
new Date().getTime速度テスト
派生クラスのアクセス速度
複数のクラスをまたいだパラメーターへのアクセス速度
getter setter 速度比較
型比較の速度比較
Static変数の速度比較
サーバー構築 / CVSサーバー
サーバー構築
サーバー構築 / WebMin
XNA
Array多次元配列の速度比較
オペレーター(加算、減算、積、乗算、余り)の比較
Numberを整数に変換処理比較
flash / jsfl

トップページ / XNA / 2D to 3D ビデオ

2D to 3D ビデオ

クリックした2D座標をy座標0で交わるの3D座標を取得して
キャラクターをその場所に移動させます。

ビデオ

コード

// variables defined elsewhere
    Vector2 mousePosition;
    GraphicsDeviceManager graphicsDevMgr;

    // class that can access your ViewMatrix and ProjectionMatrix
    ChaseCamera chaseCamera;

    //----------------------------------------------------------------
    // GetPickedPosition() - gets 3D position of mouse pointer
    //                     - always on the the Y = 0 plane     
    //----------------------------------------------------------------
    public static Vector3 GetPickedPosition()
    {

        // create 2 positions in screenspace using the cursor position. 0 is as
        // close as possible to the camera, 10 is as far away as possible
        Vector3 nearSource = new Vector3(mousePosition, 0f);
        Vector3 farSource = new Vector3(mousePosition, chaseCamera.nearClip);

        // find the two screen space positions in world space
        Vector3 nearPoint = graphicsDevMgr.GraphicsDevice.Viewport.Unproject(nearSource,
                            chaseCamera.projectionMatrix, 
                            chaseCamera.viewMatrix, Matrix.Identity);

        Vector3 farPoint = graphicsDevMgr.GraphicsDevice.Viewport.Unproject(farSource,
                            chaseCamera.projectionMatrix,
                            chaseCamera.viewMatrix, Matrix.Identity);

        // compute normalized direction vector from nearPoint to farPoint
        Vector3 direction = farPoint - nearPoint;
        direction.Normalize();

        // create a ray using nearPoint as the source
        Ray r = new Ray(nearPoint, direction);

        // calculate the ray-plane intersection point
        Vector3 n = new Vector3(0f, 1f, 0f);
        Plane p = new Plane(n, 0f);

        // calculate distance of intersection point from r.origin
        float denominator = Vector3.Dot(p.Normal, r.Direction);
        float numerator = Vector3.Dot(p.Normal, r.Position) + p.D;
        float t = -(numerator / denominator);

        // calculate the picked position on the y = 0 plane
        Vector3 pickedPosition = nearPoint + direction * t;

        return pickedPosition;
    }

参考URL


XNAに戻る




CgInstitute のホーム   -    コンタクト   -    CgInstitute について   -    アカウントの作成