2009-05-19 9 views
8

Je dois créer un monde sans fin avec Box2D (où la coordonnée X de tous les objets est 0 < X < 1000 (disons)). J'ai joué à des jeux avec des objets téléportés d'avant en arrière, mais on a l'impression qu'il pourrait y avoir un meilleur moyen - des idées? Aucun objet (ou chaîne d'objets liés) n'aura une portée X supérieure à 50, par exemple inférieure à la largeur de l'écran. La caméra ne peut voir qu'une petite partie du monde à la fois (environ 5% de largeur, 100% de hauteur - le monde est d'environ 30 de haut sur 1000 de large).Comment créer un monde d'emballage dans Box2D

Cheers.

+1

Etes-nous en Flash ici? Veuillez ajouter l'étiquette de langue appropriée afin que les gens puissent trouver la question plus facilement. –

+0

Box2D est une bibliothèque C++ avec un backend OpenGL pour développer des jeux 2D ou des simulations. –

+0

J'utilise actuellement le port C# mais je ne pense pas que la solution soit spécifique à la langue –

Répondre

0

J'ai implémenté ce qui suit, ce qui n'est pas du tout idéal mais qui correspond à mon objectif. Il y a beaucoup de limites impliquées et ce n'est pas un vrai monde d'emballage mais c'est assez bon.

public void Wrap() 
    { 
     float tp = 0; 

     float sx = ship.GetPosition().X;   // the player controls this ship object with the joypad 

     if (sx >= Landscape.LandscapeWidth())  // Landscape has overhang so camera can go beyond the end of the world a bit 
     { 
      tp = -Landscape.LandscapeWidth(); 
     } 
     else if (sx < 0) 
     { 
      tp = Landscape.LandscapeWidth(); 
     } 

     if (tp != 0) 
     { 
      ship.Teleport(tp, 0);     // telport the ship 

      foreach (Enemy e in enemies)   // Teleport everything else which is onscreen 
      { 
       if (!IsOffScreen(e.bodyAABB))  // using old AABB 
       { 
        e.Teleport(tp, 0); 
       } 
      } 
     } 

     foreach(Enemy e in enemies) 
     { 
      e.UpdateAABB();       // calc new AABB for this body 

      if (IsOffScreen(g.bodyAABB))   // camera has not been teleported yet, it's still looking at where the ship was 
      { 
       float x = e.GetPosition().X; 

       // everything which will come onto the screen next frame gets teleported closer to where the camera will be when it catches up with the ship 

       if (e.bodyAABB.UpperBound.X < 0 || e.bodyAABB.LowerBound.X + Landscape.LandscapeWidth() <= cameraPos.X + screenWidth) 
       { 
        e.Teleport(Landscape.LandscapeWidth(), 0); 
       } 
       else if (e.bodyAABB.LowerBound.X > Landscape.LandscapeWidth() || e.bodyAABB.UpperBound.X - Landscape.LandscapeWidth() >= cameraPos.X - screenWidth) 
       { 
        e.Teleport(-Landscape.LandscapeWidth(), 0); 
       } 
      } 
     } 
    } 
Questions connexes