【问题标题】:How to create a wrapping world in Box2D如何在 Box2D 中创建一个环绕世界
【发布时间】:2009-05-19 15:42:33
【问题描述】:

我需要用 Box2D 创建一个无限的环绕世界(其中所有对象的 X 坐标为 0

相机一次只能看到世界的一小部分(大约 5% 的宽度,100% 的高度 - 世界大约是 30 高乘以 1000 宽)。

干杯。

【问题讨论】:

  • 我们是在 Flash 中吗?请添加适当的语言标签,以便人们更容易找到问题。
  • Box2D 是一个带有 OpenGL 后端的 C++ 库,用于开发 2D 游戏或模拟。
  • 我实际上在使用 C# 端口,但我认为解决方案不是特定于语言的

标签: box2d


【解决方案1】:

我已经实现了以下,这绝不是理想的,但适合我的目的。涉及很多限制,这不是一个真正的包装世界,但已经足够了。

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

【讨论】:

    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多