I know there may be a forum particularly for this below, but this is sort of urgent for me. I am creating a game in Eclipse and have came across a glitch which I cannot figure out how to solve at all, I am a beginner here so I've probably missed something that a more experienced user can find. I have implemented movement into my game, however it can only move sideways regardless of pressing W, S or turning around. I am positive that I've filled it out properly, I've looked on the tutorial for movement and I cannot find any error I've made with this. Here's the code:
'Game'
package base.dragonlord; import java.awt.event.KeyEvent; public class Game { public int time; public Controller controls; public Game() { controls = new Controller(); } public void tick(boolean[] key) { time++; boolean forward = key[KeyEvent.VK_W]; boolean back = key[KeyEvent.VK_S]; boolean left = key[KeyEvent.VK_A]; boolean right = key[KeyEvent.VK_D]; controls.tick(forward, back, left, right); } }
'Input Handler'
package base.dragonlord; 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 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) { } @Override public void focusLost(FocusEvent e) { for (int i = 0; i < key.length; i++) { key[i] = false; } } @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseDragged(MouseEvent e) { } @Override public void mouseMoved(MouseEvent e) { MouseX = e.getX(); MouseY = e.getY(); } }
'Controller'
package base.dragonlord; public class Controller { public double x, z, rotation, xa, za, rotationa; public static boolean turnLeft = false; public static boolean turnRight = false; public void tick(boolean forward, boolean back, boolean left, boolean right) { double rotationSpeed = 0.025; double walkSpeed = 1; double xMove = 0; double zMove = 0; if (forward) { zMove++; } if (back) { zMove--; } if (left) { xMove--; } if (right) { xMove++; } if (turnLeft) { rotationa -= rotationSpeed; } if (turnRight) { rotationa += rotationSpeed; } xa += (xMove * Math.cos (rotation) + zMove * Math.sin (rotation)) * walkSpeed; za += (zMove * Math.cos (rotation) - xMove * Math.sin (rotation)) * walkSpeed; x += xa; x += za; xa *= 0.1; za *= 0.1; rotation += rotationa; rotationa *= 0.5; } }
Please help me out here !