5
\$\begingroup\$

I am making a game in LWJGL (2) and slick-util (I think).

I am trying to move my map (It is a double int array that uses numbers to get the textures), but my code only moves the first texture to be drawn on the screen (top-left). how can I move ALL of my map at the same time? Here is my code:

package Window;

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

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

import controls.TileChanger;
import Functions.renderer;
import Window.GridHandler;

public class Main {

 public static void main(String[] args) {

  renderer.begin();

  int[][] map = {
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  };

  GridHandler grid = new GridHandler(map);
  TileChanger changer = new TileChanger(grid);

  String mapSave = map.toString();
  float translate_x = 0;
  float translate_y = 0;
  int speed = 5;

  while (!Keyboard.isKeyDown(Keyboard.KEY_Q)) {

   //   while(Keyboard.next()){
   if (Keyboard.getEventKey() == Keyboard.KEY_P && Keyboard.getEventKeyState()) {
    System.out.println(mapSave);

    /*      Writer wr;
            try {
                wr = new FileWriter("map.txt");
                wr.write(mapSave);
                wr.close();
            } catch (IOException e) {
                e.printStackTrace();
            } */
   }
   //   }

   if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
    translate_x += speed;
   }

   if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
    translate_x -= speed;
   }

   if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
    translate_y += speed;
   }

   if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
    translate_y -= speed;
   }

   glClear(GL_COLOR_BUFFER_BIT);

   glPushMatrix();

   glTranslatef(translate_x, translate_y, 0);

   glEnable(GL_TEXTURE_2D);


   changer.Update();

   grid.draw();

   glPopMatrix();

   Display.update();
   Display.sync(60);
  }
  Display.destroy();
  System.exit(0);
 }
}

Can anyone help me out? (I need the map to move correctly when I push any of the WASD keys (W = up, A = left, S = down, D = right).

\$\endgroup\$
  • \$\begingroup\$ It seems that the core of what you're asking about is in the method changer.Update();, however, it's not displayed here. Could you edit your question and add what seems to be that important piece of code? \$\endgroup\$ – Vaillancourt Apr 18 '16 at 19:19
  • \$\begingroup\$ Changer.update is the method I use to change the tile textures, the method only calls for if the mouse button is clicked, so it is irrelevant to the question. \$\endgroup\$ – Bryan Apr 19 '16 at 5:59
1
\$\begingroup\$

Translating a camera is the equivalent to translating the world in the opposite direction. In other words, at the very bottom of your matrix stack, your camera's matrix exists. You need to translate this wherever your camera must be in the opposite direction.

If you wish to move up 5 paces. You translate the world down five paces.

This is typically the reason why games abstract away logical positioning, and graphical positioning.

Please see this for more information. https://www.opengl.org/archives/resources/faq/technical/viewing.htm

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I figured the "upside-down" part out, but how do I make it so the gltranslatef moves my camera and not the first object it draws? \$\endgroup\$ – Bryan Apr 19 '16 at 6:02
-1
\$\begingroup\$

You need to translate and rotate the entire scene. For example, use the glTranslatef() and glRotatef functions. Obviously the process becomes more complicated when using shader files.

Note this question is answered here: https://stackoverflow.com/questions/23727493/creating-a-camera-class-with-lwjgl

Some more resources (Google search):

https://m.youtube.com/watch?v=OO_yNzAuDe4

https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter8/chapter8.html

https://tutorialedge.net/lwjgl-3-orthographic-camera-tutorial

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.