How do I make mouse movement natural?

I need to make the mouse movement in my game have a natural feel to it like any other FPS. Also, how could I loop the mouse around in the screen so the mouse doesn’t go off the screen when I move it? I’ll only post the classes which I think will have to do with this problem.

package me.kenneth.game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import me.kenneth.graphics.Screen;
import me.kenneth.input.Controller;
import me.kenneth.input.InputHandler;

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

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;
	private InputHandler input;
	private int newX = 0;
	private int oldX = 0;
	private int fps = 0;
	
	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();
		input = new InputHandler();
		addKeyListener(input);
		addFocusListener(input);
		addMouseListener(input);
		addMouseMotionListener(input);
	}
	
	public void start() {
		if (running) return;
		
		running = true;
		thread = new Thread(this);
		thread.start();
	}
	
	public synchronized 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;
		
		while (running) {
			
			this.requestFocus();
			
			long currentTime = System.nanoTime();
			long passedTime = currentTime - previousTime;
			previousTime = currentTime;
			unprocessedSeconds += passedTime / 1000000000.0;
			boolean ticked = false;
			
			while (unprocessedSeconds > secondsPerTick) {
				ticked = true;
				tick();
				unprocessedSeconds -= secondsPerTick;
				tickCount++;
				if (tickCount % 60 == 0) {
					System.out.println(frames + "fps");
					fps = frames;
					previousTime += 1000;
					frames = 0;
				}
			}
			
			if (ticked) {
			}
			
			render();
			frames++;
			
			newX = InputHandler.mouseX;
			if (newX > oldX) {
				Controller.turnRight = true;
			}
			if (newX < oldX) {
				Controller.turnLeft = true;
			}
			if (newX == oldX) {
				Controller.turnLeft = false;
				Controller.turnRight = false;
			}
			oldX = newX;
			
		}
		
	}
	
	private void tick() {
		
		game.tick(input.key);
	}
	
	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.setFont(new Font("Verdana", 0, 20));
		g.setColor(Color.WHITE);
		g.drawString(String.valueOf(fps), 20, 20);
		g.dispose();
		bs.show();
		
	}
	
	public static void main(String[] args) {
		
		Image cursor = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16, 16, new int[16 * 16], 0, 16));
		Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank");
		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();
		frame.getContentPane().setCursor(blank);
		
		game.start();
		
	}

}

package me.kenneth.game;

import java.awt.event.KeyEvent;

import me.kenneth.input.Controller;

public class Game {
	
	private int time;
	public Controller controls;
	
	public Game() {
		time = 0;
		controls = new Controller();
	}
	
	public void tick(boolean[] args) {
		time++;
		boolean forward = args[KeyEvent.VK_W];
		boolean back = args[KeyEvent.VK_S];
		boolean right = args[KeyEvent.VK_D];
		boolean left = args[KeyEvent.VK_A];
		boolean jump = args[KeyEvent.VK_SPACE];
		boolean crouch = args[KeyEvent.VK_CONTROL];
		boolean run = args[KeyEvent.VK_SHIFT];
		controls.tick(forward, back, left, right, jump, crouch, run);
	}
	
	public int getTime() {
		return time;
	}

}

package me.kenneth.input;

public class Controller {
	
	public double x, z, y, rotation, xa, za, rotationa;
	public static boolean turnLeft = false, turnRight = false;
	public static boolean walk = false, crouchWalk = false, runWalk = false;
	
	public void tick(boolean forward, boolean back, boolean left, boolean right, boolean jump, boolean crouch, boolean run) {
		
		double rotationSpeed = 0.025;
		double walkSpeed = 0.5;
		double jumpHeight = 2;
		double crouchHeight = 1.3;
		double xMove = 0;
		double zMove = 0;
		
		if (forward) {
			zMove++;
			walk = true;
		}
		
		if (back) {
			zMove--;
			walk = true;
		}
		
		if (right) {
			xMove++;
			walk = true;
		}
		
		if (left) {
			xMove--;
			walk = true;
		}
		
		if (turnLeft) {
			rotationa -= rotationSpeed;
		}
		
		if (turnRight) {
			rotationa += rotationSpeed;
		}
		
		if (jump) {
			y += jumpHeight;
			run = false;
		}
		
		if (crouch) {
			y -= crouchHeight;
			run = false;
			crouchWalk = true;
			walkSpeed = 0.2;
		}
		
		if (run) {
			walkSpeed = 1;
			runWalk = true;
		}
		
		if (!forward && !back && !right && !left && !run) {
			walk = false;
		}
		
		if (!crouch) {
			crouchWalk = false;
		}
		
		if (!run) {
			runWalk = false;
		}
		
		xa += (xMove * Math.cos(rotation) + zMove * Math.sin(rotation)) * walkSpeed;
		za += (zMove * Math.cos(rotation) - xMove * Math.sin(rotation)) * walkSpeed;
		
		x += xa;
		y *= 0.5;
		z += za;
		xa *= 0.1;
		za *= 0.1;
		rotation += rotationa;
		rotationa *= 0.1;
		
	}

}

