Crystal Space 2.0.0: 4.2.3.3 Locating the Camera

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### 4.2.3.3 Locating the Camera

`‘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:

      ...
      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.

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.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated using texi2html 1.76.