Trying to use velocity with a rendered box.

Started by EB5473, January 01, 2012, 04:20:13

Previous topic - Next topic

EB5473

I'm working on the basis for a game, and I've been trying to implement a system for velocity. I have the basis, but it only goes one way, to the right, or adding to the X value.
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.input.Keyboard.*;


import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Player {

	public Player() {
		
	}
	public void start() {
		try {
			Display.setDisplayMode(new DisplayMode(800,600));
			Display.create();
		}
		catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		initGL();
		
		while(!Display.isCloseRequested()) {
			double yc = y;
			renderGL();
			
			
			
			if(isKeyDown(KEY_W)) y -= 5;
			if(isKeyDown(KEY_S)) y += 5;
			if(isKeyDown(KEY_A)) x -= 5;
			if(isKeyDown(KEY_D)) x += 5;
			
			x += velocity;
			
			
			if(isKeyDown(KEY_A) || isKeyDown(KEY_D))velocity = 5;
			if(!isKeyDown(KEY_A) && !isKeyDown(KEY_D))velocity -= .1 ;
			System.out.println(velocity);
			
			
			System.out.println(x);
			if(velocity < 0) velocity = 0.0;
			if(yc != y) r = 0;
			
			
			
			
			if(y < 0) y = 0;
			if(y+100 > 600) y = 500;
			if(x < 0) x = 0;
			if(x+100 > 850) x = 750;
			
			
			r += 1;
			y += .8 * r;
			if(y+100 > 600) r = 0;
			
			
			
			
			
			Display.update();
			
		}
		
		Display.destroy();
	}
	
	public void renderGL() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glColor3f(0.0f,1.0f,0.0f);
		
		
		glBegin(GL_QUADS);
			glVertex2d(x,y);
			glVertex2d(x,y+100);
			glVertex2d(x+50,y+100);
			glVertex2d(x+50,y);
		glEnd();
	}
	
	public void initGL() {
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0,800,600,0,1,-1);
		glMatrixMode(GL_MODELVIEW);
		
	}
	
	public static void main(String[] args) {
		Player displayer = new Player();
		displayer.start();
	}
	public double x = 100;
	public double y = 500;
	public int r = 1;
	public double velocity = 0;
	
}

Any suggestions to make it go both ways?
Coder, Mastermind, Friend

Evil-Devil

You might want to use a direction vector or as you seem to do 2D an inverted direction flag. And then add the velocity as it :)