
※この Tips は XNA Game Studio 2.0 をベースに説明しています。
テキストを回転させるとき、回転軸はデフォルトでテキストの左上になります。

回転軸を変更したい場合は、「SpriteBatch.DrawString」メソッドの第6引数に回転軸位置を指定することにより、テキストの任意の位置を軸にして回転させることができます。
しかし、実行してみるとわかりますが、単純に回転軸位置が指定されるわけではなく、実は回転軸位置はそのままで、テキストの描画位置が -origin 分移動していることがわかります。これによってあたかもテキストの任意の位置を回転軸にしているかのように見せているのです。

回転軸位置を(100, 0)と指定すると、テキストが左に 100px 移動していることがわかる。
回転軸位置はそのままなので、テキストは左上の位置から相対で(100, 0)の位置を回転軸として回転していることになる。
///// 回転軸 (100, 0) ///// // 回転なし this.spriteBatch.DrawString( this.font, "Draw rotate text.", new Vector2(200.0f, 300.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, 300.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(200.0f, 450.0f), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f); // 回転あり this.spriteBatch.DrawString( this.font, "Draw rotate text.", new Vector2(320.0f, 462.0f), // 回転軸分移動した分を加算している Color.White, MathHelper.ToRadians(this.realRotate), new Vector2(120.0f, 12.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が最背面になります。 |
| MSDN ライブラリの Web ページへリンク | ||
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_rotateorigintext_3_0_exe.zip | 14.8 KB | 3.0 | Windows (XP SP2 以降, Vista) | 2009/01/04 |
| xna_tips_rotateorigintext_2_0_exe.zip | 18.2 KB | 2.0 | Windows (XP SP2, Vista) | 2008/01/01 |
| xna_tips_rotateorigintext_1_0_ref_exe.zip | 18.4 KB | 1.0 Refresh | Windows (XP SP2, Vista) | 2007/07/17 |
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_rotateorigintext_3_0_project.zip | 16.6 KB | 3.0 | Windows (XP SP2 以降, Vista), Xbox 360 | 2009/01/04 |
| xna_tips_rotateorigintext_2_0_project.zip | 15.0 KB | 2.0 | Windows (XP SP2, Vista), Xbox 360 | 2008/01/01 |
| xna_tips_rotateorigintext_1_0_ref_project.zip | 17.1 KB | 1.0 Refresh | Windows (XP SP2, Vista), Xbox 360 | 2007/07/17 |
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 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"; } /// <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) { // Xbox360 コントローラの BACK ボタンを押したときにゲームを終了させます if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // 自動で回転量を増やす this.realRotate += 1.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(200.0f, 150.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, 150.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(200.0f, 300.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, 300.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(200.0f, 450.0f), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f); // 回転あり this.spriteBatch.DrawString( this.font, "Draw rotate text.", new Vector2(320.0f, 462.0f), Color.White, MathHelper.ToRadians(this.realRotate), new Vector2(120.0f, 12.0f), 1.0f, SpriteEffects.None, 0.0f); // スプライトの一括描画 this.spriteBatch.End(); // 登録された DrawableGameComponent を描画する base.Draw(gameTime); } } }
| 更新日時 | 更新内容 |
|---|---|
| 2009/01/04 | XNA Game Studio 3.0 のプロジェクト追加 |
| 2008/05/18 | 文章・プログラムの校正 |
| 2008/01/01 | XNA Game Studio 2.0 用に修正 |
| 2007/07/18 | ページ作成 |