Radian și grad
rezumat
Descrie radianul și gradul utilizate pentru a reprezenta unghiurile.
Mediul de operare
Cerințe preliminare
Versiuni XNA acceptate |
|
Platforme acceptate |
|
Versiunea Vertex Shader necesară pentru Windows | 2.0 |
Versiunea Pixel Shader necesară pentru Windows | 2.0 |
Mediul de operare
peron |
|
Cum se lucrează cu eșantionul
Tastatură de lucruControler Xbox | 360 tactil de mouse | |||
---|---|---|---|---|
Rotirea săgeților | ←→ | Stick-ul stânga | Buton stânga (rotire automată) | - |
substanță
Există două unități care indică unghiul: "Grad" și "Radian". "Grad" este definit ca intervalul de o tură este "0~360". "Radian" este definit ca "0~2π" și "π" este aproximativ 3,14. Imaginea de mai jos este ilustrată în figură.
În general, unghiurile sunt adesea văzute în unități de grad, dar în lumea matematicii și a programării, Radian este mai des folosit. De fapt, majoritatea metodelor claselor definite în cadrul XNA ar trebui să folosească Raian.
Conversia între radian și grad este posibilă și poate fi obținută folosind următoarea formulă:
- Conversia de la grad la radian
- Radian = Grad / 180 * π
- Convertiți din Radian în Grad
- Grad = Radian * 180 / π
De asemenea, puteți efectua conversii de unități cu calculele de mai sus, dar XNA Framework oferă metodele "MathHelper.ToRadians" și "MathHelper.ToDegrees" care vă permit să efectuați fiecare conversie.
câmp
<summary>
Degree の値
</summary>
private float degree = 0.0f;
<summary>
矢印テクスチャー
</summary>
private Texture2D arrowTexture = null;
În eșantion, săgețile sunt rotite, dar aici valoarea este Grade.
transformare
// 回転テクスチャー
this.spriteBatch.Draw(this.arrowTexture,
new Vector2(150.0f, 150.0f) + this.textureCenter,
null, Color.Red, MathHelper.ToRadians(this.degree), this.textureCenter,
1.0f, SpriteEffects.None, 0.0f);
// Degree と Radian
this.spriteBatch.DrawString(this.font,
"Degree : " + this.degree.ToString() + Environment.NewLine +
"Radian : " + MathHelper.ToRadians(this.degree).ToString(),
new Vector2(30.0f, 30.0f), Color.White);
Deoarece parametrul de rotație al metodei SpriteBatch.Draw trebuie trecut într-un radian, valoarea Degree de menținere este convertită într-un radian în metoda MathHelper.ToRadians.
Toate codurile
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 RadianAndDegree
{
<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>
Degree の値
</summary>
private float degree = 0.0f;
<summary>
矢印テクスチャー
</summary>
private Texture2D arrowTexture = null;
<summary>
テクスチャーの中心座標
</summary>
private Vector2 textureCenter = Vector2.Zero;
<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");
// テクスチャーを読み込む
this.arrowTexture = this.Content.Load<Texture2D>("Arrow");
// テクスチャーの中心座標
this.textureCenter = new Vector2(this.arrowTexture.Width / 2,
this.arrowTexture.Height / 2);
}
<summary>
ゲームが終了するときに一回だけ呼ばれ
すべてのゲームコンテンツをアンロードします
</summary>
protected override void UnloadContent()
{
// TODO: ContentManager で管理されていないコンテンツを
// ここでアンロードしてください
}
<summary>
描画以外のデータ更新等の処理を行うメソッド
主に入力処理、衝突判定などの物理計算、オーディオの再生など
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Update(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
// Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
// ゲームを終了させます
if (gamePadState.Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
// キーボードによる回転
if (keyboardState.IsKeyDown(Keys.Left))
{
this.degree -= (float)gameTime.ElapsedGameTime.TotalMilliseconds / 5.0f;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
this.degree += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 5.0f;
}
// ゲームパッドによる回転
if (gamePadState.IsConnected)
{
this.degree += gamePadState.ThumbSticks.Left.X *
(float)gameTime.ElapsedGameTime.TotalMilliseconds / 5.0f;
}
// マウスによる回転
if (mouseState.LeftButton == ButtonState.Pressed)
{
this.degree += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 5.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.Draw(this.arrowTexture, new Vector2(150.0f, 150.0f), Color.Yellow);
// 回転テクスチャー
this.spriteBatch.Draw(this.arrowTexture,
new Vector2(150.0f, 150.0f) + this.textureCenter,
null, Color.Red, MathHelper.ToRadians(this.degree), this.textureCenter,
1.0f, SpriteEffects.None, 0.0f);
// Degree と Radian
this.spriteBatch.DrawString(this.font,
"Degree : " + this.degree.ToString() + Environment.NewLine +
"Radian : " + MathHelper.ToRadians(this.degree).ToString(),
new Vector2(30.0f, 30.0f), Color.White);
// スプライトの一括描画
this.spriteBatch.End();
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}