2009-05-19 3 views
8

Box2D(すべてのオブジェクトのX座標が0 < X <(たとえば))という無限のラップ世界を作成する必要があります。オブジェクトを前後にテレポートすることでいくつかのゲームをしましたが、より良い方法があるように感じられます。オブジェクト(またはリンクされたオブジェクトのチェーン)には、約50以上のXスパン、たとえば画面の幅より小さいものはありません。Box2Dでラップ世界を作成する方法

カメラは一度に世界のわずかな部分しか見ることができません(幅約5%、高さ100% - 世界は高さ約30×高さ1000)。

乾杯。

+1

ここにはFlashがありますか?適切な言語タグを追加して、人々が質問をより簡単に見つけられるようにしてください。 –

+0

Box2Dは、2Dゲームやシミュレーションを開発するためのOpenGLバックエンドを備えたC++ライブラリです。 –

+0

私は実際にC#ポートを使用していますが、ソリューションが言語固有のものではないと思います –

答えて

0

私は以下を実装しました。それは決して理想的ではありませんが、自分の目的に合っています。多くの制限があり、それは本当のラッピング世界ではありませんが、十分です。

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); 
       } 
      } 
     } 
    } 
関連する問題