▼XNA Game Studioメモ▼
モデルの回転


モデルの回転を行うプログラムを作成する。左右キーでZ軸回転、上下キーでX軸回転を行う。



リソースの追加
  1. 戦闘機の3DモデルをソリューションエクスプローラのContentにドラッグ&ドロップ。


ソースコードの編集
  1. モデルの移動で利用したCameraクラスとFigureクラスを追加。
  2. Game.csを以下のように編集。
Game.cs
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RotateEx {
    public class Game : Microsoft.Xna.Framework.Game {
        //システム
        private GraphicsDeviceManager graphics;//グラフィックス

        //フィギュア
        private Camera camera; //カメラ
        private Figure fighter;//戦闘機

        //コンストラクタ
        public Game() {
            graphics=new GraphicsDeviceManager(this);
            Content.RootDirectory="Content";
        }

        //初期化
        protected override void Initialize() {
            //描画間隔の指定
            IsFixedTimeStep=true;
            TargetElapsedTime=new TimeSpan(200000);
            
            //ベースの初期化
            base.Initialize();
        }

        //グラフィクスコンテントの読み込み
        protected override void LoadContent() {
            //カメラの生成
            camera=new Camera(
                (float)GraphicsDevice.Viewport.Width/
                (float)GraphicsDevice.Viewport.Height);
            
            //戦闘機の生成
            fighter=new Figure(Content.Load<Model>("fighter")); 
        }

        //グラフィクスコンテントの解放
        protected override void UnloadContent() {
        }

        //更新
        protected override void Update(GameTime gameTime) {
            //キー状態
            KeyboardState keyState=Keyboard.GetState();
            
            //キー操作
            if (keyState.IsKeyDown(Keys.Left)) {
                fighter.RotateZ-=0.2f;
            } else if (keyState.IsKeyDown(Keys.Right)) {
                fighter.RotateZ+=0.2f;
            } else if (keyState.IsKeyDown(Keys.Up)) {
                fighter.RotateX+=0.2f;
            } else if (keyState.IsKeyDown(Keys.Down)) {
                fighter.RotateX-=0.2f;
            }
                        
            //ベースの更新
            base.Update(gameTime);
        }

        //描画
        protected override void Draw(GameTime gameTime) {
            //背景色
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            //フィギュアの描画
            fighter.Draw(camera);

            //ベースの描画
            base.Draw(gameTime);
        }   
    }
}



−戻る−