Question

I've started using LWJGL recently since I stopped using Java and wanted a quick refresher with something new tied in. Unfortunately, I've encountered a problem while working on a small Java pong game. I sort of sped through it, but I've reviewed my code multiple times and I cannot figure out what is wrong.

Problem: I can't update multiple entities on the screen at the same time. It is weird because I have no problem moving them, and their values update accordingly according to the debugger, but the graphics will not update on the screen. Here is my code for the main program:

package lwjgl2;

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

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Main {

    private Box ball = new Box(640>>1, 480>>1, 5, 5);
    private Box paddle = new Box(10, 10, 10, 40);

    private long lastTime;
    private long getTime(){
        return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }
    private int getDelta(){
        long currentTime = getTime();
        int delta = (int) (currentTime - lastTime);
        lastTime = getTime();
        return delta;
    }

    public Main() {
        //display
        try{
            Display.setDisplayMode(new DisplayMode(640,480));
            Display.setTitle("Hello");
            Display.create();
        } catch(LWJGLException e){
            e.printStackTrace();
        }
        //ogl
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

        lastTime = getTime();

        //main loop
        while(!Display.isCloseRequested()){

            glClear(GL_COLOR_BUFFER_BIT);
            if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
                Display.destroy();
                System.exit(0);
            }
            paddle.setDX(0.1);
            ball.setDX(0.3);
            ball.setDY(0.2);

            //main game loop goes nyah

            ball.update(getDelta());
            paddle.update(getDelta());
            ball.draw();
            paddle.draw();
            Display.update();
            Display.sync(60);
        }
        //destroy display
        Display.destroy();

    }

    private class Box extends Entity{

        public Box(int x, int y, int height, int width) {
            super(x, y, height, width);
            // TODO Auto-generated constructor stub
        }

        public void update(int delta){
            super.x += dx * delta;
            super.y += dy * delta;
        }

        public void draw(){
            glRecti(x, y, x + height, y + width);
        }

    }

    public static void main(String args[]){
        new Main();
    }

}

Like I said, the only entity that will actually render properly on the screen is whatever I call the update() method on first, in this case, the ball. If I call the paddle first, then the paddle will move, but the ball will stay still. I know I must have made a stupid mistake somewhere but I can't find it.

Also, the Entity class, possibly the main culprit:

package lwjgl2;

import java.awt.Rectangle;

public abstract class Entity {

    protected int x;
    protected int y;
    protected int height;
    protected int width;
    protected double dx;
    protected double dy;
    protected Rectangle bounds = new Rectangle();


    public Entity(int x, int y, int height, int width) {
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
        this.dx = 0;
        this.dy = 0;
    }

    public int getX(){
        return x;

    }

    public int getY(){
        return y;

    }

    public double getDX(){
        return dx;

    }

    public double getDY(){
        return dy;

    }

    public void setX(int x){
        this.x = x;
    }

    public void setY(int y){
        this.y = y;
    }

    public void setDX(double dx){
        this.dx = dx;
    }

    public void setDY(double dy){
        this.dy = dy;
    }

    public void update(int delta){

    }

    public void draw(int delta){

    }

}
Was it helpful?

Solution

I've figured it out. It happens that updating multiple entities at varying times through the loop isn't a good idea, so you should create a method for updating every entity at once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top