This is a method from 'THECH3RN0' off of youtube, he used this in exactly what you want..
(Mentioned Notch alot during his video, saying that these methods are not ''exactly'' the same as his, but it's a Distant Graphic algorithm of some type.
Basicly using nothing but pixels, creating a 3D looking envionment/movement, while using no Graphics2D.
Code Snippet: (randomly generating 8x8 walls)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| Level level = game.level; int size = 10; for (int xBlock = -size; xBlock <= size; xBlock++) { for (int zBlock = -size; zBlock <= size; zBlock++) { Block block = level.create(xBlock, zBlock); Block east = level.create(xBlock + 1, zBlock); Block south = level.create(xBlock, zBlock + 1); if (block.solid) { if (!east.solid) { renderWall(xBlock + 1, xBlock + 1, zBlock, zBlock + 1, 0); } if (!south.solid) { renderWall(xBlock + 1, xBlock, zBlock + 1, zBlock + 1, 0); } } else { if (east.solid) { renderWall(xBlock + 1, xBlock + 1, zBlock + 1, zBlock, 0); } if (south.solid) { renderWall(xBlock, xBlock + 1, zBlock + 1, zBlock + 1, 0); } } } |
The Level class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class Level {
public final int width, height; public Block[] blocks;
public Level(int width, int height) { this.width = width; this.height = height; blocks = new Block[width * height]; Random random = new Random(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Block block = null; if (random.nextInt(4) == 0) { block = new SolidBlock(); } else { block = new Block(); } blocks[x + y * width] = block; } } }
public Block create(int x, int y) { if (x < 0 || y < 0 || x >= width || y >= height) { return Block.solidWall; } return blocks[x + y * width]; }
} |
Now..
I really didn't want to post this next snippet after looking at it because it's so large..
But here's the method renderWall():
(TONS of fun math XD)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| public void renderWall(double xLeft, double xRight, double zDistanceLeft, double zDistanceRight, double yHeight) {
double upCorrect = 0.0625; double rightCorrect = 0.0625; double forwardCorrect = 0.0625; double walkCorrect = -0.0625;
double xcLeft = ((xLeft / 2) - (right * rightCorrect)) * 2; double zcLeft = ((zDistanceLeft / 2) - (forward * forwardCorrect)) * 2;
double rotLeftSideX = xcLeft * cosine - zcLeft * sine; double yCornerTL = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2; double yCornerBL = ((+0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2; double rotLeftSideZ = zcLeft * cosine + xcLeft * sine; double xcRight = ((xRight / 2) - (right * rightCorrect)) * 2; double zcRight = ((zDistanceRight / 2) - (forward * forwardCorrect)) * 2; double rotRightSideX = xcRight * cosine - zcRight * sine; double yCornerTR = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2; double yCornerBR = ((+0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2; double rotRightSideZ = zcRight * cosine + xcRight * sine; double tex30 = 0; double tex40 = 8; double clip = 0.5;
if (rotLeftSideZ < clip && rotRightSideZ < clip) { return; } if (rotLeftSideZ < clip) { double clip0 = (clip - rotLeftSideZ) / (rotRightSideZ - rotLeftSideZ); rotLeftSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ) * clip0; rotLeftSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX) * clip0; tex30 = tex30 + (tex40 - tex30) * clip0; }
if (rotRightSideZ < clip) { double clip0 = (clip - rotLeftSideZ) / (rotRightSideZ - rotLeftSideZ); rotRightSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ) * clip0; rotRightSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX) * clip0; tex40 = tex30 + (tex40 - tex30) * clip0; }
double xPixelLeft = (rotLeftSideX / rotLeftSideZ * height + width / 2); double xPixelRight = (rotRightSideX / rotRightSideZ * height + width / 2); if (xPixelLeft >= xPixelRight) { return; }
int xPixelLeftInt = (int) (xPixelLeft); int xPixelRightInt = (int) (xPixelRight);
if (xPixelLeftInt < 0) { xPixelLeftInt = 0; }
if (xPixelRightInt > width) { xPixelRightInt = width; }
double yPixelLeftTop = yCornerTL / rotLeftSideZ * height + height / 2.0; double yPixelLeftBottom = yCornerBL / rotLeftSideZ * height + height / 2.0; double yPixelRightTop = yCornerTR / rotRightSideZ * height + height / 2.0; double yPixelRightBottom = yCornerBR / rotRightSideZ * height + height / 2.0;
double tex1 = 1 / rotLeftSideZ; double tex2 = 1 / rotRightSideZ; double tex3 = tex30 / rotLeftSideZ; double tex4 = tex40 / rotRightSideZ - tex3; for (int x = xPixelLeftInt; x < xPixelRightInt; x++) { double pixelRotation = (x - xPixelLeft) / (xPixelRight - xPixelLeft); double zWall = (tex1 + (tex2 - tex1) * pixelRotation); if (zBufferWall[x] > zWall) { continue; } zBufferWall[x] = zWall; int xTexture = (int) ((tex3 + tex4 * pixelRotation) / zWall); double yPixelTop = yPixelLeftTop + (yPixelRightTop - yPixelLeftTop) * pixelRotation; double yPixelBottom = yPixelLeftBottom + (yPixelRightBottom - yPixelLeftBottom) * pixelRotation; int yPixelTopInt = (int) (yPixelTop); int yPixelBottomInt = (int) (yPixelBottom);
if (yPixelTopInt < 0) { yPixelTopInt = 0; } if (yPixelBottomInt > height) { yPixelBottomInt = height; } for (int y = yPixelTopInt; y < yPixelBottomInt; y++) { double pixelRotationY = (y - yPixelTop) / (yPixelBottom - yPixelTop); int yTexture = (int) (8 * pixelRotationY); pixels[x + y * width] = Texture.fill.pixels[(xTexture & 7) + (yTexture & 7) * 8]; zBuffer[x + y * width] = 1 / (tex1 + (tex2 - tex1) * pixelRotation) * 8; } } }
|
Hope it helps you out some mate.