Uncategorized

Lwjgl 3D Game Engine.

For some months now I have been learning opengl so that I can make my own 3D video games . So I decided to make my own game engine that I will use in making my games.
Note: The code used in this tutorial is far  from perfect also note that I assume you know basic java and eclipse (the IDE I’m using) . With that out of the way let’s  get started. 

 Create a new  project in eclipse call it whatever you like .  You can download the lwjgl library here Download the lwjgl 2  version  don’t go for lwjgl 3 since they are very different and I’m using lwjgl 2.xx so if you choose to get lwjgl 3 you are on your own.
Setting up lwjgl in eclipse .

  • Right click on your project folder and go to properties.
  • Right click on java Build Path and select the libraries tab.
  • To add lwjgl to your project click on either Add JARs…. or Add External JARs…. depending on where your lwjgl folder is on your computer .
  • Once in your lwjgl folder select lwjgl_utils ,lwjgl . 
  • Then click on lwjgl.jar and select native  library and navigate to the lwjgl files and select natives and choose your operating system .

​

        ​

        ​

        Now that the lwjgl is added to our project we can start making our game engine . 
        The Game Loop .

        I am going to be using a fixed step game loop for the game engine over the  variable step game loop .Each type of loop has its own pros and cons ,if you have time I recommend that you do your own research and decide which game loop is good for you . 

        Create a new package called gameLoop and create a GameEngine class. 

        package gameLoop;

        public class GameEngine implements Runnable {

        public static final int TARGET_FPS = 120;

        public static final int TARGET_EPS=30;

        private final Timekeeper keeper;

        private final Thread gameThread;

        public GameEngine(){

        gameThread = new Thread(this,””);

        keeper = new TimeKeeper();

        public void thread(){

        String osName = System.getProperty(“os.name”);

        If(osName.contains(“Mac”)){

        gameThread.run();

        }else{

        gameThread.start();}

        }

        @Override

        public void run(){

        try{

        create();

        gameLoop();

        }catch(Exception e ){

        e.printStackTrace();

        }}

        protected void gameloop(){

        float elapsedTime;

        float accumulator = 0f;

        float intervals = 1f/ TARGET_EPS ;

        boolean isRunning = true;

        while(isRunning && !Display.isCloseRequested()){

        elapsedTime = keeper.getElapsedTime();

        while(accumulator >= intervals){

        update(intervals);

        accumulator -= intervals;

        }

        float loop = 1f / TARGET_FPS;

        double time = keeper.getLastLoopTime() + loop ;

        while(keeper.getTime() < time){

        try{

        Thread.sleep(1);}catch(InterruptedException e){

        }

        }}

        Our gameEngine class implements Runnable so that we can have access to use the Thread (create a new Thread when run() is called) . Once you implement Runnable you will notice that you have to implement its method  run the next lines are self explanatory . In the thread method we check which operating system the engine is running .If we tried to run the engine while calling gameThread.start() on a mac an exception will occur , thats why we have to check which operating system our engine is being run on  . The rest of the code is the fixed time step we implement in our game engine.  

        Now we need to create our TimeKeeper class that  calculates the time difference since we last updated  .  

        package gameLoop;

        public class Timer {

        private double lastTick;

        public void init(){

        lastTick = getTime():

        }

        public double getTime(){

        return System.nanoTime()/1000000000.0;

        }

        public float  getElapsedTime(){

        double tick = getTime();

        float elapsedTime =(float)(tick-lastTick);

        lastTick = tick;

        return elapsedTime;

        }

        public double getLastTickTime(){return last Tick}}

          So thats our game loop done for now . next time we are going to make our display(just a black screen) using opengl . Feel free to comment cheers! 

        Leave a Reply

        Fill in your details below or click an icon to log in:

        WordPress.com Logo

        You are commenting using your WordPress.com account. Log Out / Change )

        Twitter picture

        You are commenting using your Twitter account. Log Out / Change )

        Facebook photo

        You are commenting using your Facebook account. Log Out / Change )

        Google+ photo

        You are commenting using your Google+ account. Log Out / Change )

        Connecting to %s