문제
Box2d (모든 객체의 x 좌표는 0 <x <1000 (say) 인 경우)로 끝없는 랩핑 세계를 만들어야합니다. 나는 순간 이동 물체를 앞뒤로 옮긴 게임을 연주했지만 더 나은 방법이있을 것 같은 느낌이 들었습니다. 아이디어가 있습니까? 객체 (또는 링크 된 객체의 체인)의 x 스팬은 약 50을 초과하지 않습니다. 예를 들어 화면 너비보다 적습니다.
카메라는 한 번에 전 세계의 작은 부분 만 볼 수 있습니다 (폭은 약 5%, 높이 100% - 세계의 높이는 약 30 x 1000).
건배.
해결책
나는 다음을 구현했습니다. 이것은 결코 이상적이지만 내 목적에 적합한 것은 아닙니다. 많은 제한 사항이 관련되어 있으며 진정한 포장 세계는 아니지만 충분합니다.
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