package me.kenneth.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 KeyListener, FocusListener, MouseListener, MouseMotionListener {

	public boolean[] key = new boolean[68836];
	public static int mouseX;
	public static int mouseY;
	
	@Override
	public void mouseDragged(MouseEvent event) {
		
		
		
	}

	@Override
	public void mouseMoved(MouseEvent event) {
		
		mouseX = event.getX();
		mouseY = event.getY();
		
	}

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

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

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

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

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

	@Override
	public void focusGained(FocusEvent event) {
		// TODO Auto-generated method stub
		
	}

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

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

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

	@Override
	public void keyTyped(KeyEvent event) {
		// TODO Auto-generated method stub
		
	}
	
	

}

That code is off of a Video on YouTube pertaining to creating a 3D fps using pure pixels/textures.
Believe I fixed it or he later released the fixes.
I’ll look for the methods to fix the ease of the mouse movement, but the direction of the player’s movement still is glitched depending on which degree of (90) the mouse is.

I’ll just post all the classes you posted / the ones needing updating:

Controller class:


public class Controller {

	public static double x;
	public static double z;
	public static double rotation;
	public double xa;
	public double za;
	public double rotationa;
	public double y;

	public Controller() {
	}

	public static boolean turnLeft = false;
	public static boolean turnRight = false;
	public static boolean walk = false;
	public static boolean crouchWalk = false;
	public static boolean runWalk = false;

	public void tick(boolean forward, boolean back, boolean left,boolean right, boolean jump, boolean crouch, boolean run,boolean turnRight2, boolean turnLeft2) {
		// Here we cast the rotationSpeed to 0.002 * the mouseSpeed.
		double rotationSpeed = 0.002 * Display.MouseSpeed;
		double runSpeed = 1;
		double jumpHeight = 0.9;
		double crouchHeight = 0.3;
		double walkSpeed = 0.7;
		double xMove = 0;
		double zMove = 0;

		if (turnLeft || turnLeft2) {
			rotationa -= rotationSpeed;
		}
		if (turnRight || turnRight2) {
			rotationa += rotationSpeed;
		}
		if (forward) {
			zMove += walkSpeed;
			walk = true;
		}
		if (back) {
			zMove -= walkSpeed;
			walk = true;
		}
		if (right) {
			xMove += walkSpeed;
			walk = true;
		}
		if (left) {
			xMove -= walkSpeed;
			walk = true;
		}
		if (jump) {
			y += jumpHeight;
			run = false;
		}
		if (crouch) {
			y -= crouchHeight;
			walkSpeed = 0.2;
			run = false;
			crouchWalk = true;
		}
		if (run) {
			walkSpeed = runSpeed;
			walk = true;
			runWalk = true;
		}
		if (!forward && !back && !right && !left) {
			walk = false;
		}
		if (!crouch) {
			crouchWalk = false;
		}
		if (!run) {
			runWalk = false;
		}
		xa += (xMove * Math.cos(rotation) + zMove * Math.sin(rotation))
				* walkSpeed;
		za += (zMove * Math.cos(rotation) + xMove * Math.sin(rotation))
				* walkSpeed;
		x += xa;
		y *= 0.9;
		z += za;
		xa *= 0.1;
		za *= 0.1;
		rotation += rotationa;
		rotationa *= 0.5;
	}
}

Window or Display class:


public class Display extends Canvas implements Runnable {

	private static String V = "0.07";
	private int[] pixels;
	public static final String title = "EliteFront Pre-Alpha " + V + "";
	/*
	 * 0.00: Basic Display Window. 
	 * 0.01: Rendering + alpha pixel support. 
	 * 0.02: Ceiling + floor environment + Basic 3D Support. 
	 * 0.03: Key/Mouse support + movement. 
	 * 0.04: Render Distance Limiting + Gradient paint shadow. 
	 * 0.05: Removed mouse cursor, added mouse interaction/turning. 
	 * 0.06: Walls,Textures (Entity Creation). 
	 * 0.07: First 3D Object, crash fix. 
	 * 0.08: Clipping, object textures, mouse movement speed detection.
	 */
	public static final int WIDTH = 800;
	public static final int HEIGHT = 700;
	private static Thread t;
	private static boolean running = false;
	private Screen screen;
	private BufferedImage img;
	private Game game;
	private int fps;
	public InputHandler input;
	private int newX = 0;
	private int oldX = 0;
	public static int Dimension3D = 3;
	public static JFrame frame;
	public static int MouseSpeed;

	public Display() {
		Dimension size = new Dimension(WIDTH, HEIGHT);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		screen = new Screen(WIDTH, HEIGHT);
		game = new Game();
		img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
		input = new InputHandler();
		setFocusable(true);
		addKeyListener(input);
		addFocusListener(input);
		addMouseListener(input);
		addMouseMotionListener(input);
	}

