Rotate text by specifying the rotation axis
summary
The position of the rotation axis is specified and rotated. In the sample, it is automatically rotated so that you can see it visually.
Operating environment
Prerequisites
Supported XNA Versions |
|
Supported Platforms |
|
Windows Required Vertex Shader Version | 2.0 |
Windows Required Pixel Shader Version | 2.0 |
Operating environment
platform |
|
substance
When you rotate text, the axis of rotation defaults to the top left of the text.
If you want to change the axis of rotation, you can rotate it around any position of the text by specifying the axis of rotation position in the sixth argument of the "SpriteBatch.DrawString" method.
However, as you can see when you run it, the rotation axis position is not simply specified, but in fact, the rotation axis position remains the same, and the drawing position of the text is moved by -origin. This makes it look as if the axis of rotation is at any point in the text.
If you specify the rotation axis position as (100, 0), you can see that the text has moved 100px to the left.
Since the rotation axis position is the same, the text is rotated relative to the upper left position with the position (100, 0) as the rotation axis.
// 回転軸 (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);
If you want to intuitively specify only the axis of rotation, you can solve this by moving the text by the origin minute.
Replace left-shifted text with
If you move it to the right, it will appear as if only the axis of rotation is specified.
// 回転軸分位置を補正して回転 (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
method
Draw a string.
spriteFont | SpriteFont | Specifies the SpriteFont that contains the string image. |
text | string | Specifies the text to display. |
position | Vector2 | The position at which to display the text. Specify in screen coordinates relative to the top left of the screen. The origin of the text will be at the top left of the first character. |
color | Color | Text color |
rotation | float | The rotation angle of the text. Units are specified in radian. The axis of rotation will be at the top left of the text. |
origin | Vector2 | Specifies the position of the axis of rotation when rotating text. You specify which position of the text is the axis of rotation, but in reality, the axis of rotation is fixed to the top left of the text, and the display position of the text is moved by -origin. |
scale | float | Specifies the magnification of the text. Using 1.0 as a reference, scale vertically and horizontally. The origin of the expansion is at the top left of the text. |
effects | SpriteEffects | Specifies the effect of inverting the display of text. If you don't do anything else, specify SpriteEffects.None. |
layerDepth | float | Specifies the depth at which the text is displayed. It is mainly used to display text in the foreground or back. Specify in the range of 0.0~1.0, where 0.0 is the front and 1.0 is the back. |
All Codes
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);
}
}
}