4.2.3.3 カメラの配置


4.2.3.3 カメラの配置

`flarge' contains a number of sectors and various objects, and those are now all loaded in memory. However, this is not enough. We also have to set the camera to some sector and position in that world. In the previous tutorials we simply used the sector that we just created and a fixed position in that sector. (Keep in mind that in Crystal Space a position in space is always defined as a sector in combination with a position.) When loading a map we can't work that way because we don't know which sectors are in the map (unless we make an application that can only read one map file, but that's not very flexible) and we also don't know where we can safely put our camera. In the map files it is possible to specify one or more starting positions. We will query the engine for such a starting position and initialize our view (camera) to that. This happens with the following code which you should add just after engine->Prepare() in the Application() method:

`flarge`は、多くのセクタと様々なオブジェクトを含んでいます。今それらはメモリ上に読み込まれています。しかし、十分ではありません。私たちは、ワールド内のいくつかのセクタと位置のために、カメラもセットしなければなりません。前のチュートリアルでは、単に作成したセクタを使用し、カメラは固定の位置で使用しました(覚えておいて下さい。Crystal Space内で、空間の位置は、セクタと組み合わせられた位置として定義されています)。マップを読み込んだとき、このように動かすことができません。なぜなら、マップ内のどのセクタにあるか知らないからです(もし、1つのマップしか読み込まないアプリケーションを作成したのでなければです。1つのマップしか読み込まないアプリケーションは、柔軟性がありません)。また、カメラを安全に配置することができる場所も知りません。マップファイルの中では、1つ以上の開始位置を指定することが可能です。私たちは、そのため1つの開始位置でエンジンを実行し、私達のView(カメラ)を初期化します。これは、Application()メソッドの中の、engine->Prepare()の前に追加する次のコードで起こります。

 ...
 engine->Prepare ();

 // Find the starting position in this level.
 csVector3 pos (0, 0, 0);
 if (engine->GetCameraPositions ()->GetCount () > 0)
 {
   // There is a valid starting position defined in the level file.
   iCameraPosition* campos = engine->GetCameraPositions ()->Get (0);
   room = engine->GetSectors ()->FindByName (campos->GetSector ());
   pos = campos->GetPosition ();
 }
 else
 {
   // We didn't find a valid starting position. So we default
   // to going to room called 'room' at position (0,0,0).
   room = engine->GetSectors ()->FindByName ("room");
   pos = csVector3 (0, 0, 0);
 }
 if (!room)
   ReportError("Can't find a valid starting position!");

 view->GetCamera ()->SetSector (room);
 view->GetCamera ()->GetTransform ().SetOrigin (pos);

 // Initialize our collider actor.
 collider_actor.SetCollideSystem (cdsys);
 collider_actor.SetEngine (engine);
 csVector3 legs (.2f, .3f, .2f);
 csVector3 body (.2f, 1.2f, .2f);
 csVector3 shift (0, -1, 0);
 collider_actor.InitializeColliders (view->GetCamera (),
 	legs, body, shift);

 Run ();
 ...
}

First, we see how many camera positions there were defined in the map by using iEngine::GetCameraPositionCount?(). If there are none, then the map didn't define a starting position. In that case we will assume there is a sector called 'room' and we will start the camera at (0,0,0) in that sector. Otherwise we will use the first starting position defined in the map.

まず、iEngine::GetCameraPositionCount?()を使用し、マップ内にいくつのカメラ位置が定義されているかを確認します。もし0個であれば、マップは開始位置を定義していません。この場合、`room`と呼ばれるセクタがあり、カメラは`room`セクタの(0,0,0)を開始位置とすると仮定します。その他の場合、マップで定義された第1の開始位置を使用します。

Once we know our position in the sector, we are also able to initialize the camera's collision data, as shown in the code above.

一度、セクタ内の自分の位置を知ると、上記のコードの中で見られるような、カメラの衝突データを初期化することができます。

That's it. After adding this code this application will now load the `flarge' map and display it so you can explore the map.

これで終わりです。このコードを追加した後は、このアプリケーションは`flarge`マップを読み込み、表示します。そして、あなたは、マップを探索することができます。