	public static void main(java.lang.String[] args) {
		BufferedImage cursor = new BufferedImage(16, 16,
				BufferedImage.TYPE_INT_ARGB);
		Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor,
				new Point(0, 0), "blank");
		Display game = new Display();
		frame = new JFrame();
		frame.add(game);
		frame.setResizable(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle(title);
		frame.getContentPane().setCursor(blank);
		frame.pack();
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		game.start();
	}

	void start() {
		if (running)
			return;
		running = true;
		t = new Thread(this);
		t.start();
	}

	public void run() {
		int frames = 0;
		double unprocessedSecond = 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;
			unprocessedSecond += passedTime / 1000000000.0;
			requestFocus();
			while (unprocessedSecond > secondsPerTick) {
				tick();
				debug(400, 222);
				unprocessedSecond -= secondsPerTick;
				ticked = true;
				tickCount++;
				if (tickCount % 60 == 0) {
					fps = frames;
					previousTime += 1000;
					frames = 0;
				}
			}
			if (ticked) {
				render();
				frames++;
			}
			render();
			frames++;
				// Here is where the NEW mouse controlling is handled (Basing the mouse movement off of the mouse speed)
			newX = InputHandler.MouseX;
			if (Controller.rotation != 0 || Controller.rotation == 0) {
				if (newX > oldX) {
					Controller.turnRight = true;
				}
				if (newX < oldX) {
					Controller.turnLeft = true;
				}
				if (newX == oldX) {
					Controller.turnLeft = false;
					Controller.turnRight = false;
				}
				MouseSpeed = Math.abs(newX - oldX);
				oldX = newX;
			}
		}
	}

	private void debug(int max, int desired) {
		Random r = new Random();
		int results = r.nextInt(max);
		if (results == desired) {
			System.gc();
			System.runFinalization();
			System.out.println("DEBUGGED");
		}
	}

	private void render() {
		BufferStrategy bs = this.getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(Dimension3D);
			return;
		}
		screen.render(game);
		for (int i = 0; i < WIDTH * HEIGHT; i++) {
			pixels[i] = screen.pixels[i];
		}
		Graphics g = bs.getDrawGraphics();
		final Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g.drawImage(img, 0, 0, frame.getWidth() + 10, frame.getHeight() + 10,
				null);
		g2d.setFont(new Font("Verdana", 3, 20));
		g2d.setColor(Color.YELLOW);
		g2d.drawString("Current Fps: " + fps, 10, 20);
		g.dispose();
		g2d.dispose();
		bs.show();
	}

	private void tick() {
		game.tick(input.keys);
	}

	public static void stop() {
		if (!running)
			return;
		running = false;
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
}

InputHandler class:


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

	public InputHandler() {
	}

	public static int MouseX;
	public static int MouseY;
	public static int mouseButton;

	public boolean[] keys = new boolean[68836];

	/** INPUT RELATED METHODS */
	public void mouseDragged(MouseEvent m) {
	}

	public void mouseMoved(MouseEvent m) {
		MouseX = m.getX();
		MouseY = m.getY();
	}

	public void mouseClicked(MouseEvent m) {
		mouseButton = m.getButton();
	}

	public void mouseEntered(MouseEvent m) {
	}

	public void mouseExited(MouseEvent m) {
	}

	public void mousePressed(MouseEvent m) {
	}

	public void mouseReleased(MouseEvent m) {
	}

	public void focusGained(FocusEvent f) {
	}

	public void focusLost(FocusEvent f) {
		for (int i = 0; i < keys.length; i++) {
			keys[i] = false;
		}
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if (key > 0 && (key < keys.length)) {
			keys[key] = true;
		}
		if (key == 27) {
			System.gc();
			Display.stop();
			System.exit(0);
		}
	}

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

	public void keyTyped(KeyEvent e) {
	}
}

Way too much code posted, most people here will TL;DR and not read this.

Based on the OP’s question, I believe you want the mouse to stay invisible and hidden so when you move it, it moves the player in game but the mouse stays hidden?

This is done by resetting the mouse to the center of the screen after it is moved. It is done by using the java.awt.Robot class. Beware that moving the mouse using Robot.mouseMove also throws a mouse event.

Tell me why out of all the members i was 90% sure ‘ra4king’ was going to come along and say ‘that’s too much code posted…’ or ‘do this instead’.
lol ??? ???

Trust me you aren’t alone thinking that way ;D and most don’t troll about it ;D. And that makes illusion that your the one and only ;D ;D ;D.

O_o I was only pointing something out :confused: :’(

I hate to admit but ra4king has point, we have pastebin feature which will reduce my pain in scrolling down to post this.

What would be nice is if you could hide a code block beneath a [spoiler] lolol [/spoiler] so you have to click to reveal/hide, it just blacks everything out atm…