简体   繁体   中英

Java 2D Sphere Scrolling

So I have this randomly generated set of tiles that is wrapped in a circle and I'm not really sure how to scroll it around the circle. Basically it's a side-view planet that is in 2D and needs to be wrapped and moving at a controllable rate to give the illusion of planet rotation. I'll post my current render code below so you can get an idea of what I'm working with but I'm not really sure what to do to the x and y to make it scroll around. Here's what a planet looks like: https://imgur.com/Ytdz2mG

for (int x = 0; x < planet1.length; x++)
    {
        for (int y = 0; y < planet1[0].length; y++)
        {
            if (planet1[x][y] == 1 || planet1[x][y] == 2)
            {
                g.drawImage(water, x * 32, y * 32);
            } 
            else if (planet1[x][y] == 3)
            {
                g.drawImage(desert, x * 32, y * 32);
            }
            else if (planet1[x][y] == 4)
            {
                g.drawImage(plains, x * 32, y * 32);
            }
            else if (planet1[x][y] == 5)
            {
                g.drawImage(grassland, x * 32, y * 32);
            }
            else if (planet1[x][y] == 6)
            {
                g.drawImage(forest, x * 32, y * 32);
            }
            else if (planet1[x][y] == 7)
            {
                g.drawImage(hills, x * 32, y * 32);
            }
            else if (planet1[x][y] == 8)
            {
                g.drawImage(mountain, x * 32, y * 32);
            }
            else if (planet1[x][y] == 9)
            {
                g.drawImage(mountain, x * 32, y * 32);
            }
            else if (planet1[x][y] == -1)
            {

            }
        }
    }

From the look of it you aren't drawing a sphere, rather a repeating rectangle clipped to a circle.

If you want it to actually appear to rotate, they you'll need make an orthographic projection of a sphere.
In which case, you'll need your tile data stored over θ,φ, and then create a constant map of on-screen xy into angles using φ= acos(length(x,y)/radius) θ= atan(y/x)
And then sample from the terrain data using [θ+roll,φ+pitch] (not exactly roll and pitch, but close enough)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM