如何在Box2D中创建一个包裹世界

8
我需要使用Box2D创建一个无限循环的世界(所有物体的X坐标为0 < X < 1000(例如))。我曾经尝试过将物体来回传送,但感觉可能有更好的方法 - 有什么想法吗?没有任何对象(或一系列链接的对象)的X跨度超过约50,即屏幕宽度以下。
相机每次只能看到世界的一小部分(大约5%的宽度,100%的高度 - 该世界大约为30高,1000宽)。
谢谢。

1
我们在使用Flash吗?请添加适当的语言标签,以便人们更容易地找到这个问题。 - Michael Myers
Box2D是一个C++库,具有OpenGL后端,用于开发2D游戏或模拟。 - Mike Dinescu
我实际上在使用C#端口,但我认为解决方案不会与语言有关。 - Charlie Skilbeck
1个回答

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);
                }
            }
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接