フォーメーションオプション



これはグラディウスIIIにおけるフォーメーションオプションですが、このように、隊列を組んだり、プレイヤーと一定の位置関係を保ったまま移動したりするというのは、ゲームではよくある処理ですね。

基本のプログラム


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Formation
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        // プレイヤー移動速度
        const float PlayerSpeed = 5.0f;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // ぱっちぃのテクスチャ
        Texture2D texturePacchi;
        // かまトゥのテクスチャ
        Texture2D textureKamatoo;
        // プレイヤーの位置
        Vector2 playerPosition = new Vector2(200, 200);
        // オプションの位置
        Vector2 option0Position;
        Vector2 option1Position;
        Vector2 option2Position;
        Vector2 option3Position;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            // 画像を読み込む
            texturePacchi = Content.Load<Texture2D>("pacchi_32");
            textureKamatoo = Content.Load<Texture2D>("kamatoo_32");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            // 上下左右が押されたらプレイヤーを移動させる
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                playerPosition.X -= PlayerSpeed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                playerPosition.X += PlayerSpeed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                playerPosition.Y -= PlayerSpeed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                playerPosition.Y += PlayerSpeed;
            }

            // オプションの位置を更新する。番号と位置の対応は以下の通り↓
            //
            //          (P)
            //      (0)     (1)
            //  (2)             (3)
            //
            option0Position = playerPosition + new Vector2(-50, 25);
            option1Position = playerPosition + new Vector2(50, 25);
            option2Position = playerPosition + new Vector2(-100, 50);
            option3Position = playerPosition + new Vector2(100, 50);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            spriteBatch.Begin();

            // オプションを描画
            spriteBatch.Draw(textureKamatoo, option0Position, Color.White);
            spriteBatch.Draw(textureKamatoo, option1Position, Color.White);
            spriteBatch.Draw(textureKamatoo, option2Position, Color.White);
            spriteBatch.Draw(textureKamatoo, option3Position, Color.White);

            // プレイヤーを描画
            spriteBatch.Draw(texturePacchi, playerPosition, Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

何も難しい箇所は無いと思います。
ただ単に、プレイヤーの位置から少しずらした場所にオプションを配置しているだけです。

さて、実行してみると、どうでしょうか?



・・・たしかに隊列は組んでいるのですが、動きが硬いです。
グラディウスIIIのものと比べてみましょう。



グラディウスIIIでは、オプションが少し遅れてヌルっとついてきますね。どちらがカッコいいかは、言うまでもないですね。


ヌルっとついてこさせる

ヌルっと遅れてついてこさせるには、Lerp(ラープ)という定番のテクニックを使います。

LerpというのはLinear Interpolationの略で、日本語にすると線形補間です。
ものすごく難しそうな響きですが、実際は単純で、2つの値を混ぜてくれる関数に過ぎません。2つの値と混ぜる割合を指定すると、結果を返してくれます。

変な例えですみませんが、例えば、母親の頭の良さが100で、父親の頭の良さが200だったとします。
長男は100%父親の血を受け継いだとします。そしたら頭の良さは200ですね。
次男は0%父親の血を受け継いだ(つまり100%母親)とします。そしたら頭の良さは100ですね。
三男は50%父親の血を受け継いだとします。そしたら頭の良さは150ですね。
(実際の遺伝はそんな単純な話ではないですが・・・)
そういう計算をしてくれるのがLerp関数です。

Lerp関数は大抵引数が3つで、上記でいうところの母親の値、父親の値、混ぜる割合を指定します。もちろんこの程度の計算は自力でも簡単にできるでしょうが、せっかく用意されているので、普通はLerp関数を使います。

で、このLerp関数はゲームプログラミングのいたるところで使うのですが、今回のように、ヌルっと動かしたいときにも有用です。

これは、文章で説明されるより、コードと結果を見たほうが良いでしょう。
オプションの位置を更新する処理を、次のように書き換えてください。
            // オプションの位置を更新する。番号と位置の対応は以下の通り↓
            //
            //          (P)
            //      (0)     (1)
            //  (2)             (3)
            //
            option0Position = Vector2.Lerp(option0Position, playerPosition + new Vector2(-50, 25), 0.2f);
            option1Position = Vector2.Lerp(option1Position, playerPosition + new Vector2(50, 25), 0.2f);
            option2Position = Vector2.Lerp(option2Position, playerPosition + new Vector2(-100, 50), 0.1f);
            option3Position = Vector2.Lerp(option3Position, playerPosition + new Vector2(100, 50), 0.1f);

これだけです。たったこれだけで、ヌルっとします。




一体なぜヌルっとしたのでしょうか。

上記のコードでは、オプションの現在位置と、目標位置をLerpで混ぜています。
オプションの0番の処理に注目してみます。Lerpの第1引数に現在位置、第2引数に目標位置、そして第3引数に0.2を指定しています。これは、現在位置から目標位置に20%だけ近づいた場所を表します。
つまり、オプションの0番は、毎フレーム、20%ずつ目標位置に近づきます。
目標位置に近づくほど、移動すべき距離が短くなっていくため、徐々に減速し、ヌルっとした動きになるのです。

オプションの2番ないし3番の処理では、Lerpの第3引数に0.1を指定しています。これは、現在位置から目標位置に10%だけ近づいた場所を表します。つまり、オプションの2番および3番は、毎フレーム10%ずつ目標位置に近づきます。これにより、0番と1番よりも2番3番のほうが、ゆっくり、もっさりとした動きになって味が出ます。



練習問題

以下にチャレンジ!
  • フォーメーションを変えてみる
  • Lerpの第3引数を変えてみる
  • オプションの個数を変えてみる




0 件のコメント:

コメントを投稿