题
我需要创建与box2d的无端缠绕世界(其中X的所有对象的坐标为0 相机可以在一个时间只能看到世界的一小部分(约5%的宽度,100%高度 - 世界是由1000宽约30高) 干杯。
解决方案
我已经实现了以下内容,这绝不是理想的,但适合我的目的。它牵涉到很多局限性,它不是一个真正的世界包装,但它就好了。
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);
}
}
}
}
不隶属于 StackOverflow