Character Movement?(beginner question)

Started by Grilled, January 30, 2015, 05:09:19

Previous topic - Next topic

Grilled

Hello ;)!

I am currently having trouble getting my character to move by using the WASD keys (so far there's only the D key implemented). What would you suggest I do? Also, there are any tips you can give me about my code in general I would greatly appreciate it!

Here's my code:

package main;

import static aid.Draw.*;
import static org.lwjgl.opengl.GL11.*;

import java.awt.Color;

import org.lwjgl.input.Keyboard;

public class Player {
	
	public static float x,y;
	public static float vel=0.96f;
	
	public Player(float x, float y){
		
		this.x=x;
		this.y=y;
		
	}
	
	public void playerMovement(){
		drawPlayer();		
		while(Keyboard.next()){
			
			if (Keyboard.getEventKeyState()){
				if(Keyboard.isKeyDown(Keyboard.KEY_D)){					
					x++;
					
				
				
				}
			
		
			}
			
		}		
	}
	
	public static void drawPlayer(){
		
		glBegin(GL_QUADS); //Begins drawing the quad
		glColor3f(0.5f,0.4f,0.3f); //Paints the quad
		glVertex2f(x, y);//Top left corner
		glVertex2f(x + 45,y);//Top right corner
		glVertex2f(x + 45, y+ 45);//Bottom right corner
		glVertex2f(x, y+45);//Bottom left corner
		glEnd();// Ends quad drawing
	}
	
	

}

abcdef

What trouble are you actually having? From a very simple standpoint it looks like you are on the right track. As you build your simple example out you will learn how to better organise things but that's the least of your worries right now.

I would recommend trying to start with LWJGL3 though

Here is some test code which looks at the events (so you know how to register for all of them)

https://github.com/LWJGL/lwjgl3/blob/master/src/tests/org/lwjgl/demo/glfw/Events.java

And a simple example which draws nothing but sets up a new window

http://www.lwjgl.org/guide

You just need to plug your code in to the render cycle here, it should be exactly the same

Grilled

I have the window and stuff set up and I've drawn the square to the screen its just whenever I move it it doesn't actually move the square, it just extends the right side of it whenever I press D.

quew8

You need to clear the screen at the beginning of each loop. Call

glClear(GL_COLOR_BUFFER_BIT);

Grilled

Thank you so much! It worked as intended