Example #1
0
void Camera::Update()
{
  //float dt = TheTimer::Instance()->GetDt();
  Vec3f pos;

  if (m_target)
  {
    pos = m_target->GetPos();

    // yRotAuto = 0;
#ifdef AUTO_ROTATE
    // Swing behind target
    float dir = m_target->GetDir() + 180.0f;
    float angleDiff = yRotAuto - dir;

    // Rotate to face m_dir, taking the shortest route (CW or CCW)
    if (fabs(angleDiff) < 0.1f) // TODO CONFIG
    {
      yRotAuto  = dir;
    }
    else
    {
      float ROT_SPEED = 1.0f; // TODO CONFIG
      if (yRotAuto > dir)
      {
        yRotAuto -= ROT_SPEED * dt * fabs(angleDiff);
      }
      else if (yRotAuto < dir)
      {
        yRotAuto += ROT_SPEED * dt * fabs(angleDiff);
      }
    }
#endif // AUTO_ROTATE

#ifdef PORTAL_ROTATE
    // TODO Get closest portal
    static PGameObject lastportal = 0;
    // We want to get the closest portal to the target, even if they have never intersected it.
    int pid = m_target->GetIgnorePortalId();
    if (pid > -1)
    {
      lastportal = TheGame::Instance()->GetGameObject(pid);
    }
    else
    {
      // Look for portals in this room, find closest.
      // TODO Optimise
      // just check periodically ?
      Portals portals = GetPortals();
      float bestSqDist = 999999.9f;
      for (Portals::iterator it = portals.begin(); it != portals.end(); ++it)
      {
        Portal* p = *it;
        float sqDist = (p->GetPos() - pos).SqLen();
        if (sqDist < bestSqDist)
        {
          bestSqDist = sqDist;
          lastportal = p;
        }
      } 
    }
    
    if (lastportal)
    {
      float pdist = (pos - lastportal->GetPos()).SqLen(); // sq dist from portal to player
      static const float MAX_DIST = ROConfig()->GetFloat("portal-max-dist", 200.0f); 
      static const float MAX_SQ_DIST = MAX_DIST * MAX_DIST; 
      if (pdist < MAX_SQ_DIST) 
      {
        yRotAuto = atan2(-pos.x, pos.z) * (1.0f - pdist / MAX_SQ_DIST); 

//std::cout << "SQ Dist from portal: " << pdist << " pos.z=" << pos.z << " pos.x=" << pos.x << " yRotAuto degs=" << RadToDeg(yRotAuto) << "\n";
      }
    }
#endif // PORTAL_ROTATE
  }
  else
  {
    pos = posNoTarget; 
  }
  pos += posOffset;

  float y = DegToRad(yRotAuto + yRotUser); 

  SetEyePos(Vec3f(
    pos.x + sin(y) * cos(xRot) * zDist, 
    pos.y + sin(xRot) * zDist, 
    pos.z + cos(y) * cos(xRot) * zDist));

  SetLookAtPos(pos);
}