Equations or other problems

I’m following this tutorial http://www.youtube.com/watch?v=RGKI_2A5q0M&feature=BFa&list=PL656DADE0DA25ADBB and I copied all his code properly (i think) and my screen seems to move around randomly, if it isnt too bad could anybody help me :slight_smile:

package com.game.Fearless.input;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class InputHandler implements MouseListener, FocusListener, KeyListener, MouseMotionListener {

	
	public boolean[] key = new boolean[68836];
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode > 0 && keyCode < key.length) {
			key[keyCode] = true;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode > 0 && keyCode < key.length) {
			key[keyCode] = false;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}

	@Override
	public void focusGained(FocusEvent e) {
		for (int i = 0; i < key.length; i++){
			key[i] = false;
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		
	}

}

package com.game.Fearless.input;

public class Controller {
	
	public double x, z, xa, za, rotation, rotationa;
	
	public void tick(boolean forward, boolean left, boolean back, boolean right, boolean turnLeft, boolean turnRight) {
//previous code
//		double rotationSpeed = 1;
//		double rotationa = 0.5;
//		double walkSpeed =0.00025;
//		double xMove = 0;
//		double zMove = 0;
//		double angle = 0;
//		
//		if (forward) {
//			if (back == false) {
//				zMove++;
//				z += za;
//				x += xa;
//			}
//		}
//		if (left) {
//			if (right == false) {
//				xMove--;
//				x -= xa;
//				z += za;
//			}
//		}
//		if (back) {
//			if (forward == false) {
//				zMove--;
//				z -= za;
//				x -= xa;
//			}
//		}
//		if (right) {
//			if (left == false) {
//				xMove++;
//				x += xa;
//				z += za;
//			}
//		}
//		if (turnLeft) {
//			if (turnRight == false) {
//				angle += rotationa; 
//			}
//			rotation -= angle * rotationSpeed;			
//		}
//		if (turnRight) {
//			if (turnLeft == false) {
//				angle += rotationa; 
//			}
//			rotation += angle * rotationSpeed;
//		}
//				
//		xa += (xMove * Math.cos(rotation) + zMove * Math.sin(rotation)) * walkSpeed;
//		za += (xMove * Math.cos(rotation) - zMove * Math.sin(rotation)) * walkSpeed;
//		
//		System.out.println("X= " + x + " Z= " + z + " Rotation= " + rotation);
		
		double rotationSpeed = 1;
		double walkSpeed = 1;
		double xMove = 0;
		double zMove = 0;
		
		if (forward) {
			if (!back) {
				zMove++;
			}
		}
		if (left) {
			if (!right) {
				xMove--;
			}
		}
		if (back) {
			if (!forward) {
				zMove--;
			}
		}
		if (right) {
			if (!left) {
				xMove++;
			}
		}
		if (turnLeft) {
			if (!turnRight) {
				rotationa += rotationSpeed;
			}
		}
		if (turnRight) {
			if (!turnLeft) {
				rotationa -= rotationSpeed;
			}
		}
		
		xa += (xMove * Math.cos(rotation) + zMove * Math.sin(rotation)) * walkSpeed;
		za += (xMove * Math.cos(rotation) - zMove * Math.sin(rotation)) * walkSpeed;
	
		x += xa;
		z += za;
		xa *= 0.1;
		za *= 0.1;
		rotation += rotationa;
		rotationa += 0.8;
	}
}


package com.game.Fearless.graphics;

import com.game.Fearless.Game;

public class Render3D extends Render {

	public Render3D(int width, int height) {
		super(width, height);
	}

	public void floor(Game game) {
      
		double floorPos = 8;
		double ceilingPos = 8;
		double xMove = game.controls.x;
		double zMove = game.controls.z;
		double rotation = game.controls.rotation;
		double cos = Math.cos(rotation);
		double sin = Math.sin(rotation);
		
		for (int y = 0; y < HEIGHT; y++) {
		double ceiling = (y  - HEIGHT / 2.0) / HEIGHT;
         
        double floor = floorPos / ceiling;   
		
		if (ceiling < 0){
        	 floor = ceilingPos / -ceiling;
        }       
         
        for (int x = 0; x < WIDTH; x++) {
            double depth = (x - WIDTH / 2.0) / HEIGHT;
            depth *= floor;
            double xx = (depth * cos) + (floor * sin);
            double yy = (floor * cos) - (depth * sin);
            int xPix = (int) (xx + xMove);
            int yPix = (int) (yy + zMove);
            pixels[x + y * WIDTH] = ((yPix & 15) * 16) | ((xPix & 15) * 16) << 8;
         }
      }  
   }
}

What do you mean by “moving screen”? I mean, what’s wrong?

:o That’s quite the wall of text you’ve got there…

What I suggest, and excuse me for my assumptions, is that you look over the tutorial ( I haven’t seen it ) and instead of copying the code, try and understand what each part does. Start slow and debug along the way instead of the end product.

If you’re interested in learning 3D, I might suggest you look into one of the awesome java OpenGL libraries like lwjgl, jogl or even libgdx.

:}

the character will move around randomly

Ive looked around for some lwjgl tuts but I havent found any good ones, I already have the lib its just that I cant find any place to learn it, and Im pretty sure I know what most parts do, but I cant figure out how to fix it

the char will move randomly

Okay there’re so many lines of code above. Gueesing that somethign wrong with your logic (coordinates, speeds).

Why are all the mouse listener methods overridden so that none of them do anything? is it some generalized sort of all-in-one input handler? Where is it used in the rest of the code?

thats because im doing that later on, and I just want to start on the maain movement