3D Game Programming Error

I’m following this video : http://www.youtube.com/watch?v=RKPEQfkhbAY&feature=channel and when I was done with it, I ran the program and got this: http://imgur.com/eammB It should look staright without the zig zag lines. Here is all of my code:

package me.kenneth.game;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import me.kenneth.graphics.Screen;

import java.awt.image.BufferStrategy;
import java.awt.image.DataBufferInt;

public class Window extends Canvas implements Runnable {
	
	public static final long serialVersionUID = 1L;
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	public static final String TITLE = "MineGame Pre-Alpha 0.2";
	
	private Thread thread;
	private boolean running = false;
	private BufferedImage img;
	private Screen render;
	private int pixels[];
	private Game game;
	
	public Window() {
		Dimension dim = new Dimension(WIDTH, HEIGHT);
		setPreferredSize(dim);
		setMinimumSize(dim);
		setMaximumSize(dim);
		render = new Screen(WIDTH, HEIGHT);
		img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
		game = new Game();
	}
	
	private void start() {
		if (running) return;
		
		running = true;
		thread = new Thread(this);
		thread.start();
	}
	
	private void stop() {
		
		if (!running) return;
		
		running = false;
		try {
			thread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
	}
	
	public void run() {
		
		int frames = 0;
		double unprocessedSeconds = 0;
		long previousTime = System.nanoTime();
		double secondsPerTick = 1 / 60.0;
		int tickCount = 0;
		boolean ticked = false;
		
		while (running) {
			
			long currentTime = System.nanoTime();
			long passedTime = currentTime - previousTime;
			previousTime = currentTime;
			unprocessedSeconds += passedTime / 1000000000.0;
			
			while (unprocessedSeconds > secondsPerTick) {
				tick();
				unprocessedSeconds -= secondsPerTick;
				ticked = true;
				tickCount++;
				if (tickCount % 60 == 0) {
					System.out.println(frames + "fps");
					previousTime += 1000;
					frames = 0;
				}
			}
			
			render();
			frames++;
			
		}
		
		Toolkit.getDefaultToolkit().sync();
		
	}
	
	private void tick() {
		
		game.tick();
		
	}
	
	private void render() {
		
		BufferStrategy bs = this.getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(3);
			return;
		}
		
		render.render(game);
		
		for (int i = 0; i < WIDTH * HEIGHT; i++) {
			pixels[i] = render.pixels[i];
		}
		
		Graphics g = bs.getDrawGraphics();
		g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
		g.dispose();
		bs.show();
		
	}
	
	public static void main(String[] args) {
		
		Window game = new Window();
		JFrame frame = new JFrame(TITLE);
		frame.add(game);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.pack();
		
		game.start();
		
	}

}

package me.kenneth.game;

public class Game {
	
	public int time;
	
	public Game() {
		time = 0;
	}
	
	public void tick() {
		time += 2;
	}

}

package me.kenneth.graphics;

import me.kenneth.game.Window;

public class Render {
	
	public final int WIDTH;
	public final int HEIGHT;
	public final int[] pixels;
	
	public Render(int width, int height) {
		
		WIDTH = width;
		HEIGHT = height;
		pixels = new int[width * height];
		
	}
	
	public void draw(Render render, int xOffset, int yOffset) {
		
		for (int y = 0; y < render.HEIGHT; y++) {
			int yPix = y + yOffset;
			if (yPix < 0 || yPix >= Window.HEIGHT) continue;
			for (int x = 0; x < render.WIDTH; x++) {
				int xPix = x + xOffset;
				if (xPix < 0 || xPix >= Window.WIDTH) continue;
				
				int alpha = render.pixels[x + y + render.WIDTH];
				
				if (alpha > 0) {
				
				pixels[xPix + yPix * WIDTH] = render.pixels[x + y * render.WIDTH];
				
				}
			}
		}
		
	}

}

package me.kenneth.graphics;

import java.util.Random;

import me.kenneth.game.Game;

public class Screen extends Render {

	private Render test;
	private Render3D render3D;

	public Screen(int width, int height) {
		super(width, height);
		Random random = new Random();
		render3D = new Render3D(WIDTH, HEIGHT);
		test = new Render(256, 256);
		for (int i = 0; i < 256 * 256; i++) {

			test.pixels[i] = random.nextInt() * (random.nextInt(5) / 4);

		}
	}

	public void render(Game game) {
		for (int i = 0; i < WIDTH * HEIGHT; i++) {
			pixels[i] = 0;
		}

		for (int i = 0; i < 50; i++) {
			int anim = (int) (Math.sin((game.time + i * 2) % 1000.0 / 100) * 100);
			int anim1 = (int) (Math.cos((game.time + i * 2) % 1000.0 / 100) * 100);

		}
		
		render3D.floor();
		draw(render3D, 0, 0);

	}

}

package me.kenneth.graphics;

public class Render3D extends Render {

	public Render3D(int width, int height) {
		super(width, height);
	}
	
	public void floor() {
		
		for (int y = 0; y < HEIGHT; y++) {
			double yDepth = y - HEIGHT / 2.4;
			double z = 100.0 / yDepth;
			for (int x = 0; x < WIDTH; x++) {
				double xDepth = x - WIDTH / 2;
				xDepth *= z;
				int xx = (int) xDepth & 5;
				pixels[x + y * WIDTH] = xx * 128;
			}
		}
		
	}

}

Is it something wrong in the Render3D class?

In the video, he did “(int) xDepth & 15”, not “& 5”. Try to double check your code before posting immediately for help :cranky:

Sorry ha ha. I tripple checked the video and I really thought it said & 5.

He actually wrote & 5 and not &15. I copied him exactly and it has a 3D effect but there are diagonal lines going through everything so it kind of ruins the effect.

& 15 is masking off the low four bits. & 5 just doesn’t make sense for anything I can think of other than maybe checking two flags (1 and 4). You should always be keeping powers of two in mind when doing bitwise arithmetic, and more generally you should understand why code is written the way it is rather than blindly copying it.