Question

Je dois créer un monde d'emballage sans fin avec Box2D (où la coordonnée X de tous les objets est 0

La caméra peut voir qu'une petite partie du monde à la fois (environ 5% de la largeur, hauteur 100% - le monde est d'environ 30 élevée par 1000 large)

.

Vive.

Était-ce utile?

La solution

Je l'ai mis en œuvre ce qui suit, ce qui est loin d'être idéal, mais propre à mon but. Il y a beaucoup de limitations impliquées et ce n'est pas un vrai monde d'emballage, mais il 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);
                }
            }
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top