Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (805) Games in Android Showcase (239) games submitted by our members Games in WIP (868) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [LWJGL] Basic texture velocity  (Read 9774 times) 0 Members and 1 Guest are viewing this topic.
Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Posted 2014-05-04 22:14:57 »

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
 1 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?
Longarmx
 « Reply #1 - Posted 2014-05-05 00:01:26 »

 glTranslate(x, y, z)
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
 glTranslate(-7,  3, 0);
I'm not sure I fully understand your question though. Could you explain it more?

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #2 - Posted 2014-05-05 01:21:20 »

 glTranslate(x, y, z)
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
 glTranslate(-7,  3, 0);
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
Longarmx
 « Reply #3 - Posted 2014-05-05 01:24:25 »

Some pseudo code for you:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21 float dx = 0;float dy = 0;float dz = 0;// Moves at a speed of 3if(moveRightKeyPressed){     dx = 3;}if(moveDownKeyPressed){     dy = -3;}if(moveCloserKeyPressed){     dx = 3;}glTranslate(dx, dy, dz);

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #4 - Posted 2014-05-05 01:26:41 »

Some pseudo code for you:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21 float dx = 0;float dy = 0;float dz = 0;// Moves at a speed of 3if(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.
Longarmx
 « Reply #5 - Posted 2014-05-05 01:45:24 »

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.

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.
 1  2  3  4  5  6  7  8 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.

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #6 - Posted 2014-05-05 02:02:13 »

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.  I apologize if the image doesn't work. I don't know how to post mini recordings of code running besides YouTube.
Longarmx
 « Reply #7 - Posted 2014-05-05 02:11:53 »

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.   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

 1  2  3  4  5  6  7  8  9 glPushMatrix();glTranslate(x, y, z);drawObject(); // However you accomplish thisglPopMatrix();glPushMatrix();glTranslate(x, y, z);drawObject2(); // Draw the second objectglPopMatrix();

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #8 - Posted 2014-05-05 02:15:09 »

 1  2  3  4  5  6  7  8  9 glPushMatrix();glTranslate(x, y, z);drawObject(); // However you accomplish thisglPopMatrix();glPushMatrix();glTranslate(x, y, z);drawObject2(); // Draw the second objectglPopMatrix();

Would I still need the
 1 glBegin()
and
 1 glEnd()
methods for this?
Longarmx
 « Reply #9 - Posted 2014-05-05 02:16:39 »

Would I still need the
 1 glBegin()
and
 1 glEnd()
methods for this?

You put these where the drawObject() goes.

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #10 - Posted 2014-05-05 02:24:05 »

Would I still need the
 1 glBegin()
and
 1 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:

 1  2  3  4  5  6  7  8  9  10  11  12  13 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?
trollwarrior1
 « Reply #11 - Posted 2014-05-05 04:43:37 »

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

 1  2  3  4  5  6  7  8 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();
Longarmx
 « Reply #12 - Posted 2014-05-05 05:15:29 »

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.

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #13 - Posted 2014-05-05 20:04:48 »

It's working now.   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:

 1  2  3  4  5  6 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?
Opiop
 « Reply #14 - Posted 2014-05-05 20:19:30 »

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.
Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #15 - Posted 2014-05-05 20:38:10 »

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
Spacebeans
 « Reply #16 - Posted 2014-05-05 22:03:45 »

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.

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:

Quote from: Wikipedia
A Vertex Buffer Object (VBO) is an OpenGL feature that provides methods for uploading vertex data (position, vector, color, etc.) to the video device for non-immediate-mode rendering. VBOs offer substantial performance gains over immediate mode rendering primarily because the data resides in the video device memory rather than the system memory and so it can be rendered directly by the video device. (This is retained mode)

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/
Longarmx
 « Reply #17 - Posted 2014-05-05 22:06:45 »

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

Opiop
 « Reply #18 - Posted 2014-05-05 22:14:19 »

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.
Spacebeans
 « Reply #19 - Posted 2014-05-05 22:19:38 »

Where did you get the term retained mode...?

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?
Longarmx
 « Reply #20 - Posted 2014-05-05 22:21:11 »

- I don't even think there is a name for that, is there?

Improved mode? Do everything yourself mode? Power mode?

I just usually say it's the modern version.

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #21 - Posted 2014-05-05 22:59:51 »

No idea about that mode thing but I was pretty angry I couldn't get some code to work and you said it was outdated. There was nothing I could do since I have a slow laptop with old graphics card. I did fix my code based on answers though.
Longarmx
 « Reply #22 - Posted 2014-05-05 23:04:51 »

Do you know what version that gfx card can support? Even if it just supports OpenGL 2 then there are some nice things that you can do in code.

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #23 - Posted 2014-05-05 23:16:09 »

Do you know what version that gfx card can support? Even if it just supports OpenGL 2 then there are some nice things that you can do in code.

Actually I haven't checked. now I realized what you guys mean. To test it, should I just bring the GL11 up and run until I get a graphics error?
Longarmx
 « Reply #24 - Posted 2014-05-05 23:17:34 »

Or you can just run this code instead

 1 System.out.println(GL11.glGetStringâ€‹(GL11.GL_VERSIONâ€‹));

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #25 - Posted 2014-05-05 23:24:44 »

 1  2  3  4 [quote author=Longarmx link=topic=33044.msg309982#msg309982 date=1399331854]Or you can just run this code instead ;)[code]System.out.println(GL11.glGetStringâ€‹(GL11.GL_VERSIONâ€‹));

[/quote]

It prints: code]2.1.0 - Build 8.15.10.2555[/code] in the console.
Opiop
 « Reply #26 - Posted 2014-05-05 23:26:11 »

Wow... that's quite low. What graphics card (or more likely integrated chipset) do you have?
Longarmx
 « Reply #27 - Posted 2014-05-05 23:29:16 »

That means that you can use Shaders and VBOs for rendering (Opengl V2). These are considered modern techniques in comparison to legacy Opengl. Obviously, you won't be able to do all that the OpenGL 3+ tutorials teach you, but it is still quite an improvement.

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #28 - Posted 2014-05-06 00:01:57 »

Wow... that's quite low. What graphics card (or more likely integrated chipset) do you have?

Yeah it's about 2 or 3 years old. Where do I check  its' graphics card.
Pages: [1]
 ignore  |  Print

 Riven (585 views) 2019-09-04 15:33:17 hadezbladez (5526 views) 2018-11-16 13:46:03 hadezbladez (2407 views) 2018-11-16 13:41:33 hadezbladez (5788 views) 2018-11-16 13:35:35 hadezbladez (1228 views) 2018-11-16 13:32:03 EgonOlsen (4667 views) 2018-06-10 19:43:48 EgonOlsen (5685 views) 2018-06-10 19:43:44 EgonOlsen (3203 views) 2018-06-10 19:43:20 DesertCoockie (4102 views) 2018-05-13 18:23:11 nelsongames (5120 views) 2018-04-24 18:15:36
 A NON-ideal modular configuration for Eclipse with JavaFXby philfrei2019-12-19 19:35:12Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org