回転軸を指定して文字を回転させる

Page creation date :

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

概要

回転軸の位置を指定して回転させています。サンプルでは視覚的にわかるように自動で回転させています。

回転軸を指定して文字を回転させる

動作環境

必須環境

対応 XNA バージョン
  • 2.0
  • 3.0
  • 3.1
  • 4.0
対応プラットフォーム
  • Windows (XP SP2 以降, Vista, 7)
  • Xbox 360
  • Windows Phone 7
Windows 必須頂点シェーダ バージョン 2.0
Windows 必須ピクセルシェーダ バージョン 2.0

動作確認環境

プラットフォーム
  • Windows 7
  • Xbox 360
  • Windows Phone 7 エミュレーター

内容

テキストを回転させるとき、回転軸はデフォルトでテキストの左上になります。

デフォルトの回転軸位置

回転軸を変更したい場合は、「SpriteBatch.DrawString」メソッドの第6引数に回転軸位置を指定することにより、テキストの任意の位置を軸にして回転させることができます。

しかし、実行してみるとわかりますが、単純に回転軸位置が指定されるわけではなく、実は回転軸位置はそのままで、テキストの描画位置が -origin 分移動していることがわかります。これによってあたかもテキストの任意の位置を回転軸にしているかのように見せているのです。

回転軸位置を指定するとテキストが相対的に移動される

回転軸位置を(100, 0)と指定すると、テキストが左に 100px 移動していることがわかる。

回転軸位置はそのままなので、テキストは左上の位置から相対で(100, 0)の位置を回転軸として回転していることになる。

///// 回転軸 (100, 0) /////

// 回転なし
this.spriteBatch.DrawString(
    this.font,
    "Draw rotate text.",
    new Vector2(100.0f, 250.0f),
    Color.White,
    0.0f,
    Vector2.Zero,
    1.0f,
    SpriteEffects.None,
    0.0f);

// 回転あり
this.spriteBatch.DrawString(
    this.font,
    "Draw rotate text.",
    new Vector2(100.0f, 250.0f),
    Color.White,
    MathHelper.ToRadians(this.realRotate),
    new Vector2(100.0f, 0.0f),
    1.0f,
    SpriteEffects.None,
    0.0f);

これを直感的に回転軸だけを指定しているようにするには、origin 分テキストを移動させることにより解決できます。

回転軸位置で左に移動したテキスト

左にずれたテキストを

テキストを移動させてテキストの中心で回転させているように見せる

右に移動させると回転軸だけを指定したように見せられる

///// 回転軸分位置を補正して回転 (120, 12) /////

// 回転なし
this.spriteBatch.DrawString(
    this.font,
    "Draw rotate text.",
    new Vector2(100.0f, 400.0f),
    Color.White,
    0.0f,
    Vector2.Zero,
    1.0f,
    SpriteEffects.None,
    0.0f);

// 回転あり
this.spriteBatch.DrawString(
    this.font,
    "Draw rotate text.",
    new Vector2(200.0f, 414.0f),
    Color.White,
    MathHelper.ToRadians(this.realRotate),
    new Vector2(100.0f, 14.0f),
    1.0f,
    SpriteEffects.None,
    0.0f);

SpriteBatch.DrawString メソッド

文字列を描画します。

spriteFont SpriteFont 文字列のイメージが格納されている SpriteFont を指定します。
text string 表示するテキストを指定します。
position Vector2 テキストを表示する位置。スクリーンの左上を基準にしたスクリーン座標で指定します。テキストの原点は最初の文字の左上になります。
color Color テキストの色
rotation float テキストの回転角度。単位は radian で指定する。回転軸はテキストの左上になります。
origin Vector2 テキストを回転させるときの回転軸の位置を指定します。テキストのどの位置を回転軸にするかを指定しますが、実際には回転軸の位置はテキストの左上固定で、テキストの表示位置が -origin 移動します。
scale float テキストの拡大率を指定します。1.0を基準として、縦と横に拡大縮小します。拡大の原点はテキストの左上になります。
effects SpriteEffects テキストの表示反転効果を指定します。特に何もしなければ SpriteEffects.None を指定します。
layerDepth float テキストを表示する深度を指定します。主にテキストを手前に表示したり奥に表示するのに使用します。0.0~1.0 の範囲で指定し、0.0が最前面、1.0が最背面になります。

