Search code examples
c#c#-4.0mathxnaxna-4.0

c# / XNA 4.0- Move textures to give illusion of infinite plane


I've been having trouble implementing an algorithm to shift my textures given their positions and the cameras position. The first two pictures in the image explain what I'm trying to accomplish, but I can't figure out how to move them accordingly. I had created a program once upon a time that did this, but I've gone and lost it. Any ideas?

If it helps any, the Cameras/Viewports width and height are the same as the textures' width and height. The goal is the get them to shift positions, giving the illusion of an infinite plane. (With out having to draw an infinite plane, lol.)

The first two examples:


Solution

  • After a couple hours of trial and error, I finally figured out how to get the regions/textures/rectangles to move accordingly. For those who want the solution,

            if ((int)Math.Abs(region.X - camPos.X) > region.Width * 2)
            {
                region.X += region.Width * 2;
            }
    
            if (camPos.X < region.X - region.Width)
            {
                region.X -= region.Width * 2;
            }
    
            if ((int)Math.Abs(region.Y - camPos.Y) > region.Height * 2)
            {
                region.Y += region.Height * 2;
            }
    
            if (camPos.Y < region.Y - region.Height)
            {
                region.Y -= region.Height * 2;
            }
    

    Where camPos is the camera position, and region is the region/texture/rectangle/whatever.

    This code works for a 4 square region (2 regions by 2 regions). To change for more regions, simply change all the *2s to *3s or *4s for a 9 square region and 16 square region, respectively.