Results 1 to 1 of 1
  1. #1
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default 3d wall not rendering correctly

    I am making a game based off of a tutorial on YouTube, and I watched the video a few times and my code looks the same to me. The "wall" is always facing the camera as if it were a sprite, although it shouldn't. It should be facing the way it always is. Can someone check my code to see if anything is wrong with it?


    Here are the variables defined before the method(I didn't want to show the whole class; it's long and I don't want you to read unnecessary code.);

    Java Code:
    public double[] zBuffer;
    	public double renderDistance = 6250;
    
    	private double forward, right, up, cosine, sine;
    
    	public Render3D(int width, int height) {
    		super(width, height);
    		zBuffer = new double[width * height];
    	}
    
    
    		forward = game.controls.z;
    		right = game.controls.x;
    		up = game.controls.y;
    		double floorPosition = 8;
    		double ceilingPosition = 8;
    		double walking = Math.sin(game.time / 8.0) * 0.8;
    
    		double rotation = //Math.sin(game.time / 40.0) * 0.5;
    		game.controls.rotation;
    		cosine = Math.cos(rotation);
    		sine = Math.sin(rotation);
    		if (Controller.crouchWalk) {
    			walking = Math.sin(game.time / 6.0) * 0.25;
    		}
    		if (Controller.runWalk) {
    			walking = Math.sin(game.time / 4.5) * 1;
    		}
    Here is where the actual wall rendering happens.
    Java Code:
    public void renderWall(double xLeft, double xRight, double zDistance, double yHeight) {
    		double xcLeft = ((xLeft) - right) * 2;
    		double zcLeft = ((zDistance) - forward) * 2;
    
    		double rotLeftSideX = xcLeft * cosine - zcLeft * sine;
    		double yCornerTL = ((-yHeight) - up) * 2;
    		double yCornerBL = ((+0.5 - yHeight) - up) * 2;
    		double rotLeftSideZ = zcLeft * cosine + xcLeft * sine;
    
    		double xcRight = ((xRight) - right) * 2;
    		double zcRight = ((zDistance) - forward) * 2;
    
    		double rotRightSideX = xcRight * cosine - zcRight * sine;
    		double yCornerTR = ((-yHeight) - up) * 2;
    		double yCornerBR = ((+0.5 - yHeight) - up) * 2;
    		double rotRightSideZ = zcRight * cosine + xcRight * sine;
    
    		double xPixelLeft = (rotLeftSideX / rotLeftSideZ * height + width / 2);
    		double xPixelRight = (rotRightSideX / rotRightSideZ * height + width / 2);
    
    		if (xPixelLeft >= xPixelRight) {
    			System.out.println("ERROR: 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);
    		double yPixelLeftBottom = (yCornerBL / rotLeftSideZ * height + height / 2);
    		double yPixelRightTop = (yCornerTR / rotLeftSideZ * height + height / 2);
    		double yPixelRightBottom = (yCornerBR / rotLeftSideZ * height + height / 2);
    
    		double tex1 = 1 / rotLeftSideZ;
    		double tex2 = 1 / rotRightSideZ;
    		double tex3 = 0 / rotLeftSideZ;
    		double tex4 = 8 / rotRightSideZ - tex3;
    
    		for (int x = xPixelLeftInt; x < xPixelRightInt; x++) {
    			double pixelRotation = (x - xPixelLeft) / (xPixelRight - xPixelLeft);
    
    			int xTexture = (int) ((tex3 + tex4 * pixelRotation) / tex1 + (tex2 - tex1) * pixelRotation);
    
    			double yPixelTop = yPixelLeftTop + (yPixelRightTop - yPixelLeftTop) * pixelRotation;
    			double yPixelBottom = yPixelLeftBottom + (yPixelRightBottom - yPixelLeftBottom) * pixelRotation;
    
    			int yPixelTopInt = (int) (yPixelTop);
    			int yPixelBottomInt = (int) (yPixelBottom);
    
    			if (yPixelTopInt < 0) {
    				System.out.println("ERROR: yPixelTopInt < 0");
    				yPixelTopInt = 0;
    			}
    
    			if (yPixelBottomInt > height) {
    				System.out.println("ERROR: yPixelTopInt > height");
    				yPixelBottomInt = height;
    			}
    
    			for (int y = yPixelTopInt; y < yPixelBottomInt; y++) {
    				pixels[x + y * width] = xTexture * 100;
    				zBuffer[x + y * width] = 0;
    			}
    		}
    
    	}
    I attached the actual .jar file if you need to test it to see what's going on.
    Attached Files Attached Files
    Last edited by Andrew_3ds; 05-13-2014 at 05:47 AM.

Similar Threads

  1. How to implement the wall follower algorithm in java?
    By michaelnana in forum New To Java
    Replies: 8
    Last Post: 02-10-2012, 09:42 PM
  2. Brick wall using BLEUJ
    By boumasmoud in forum New To Java
    Replies: 1
    Last Post: 11-03-2011, 11:33 PM
  3. Only one wall block?
    By werner291 in forum New To Java
    Replies: 4
    Last Post: 09-28-2011, 02:47 PM
  4. Wall bug on physics simulation
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 07-14-2010, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •