[LWJGL] Basic texture velocity

Basically all I did was load a texture using the Slick_util and need to move it. Based on my game dev experience, would I just make a getter and setter for the velocity and x and y coordinates? I read on this and saw a method called

glTranslate()

and it moves the matrix or something instead. How would basic movement work in lwjgl, and how do I keep the movement simple before side scrolling? ???

[icode]glTranslate(x, y, z)[/icode] does pretty much what it says. It translates the matrix. If you want a texture to move left 7 and up 3 then you would say [icode]glTranslate(-7, 3, 0);[/icode] I’m not sure I fully understand your question though. Could you explain it more?

I mean how would I use this in a keypress event. Such as if up is pressed it moves up at a certain speed

Some pseudo code for you:


float dx = 0;
float dy = 0;
float dz = 0;

// Moves at a speed of 3
if(moveRightKeyPressed)
{
     dx = 3;
}

if(moveDownKeyPressed)
{
     dy = -3;
}

if(moveCloserKeyPressed)
{
     dx = 3;
}

glTranslate(dx, dy, dz);

Yes! this is what I was looking for. After seeing your other post on the guy asking about slick, I thought Java2D was a great intro to game dev.

So now that I have this covered, do you have any good math sites to explain what matrixes are. If you can explain in detail that would be great. I havn’t covered this kind of material in school yet.

Matrices basically just encapsulate data. They have special rules on how you can transform them. The basic idea is that you can create a single matrix to represent a series of transformations (translations, rotations, scaling). You can then multiply vectors (representing object positions) by that matrix to transform that point into a different spot.

Link 1
Link 2

Basically just google ‘matrix tutorial.’ You will probably have to see how this is used in code to get how matrices are used.

The main example I can think of is in a shader.


uniform mat4 transformation;

in vec3 vertex;

void main()
{
     gl_Position = vertex * transformation;
}

Don’t worry if this all flies over your head. If you’re just using legacy (which you are if you’re using glTransform) then opengl will handle all of this for you.

lastly, with glTranslate; how do I make each texture move itself instead of the whole matrix or screen. I have 2 images both sharing glTranslate and moving at the same direction.
http://screenshooter.net/8987137/jwcdpkr
I apologize if the image doesn’t work. I don’t know how to post mini recordings of code running besides YouTube.

You would use glPushMatrix() and glPopMatrix(). These two functions basically put a new matrix onto the stack and then remove the top matrix. If you ever want to get back to the main (modelview) matrix then you can call [icode]glLoadIdentity()[/icode]


glPushMatrix();
glTranslate(x, y, z);
drawObject(); // However you accomplish this
glPopMatrix();

glPushMatrix();
glTranslate(x, y, z);
drawObject2(); // Draw the second object
glPopMatrix();

Would I still need the

glBegin()

and

glEnd()

methods for this?

You put these where the drawObject() goes.

I think I understand where your going with this but the textres aren’t moving. In my !Display.IsClosed loop I called 2 of these:

 
                     glPushMatrix();
            glTranslated(1, 1, 0);
            
            glBegin(GL_QUADS);

                        glTexCoord2f(0, 0); glVertex2i(100, 100); // Upper-left
                        glTexCoord2f(1, 0); glVertex2i(150, 100); // Upper-right
                        glTexCoord2f(1, 1); glVertex2i(150, 150); // Bottom-right
                        glTexCoord2f(0, 1); glVertex2i(100, 150); // Bottom-left
                        
            glEnd();
                        glPopMatrix();

Assuming already created the texture and binded it, what would make this incorrect?

Why are you using matrices to translate each object, if you’re sending vertices every time you render? Wouldn’t it be more logical to send x,y,width,height


            glBegin(GL_QUADS);

                        glTexCoord2f(0, 0); glVertex2i(x, y); // Upper-left
                        glTexCoord2f(1, 0); glVertex2i(x + width, y); // Upper-right
                        glTexCoord2f(1, 1); glVertex2i(x + width, y + height); // Bottom-right
                        glTexCoord2f(0, 1); glVertex2i(x, y + height); // Bottom-left
                        
            glEnd();

glTranslate would be a better option for transforming groups of objects the same way. If you want to transform each object individually then setting the x and y for each vertex would be easier.

It’s working now. ;D But could I create a player class with its’ private x and y and in my main drawing class just call player.getx and player.gety? I’m getting the hang of things, I created some pseudo code:


             glBegin(GL_QUADS);
                        glTexCoord2f(0, 0); glVertex2i(player.getx(), player.gety()); 
                        glTexCoord2f(1, 0); glVertex2i(player.getx() + width, player.gety()); 
                        glTexCoord2f(1, 1); glVertex2i(player.getx() + width,  player.gety() + height);
                        glTexCoord2f(0, 1); glVertex2i(player.getx(), player.gety() + height);
            glEnd();

I would be calling the getters and setters in the player class, but this seems correct right?

Also, in my glBegin; do I have to draw everything in between begin and end? I can’t have more than one begins and ends?

You can have more than one begin and end pair, but try to minimize the amount of calls you make to them. I also have to tell you, the OpenGL code you’re using right now is very outdated, and you would probably be better off learning modern (3.x or so) OpenGL because you’ll have to move away from the code you are using right now.

So where should I learn it then? Every tutorial ive seen uses something like this except usually with a constructor

Knowing Opiop56, he will pretty much recommend advanced stuff to everyone whenever he sees deprecated OpenGL methods. :slight_smile:

Basically glBegin and glEnd are very old. People don’t use them anymore, because it sends the vertex data (points) to the graphics card inefficiently. (Every frame, Which is very, very slow)
glBegin and glEnd are Immediate mode. While everything else is retained mode.

You don’t really have to worry about all this new 3.x stuff yet, but if you want to get you feet wet, Wikipedia states:

You can get some really awesome tutorials on that here:

http://www.opengl-tutorial.org/beginners-tutorials/
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-4-a-colored-cube/

Or you can even look on the lwjgl wiki under the OpenGL 3.2 and newer section to get you started.

Where did you get the term retained mode…?
And I recommend it only because everyone recommended it to me when I was starting out, and I ignored them. I spent a year learning deprecated code only to have to relearn OpenGL completely. Trust me, old immediate mode isn’t worth the time.

Mah bad. I thought it was the opposite of immediate. I just googled “immediate mode vs ?” and it had “retained mode” in the search.

  • I don’t even think there is a name for that, is there?