全コード

using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace RotateOriginText
{
    /// <summary>
    /// ゲームメインクラス
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// グラフィックデバイス管理クラス
        /// </summary>
        private GraphicsDeviceManager graphics = null;

        /// <summary>
        /// スプライトのバッチ化クラス
        /// </summary>
        private SpriteBatch spriteBatch = null;

        /// <summary>
        /// スプライトでテキストを描画するためのフォント
        /// </summary>
        private SpriteFont font = null;

        /// <summary>
        /// リアルタイムに回転量が増える
        /// </summary>
        private float realRotate = 0.0f;


        /// <summary>
        /// GameMain コンストラクタ
        /// </summary>
        public GameMain()
        {
            // グラフィックデバイス管理クラスの作成
            this.graphics = new GraphicsDeviceManager(this);

            // ゲームコンテンツのルートディレクトリを設定
            this.Content.RootDirectory = "Content";

#if WINDOWS_PHONE
            // Windows Phone のデフォルトのフレームレートは 30 FPS
            this.TargetElapsedTime = TimeSpan.FromTicks(333333);

            // バックバッファサイズの設定
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 800;

            // フルスクリーン表示
            this.graphics.IsFullScreen = true;
#endif
        }

        /// <summary>
        /// ゲームが始まる前の初期化処理を行うメソッド
        /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
        /// </summary>
        protected override void Initialize()
        {
            // TODO: ここに初期化ロジックを書いてください

            // コンポーネントの初期化などを行います
            base.Initialize();
        }

        /// <summary>
        /// ゲームが始まるときに一回だけ呼ばれ
        /// すべてのゲームコンテンツを読み込みます
        /// </summary>
        protected override void LoadContent()
        {
            // テクスチャーを描画するためのスプライトバッチクラスを作成します
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

            // フォントをコンテンツパイプラインから読み込む
            this.font = this.Content.Load<SpriteFont>("Font");
        }

        /// <summary>
        /// ゲームが終了するときに一回だけ呼ばれ
        /// すべてのゲームコンテンツをアンロードします
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: ContentManager で管理されていないコンテンツを
            //       ここでアンロードしてください
        }

        /// <summary>
        /// 描画以外のデータ更新等の処理を行うメソッド
        /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
            // ゲームを終了させます
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // 自動で回転量を増やす
            this.realRotate += (float)gameTime.ElapsedGameTime.TotalSeconds * 60.0f;

            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }

        /// <summary>
        /// 描画処理を行うメソッド
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);

            // スプライトの描画準備
            this.spriteBatch.Begin();

            ///// 回転軸デフォルト /////

            // 回転なし
            this.spriteBatch.DrawString(
                this.font,
                "Draw rotate text.",
                new Vector2(100.0f, 100.0f),
                Color.White,
                0.0f,
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                0.0f);

            // 回転あり
            this.spriteBatch.DrawString(
                this.font,
                "Draw rotate text.",
                new Vector2(100.0f, 100.0f),
                Color.White,
                MathHelper.ToRadians(this.realRotate),
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                0.0f);


            ///// 回転軸 (100, 0) /////

            // 回転なし
            this.spriteBatch.DrawString(
                this.font,
                "Draw rotate text.",
                new Vector2(100.0f, 250.0f),
                Color.White,
                0.0f,
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                0.0f);

            // 回転あり
            this.spriteBatch.DrawString(
                this.font,
                "Draw rotate text.",
                new Vector2(100.0f, 250.0f),
                Color.White,
                MathHelper.ToRadians(this.realRotate),
                new Vector2(100.0f, 0.0f),
                1.0f,
                SpriteEffects.None,
                0.0f);

            ///// 回転軸分位置を補正して回転 (120, 12) /////

            // 回転なし
            this.spriteBatch.DrawString(
                this.font,
                "Draw rotate text.",
                new Vector2(100.0f, 400.0f),
                Color.White,
                0.0f,
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                0.0f);

            // 回転あり
            this.spriteBatch.DrawString(
                this.font,
                "Draw rotate text.",
                new Vector2(200.0f, 414.0f),
                Color.White,
                MathHelper.ToRadians(this.realRotate),
                new Vector2(100.0f, 14.0f),
                1.0f,
                SpriteEffects.None,
                0.0f);

            // スプライトの一括描画
            this.spriteBatch.End();

            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}