Game Camera OpenGL | Coding a 2D Game Engine in Java #7

Поділитися
Вставка
  • Опубліковано 7 жов 2024

КОМЕНТАРІ • 74

  • @jestry2508
    @jestry2508 3 роки тому +54

    Gabe actually did the whole series about making mario on one day and switched his outfits after every video , to let us think he made the following videos on seperate days.

    • @jestry2508
      @jestry2508 3 роки тому +7

      much luv

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому +25

      Oh no! How did you find out??
      I have done this for a few episodes actually lol, but I haven't had time to do multiple recordings in one day anymore. I also appreciate your comments Jestry, and I'm glad you're enjoying the videos :D

  • @nicholashendrata
    @nicholashendrata 4 роки тому +41

    Sometimes I just wish I can press the like and subscribe button a thousand times.
    Best series ever man. Helps out a lot and its just amazing.

  • @drochit5901
    @drochit5901 2 роки тому +1

    Im nooob in java, and this is not just "guide how to create ur own engine". It is some more prectice, some more intresting stuff,some more usefull things. Thanks u man! I really enjoing ur course.

  • @twoleggedcat_cs2
    @twoleggedcat_cs2 3 роки тому +2

    You deserve more subs this content is awesome not many people explain things very well but you take your time and I can tell you are super passionate about teaching it's dope!

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому

      Thanks @TwoLeggedCat33! I really appreciate the encouraging comment! I figure if I keep making the content and people enjoy it they'll eventually sub :)

  • @triangle4studios
    @triangle4studios 2 роки тому

    The best game development tutorial out there. By Far.

  • @rxn7
    @rxn7 3 роки тому +1

    you're helping a ton, i hope you'll continue this series

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому

      Thanks Rotthin! I do plan on creating some more videos to get the engine to a point where we can make games with it, and then continuing with some more series that are shifting to C++. But if you guys want me to keep making videos expanding on this engine I'm definitely not opposed to that :)

  • @zapjoshua
    @zapjoshua 3 роки тому

    I just want to say thank you for making these! I started watching yesterday and already on this episode. Every tutorial is engaging and makes me want to continue. Even though I may not understand most of what you're doing right now, my hopes is that I will after I'm done and I mess around with the code a bit myself. Thanks Again!

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому +1

      Hey Joshua, thanks for the comment! I'm glad you're liking the series, and I would say that it's alright if you don't completely understand the material immediately. When I first started learning all this stuff I was following ThinMatrix's videos and did not understand why we did any of the stuff we did either haha. But, the foundation helped me out a lot when I finally decided to try and code this myself, and I had a general idea of the things that I needed to code and the proper terminology for a lot of the concepts. Feel free to reach out with any specific questions though and I'll try to answer to the best of my ability :)

    • @zapjoshua
      @zapjoshua 3 роки тому

      @@GamesWithGabe Thank you so much. You're the type of person to inspire many to start coding including myself. Out of all the tutorials using java to create something, your videos have been the only ones I've been able to follow and get working so far. Keep up the great work!

  • @giovannimezzadri266
    @giovannimezzadri266 3 роки тому

    great tutorial

  • @sigalgivatikovayko4583
    @sigalgivatikovayko4583 4 роки тому +8

    Hey Gabe, awesome series! surely the best one I've found since I got interested in opengl.
    I have a little trouble with some of the concepts in this video, mainly the idea of view matrix.
    1. My biggest issue is with the cameraUp object. What does it represent? What does it even mean? If I set it's values to (1.0f, 0.0f, 0.0f) - nothing is shown. Why is that?
    2. We have the ortho() method (of the Matrix4f class). It takes some params such as 32.0f * 40.0f for right and some other float for the top. Where do these numbers come from? How did you calculate them? What do they represent?
    3. You also create an object named cameraFront object, with some initial values, that are overridden with other values with the add() method. Is there a meaning for initializing the object before setting the actual required values?
    4. Also, if you can please confirm of deny a conclusion I have: if the camera if positioned in the 20.0f of the z axis, than any value of the z axis for the cameraFront object - will show the same result.
    Thanks a lot for these videos!

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +8

      Hey Sigal, thanks for the comment! So, this is definitely a concept that can be difficult to grasp, and I was rewatching my explanation and I did leave a few things out haha.
      These questions are a bit difficult to explain through text only, so I wrote up a quick article here: ambrosiogabe.github.io/on-cameras-in-opengl/ that addresses question 1 and 2 and kind of addresses 3. You can also take a look at this article: learnopengl.com/Getting-started/Camera which goes a bit deeper into the math behind everything.
      Let me just sort of rehash it here too though. The camera up represents the top of the camera. When you change it to (1.0f, 0.0f, 0.0f) you're basically saying that the top of the camera is in the positive x-direction, which means the camera would be pointing up instead of forwards at our game scene. This means you don't see anything, since we don't have any objects above the camera.
      The ortho method takes in parameters that represent the camera frustum, this basically just means that it will define how many world units the camera can "see". So, if you were to give it (0, 1920, 0, 1080, 0.5, 100) as inputs, this means that the camera can "see" any objects that are between x-coordinates 0 and 1920, and y-coordinates 0 and 1080, and z-coordinates 0.5-100. When we use this in conjunction with the view matrix, we are able to "move" the camera around so it sees different objects.
      The reason I set the cameraFront before adding is just an arbitrary decision I made on the fly haha. You could initialize it as cameraFront = (position.x, position.y, -1.0f) and then not do the add operation in the method, and that would be equivalent.
      Lastly, you are almost correct on that assumption. If you move the z-axis to anything below -1, then you won't see anything, because the camera will have flipped directions, it was previously looking along the positive z-axis, but now it's looking along the negative z-axis. A cool thing you can try to experiment with this, is change your camera position to -20.0f for the z coordinate, change cameraFront to (0.0f, 0.0f, 1.0f) and then in your projectionMatrix.ortho() call, swap left and right, and make left negative. So if your ortho call looked like this (0.0f, 32.0f * 40.0f, 0.0f, 32.0f * 21.0f, 0.0f, 100.0f) change it to (-32.0f * 40.0f, 0.0f, 0.0f, 32.0f * 21.0f, 0.0f, 100.0f) where I just swapped the first two values and made it negative. What you'll notice is that you're entire scene is now flipped horizontally, which makes sense because the camera is now facing your scene from behind :)
      Let me know if any of that needs further explanation, and once again thanks for watching! :D

    • @sigalgivatikovayko4583
      @sigalgivatikovayko4583 4 роки тому +3

      @@GamesWithGabe Thanks a lot for your answer! Really helpful

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +1

      No problem man!

    • @sigalgivatikovayko4583
      @sigalgivatikovayko4583 4 роки тому +3

      @@GamesWithGabe Ok, I read both your comment and your article, and I need one last clarification.
      So we have the camera position (the eye). Say it's (0, 0, 20).
      It's looking towards some random position in the space (the cameraFront/center). Say its (0, 0, -1).
      But - that cameraFront/center is not absolute, but relative to the cameraUp.
      Because - it the cameraUp is (1, 0, 0), then the point (0, 0, -1) will not even be in its range.
      From what I get, the absolute cameraFront/center point will not be (0, 1, 0).
      Am I right?
      Thanks for your answers!

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +4

      Hey Sigal, you are correct in all your assumptions. The camera front is always relative to the eye, which also depends on which direction the camera up is, so (0, 1, 0) would be a good absolute coordinate for the camera if the camera up is (1, 0, 0), and (0, 0, -1) would not even be in the range of the camera :)

  • @blacklight3403
    @blacklight3403 4 роки тому +1

    Hey Gabe, first I would like to say, thank you for these tuts. They have to be the most comprehensive java game coding tutorials that I personally have ever found on youtube. You have a talent for teaching this stuff. I have one question, is there a way to set up a ( isometric 2d Camera ) with the code we did here, or is that something totally different ?

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +2

      Hey BlackLight, thanks for the comment! In terms of setting up a 2D isometric camera, there is no "easy" way that I know of to enable this. You could go one of two ways to get this effect, the first method you would just create isometric sprites and then place them in the game scene, this is how most 2D games achieve this effect (BWdev has some great devlogs showing the process ua-cam.com/video/-UJUfkxSjHc/v-deo.html) . The other way you could achieve this is to build 3D models and the switch the camera to orthographic mode and rotate the camera to look at the origin from a 45 degree angle. I can't find any good tutorials on using the 3D models, but I would suggest going for the 2D approach, otherwise you have to code a lot more to get it working haha. Let me know if you have any other questions :)

  • @ParalyticAngel
    @ParalyticAngel Рік тому +1

    With the last changes, my square disappeared rather than moving right.^^ xDDDD

  • @RobarthVideo
    @RobarthVideo 4 роки тому

    Hello, I just wantet to mention, that you don't have to reset the viewMatrix or the projectionMatrix, instead you can just use projectionMatrix.setOrtho(...) and viewMatrix.setLootAt(...), this way you avoid unnecessary matrix multiplication, which is way more efficient.
    Otherwise great tutorial!

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +1

      Thanks @RobarthVideo! I'll definitely implement this in the next episode of the GameEngine series. (I'm writing the TODO now :D ).

  • @alexandruantoci2691
    @alexandruantoci2691 3 роки тому

    STILL LOVE YOU

  • @javiervelez534
    @javiervelez534 3 роки тому

    Very good tutorials! Thank you so much for doing this.
    I have a problem which is that my square blinks but i don't know what may be causing this issue.

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому

      That does sound like a strange problem haha. My initial thoughts are that maybe you're vsync is off? Check in the main loop and make sure that you have glfwSetSwapInterval(1); . Another thing it could be is that there is a problem with Java's System.nanoTime(); so you could replace all those with glfwGetTime(); and see if that fixes it as well. Let me know if either of those help :)

  • @lucasgiffuni6267
    @lucasgiffuni6267 4 роки тому

    im not very clear what type of camera we use, orhto or prespect, taking that very good tutorial.

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +2

      Hey Lucas, thanks for the comment! I had a few other questions on this tutorial, and after rewatching it I saw that I had left a few details out. We are using the orthographic camera, because we do not want perspective in a 2D game. I wrote an article that supplements this a bit more here: ambrosiogabe.github.io/on-cameras-in-opengl/ :)

    • @lucasgiffuni6267
      @lucasgiffuni6267 4 роки тому +1

      @@GamesWithGabe ah okay, but, its complicated implement an prespective camera in java? It has a some adventages

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +3

      Hey Lucas, it's not that complicated at all to implement the perspective camera. It's just the difference between a 3D view and a 2D view. You can think of Orthographic cameras as 2D cameras, and the perspective cameras as 3D cameras if that makes sense

  • @justins7796
    @justins7796 2 роки тому

    Love your desktop background, if it's not too much do you have a link to it? But awesome video, will have to rewatch to have it sink in

    • @GamesWithGabe
      @GamesWithGabe  2 роки тому

      Thanks Justin! And I'm glad I didn't change the filename, because I never would have been able to find the background again haha. But I think this is the one you were talking about wallpapershome.com/download-wallpapers/windows/abstract-nature-8k-21456.html :)

  • @ap0kalypsis233
    @ap0kalypsis233 3 роки тому +2

    Love the videos, I've been following them for the last couple of days now. However, I seem to be having a problem where the square comes up for a split second then disappears for good. Instead of moving around as you have. Any idea what could be causing that?

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому +1

      Hey @Ap0kalypsis! I'm glad you're enjoying the videos, and that sounds like it could be a lot of different problems haha. If you have a link to your code on Github or something I could take a look and let you know if I spot anything :)

    • @ap0kalypsis233
      @ap0kalypsis233 3 роки тому +2

      @@GamesWithGabe I have no idea what eventually fixed it but as we continued along with your videos stuff has been working just fine. Might have been some macOS specifics that i added myself sometime. Guess whatever it was you fixed eventually. Thanks for the reply man!

    • @kingkhoan6748
      @kingkhoan6748 2 роки тому

      I had this exact issue too, I eventually found the culprit. In Time.getTime(), I forgot to add parenthesis around System.nanoTime()-timeStarted, so it was consistently multiplying timeStarted first.

  • @drochit5901
    @drochit5901 2 роки тому +1

    1 секунду назад
    I have some problem, help me please! My square is twitching... not slowly going right, just twitching(like not enough FPS). And in last videos was problem when Gabe prints in console FPS , i had aboud 6-7 ... This is strange.. help me please if somebody know what the problem is

  • @HoangNguyen-yc3ov
    @HoangNguyen-yc3ov 3 роки тому +1

    I have read your article in the comment. I am quite confusing about how the coordinate system work on our screen, for the cameraUp is(0,1,0) and the cameraFront(0,0-1) so the camera is facing into our screen, but what happen if cameraUp is (1,0,0), the camera is somehow become upward :? Can you explain a little more specific about it. Thank you

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому +1

      Hey Konfusing Kat, sorry about the late response! I read this awhile ago and meant to reply but I couldn't find the time until now. Your thinking is right, so if you change the cameraUp to (1, 0, 0) then the front of the camera will be pointing up into the air. Basically the cameraUp is a vector we give to the math library, and the math library uses that to determine which way the top of the camera is pointing. So if you gave it something like (0.45, 0.45, 0) the camera would be pointing up at an angle. This is a lot easier to see in a 3D game engine, so if you want to experiment with this concept I would highly recommend playing around with the Unity engine. They have a function called Transform.LookAt (which you can see here: docs.unity3d.com/ScriptReference/Transform.LookAt.html) and you'll notice that the second method takes in (Transform target, Vector3 worldUp). The worldUp in this function is the same thing as cameraUp in this video, so if you change the worldUp then it will change the way the camera is looking at the target. Let me know if that makes a little bit more sense :)

    • @HoangNguyen-yc3ov
      @HoangNguyen-yc3ov 3 роки тому +1

      @@GamesWithGabe Thank for your reply. I respect what you are doing for us, knowledge, education. I don't mind waiting some days for a precious reply. I am understand now, thank you very much

    • @GamesWithGabe
      @GamesWithGabe  3 роки тому +1

      @@HoangNguyen-yc3ov I appreciate your understanding :). And i'm glad it makes more sense now. Don't worry about it taking awhile to understand some things too, some OpenGL concepts are just now "clicking" for me and I've been coding with it for the past 5 years haha

  • @Bandit-is8zi
    @Bandit-is8zi 9 місяців тому

    We always use ortho for 2d , or there is specific case when we can use for 3d?

    • @GamesWithGabe
      @GamesWithGabe  9 місяців тому

      There are definitely some cases where ortho mode in 3D helps out. AutoCAD software makes a lot of use of this. Blender uses it to help while modelling. Unreal uses it to help while you’re laying out maps. So there are multiple use cases, it just depends on what you’re trying to accomplish :)

  • @Bandit-is8zi
    @Bandit-is8zi 9 місяців тому

    camera.position.y -= dt * 24.0f;
    almost in corner

  • @hyperocket3279
    @hyperocket3279 4 роки тому

    Great series! Do you have a Twitter?

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +2

      Hey hype rocket, thanks for the comment!! And I don't have an active Twitter right now, I may add one in the future though

    • @hyperocket3279
      @hyperocket3279 4 роки тому

      @@GamesWithGabe all good, your vids have been a great help to me this past few days!

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +1

      @@hyperocket3279 that's awesome! I'm glad the videos are helping, feel free to reach out if you have any other questions 😊

  • @m4rt_
    @m4rt_ 2 роки тому +1

    holy shit you type fast, I have to pause the video almost every few seconds.

  • @tomb_1343
    @tomb_1343 4 роки тому +1

    Is it weird if my square moves to the left instead of to the right at the end? If it's an issue I want to fix it now before it causes more problems.

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +1

      Potentially haha. It could be a lot of different things that could be causing this. First of all, make sure your camera x is being subtracted in the update loop. If it is, then the next thing I would check is your getViewMatrix function. You could potentially have your z values flipped (e.g they're negative when they should be positive or vice versa) which means the camera would be looking at the square from behind. This would make it appear to be moving to the left instead of the right. Double check your code against this: github.com/codingminecraft/MarioUA-cam/blob/e5675ad646063ba2d869bdc183af02db37ad7e21/src/main/java/jade/Camera.java . If that is alright, then I would have to look a little deeper at your code :)

    • @tomb_1343
      @tomb_1343 4 роки тому

      @@GamesWithGabe All seems normal. Here it is if you want a peak github.com/TomB-134/EngineTest

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому

      Strange, I just tried to take a look at your code, but I got a 404. Could your repository be private?

    • @tomb_1343
      @tomb_1343 4 роки тому

      @@GamesWithGabe It isn't private anymore :p

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +2

      Thanks, I was able to take a look now :). It looks like line 79 of your LevelEditorScene, you have
      camera.position.x -= dt * -50.0f;
      This means that you're subtracting a negative, which is really the same as
      camera.position.x += dt * 50.0f;
      So the camera is moving to the right instead. If you change -50.0f to 50.0f, it should work properly. But good thing to double check because it could have been a much bigger problem haha :)

  • @sihleebuthelezi9231
    @sihleebuthelezi9231 3 роки тому

    my box is appearing top right corner, help

  • @drochit5901
    @drochit5901 2 роки тому

    well, i solve this problem, if somebody will face with it so you should to know - need to go in class Time and change the float type to double, and do this in all files where we use deltaTime and other things

  • @bencorrell
    @bencorrell 4 роки тому

    For some reason my program doesn't cap the framerate like it should. I noticed this when the camera moves because it's really choppy. When I print the FPS to the console it says it's "Infinity FPS" for a few frames and then it drops to 7. I've tried messing with the swap interval but that doesn't help. When I change it to 60 it limits the FPS to 1. Also, I don't know if this makes a difference or not but I'm running it in Eclipse on a laptop with integrated graphics. I've heard that it might have something to do with the GPU or vsync?

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +2

      Hey RubiksJr, I did some Googling and it doesn't seem like running an integrated graphics card should cause any problems (and when I first started openGL programming I was using the integrated graphics card on my old laptop as well). I can't really diagnose any other issues that might be happening without seeing the code, if you have it on Github I could take a look at it and see if it might be something else? The only thing I can think of is trying to set the glSwapBuffers to 0, which should allow unlimited frame rate.
      Another thing I would suggest you try is to download the code from the repository here: github.com/codingminecraft/MarioUA-cam/tree/e5675ad646063ba2d869bdc183af02db37ad7e21 and try to run that and see if you're still only getting the 7 fps :)

    • @bencorrell
      @bencorrell 4 роки тому

      @@GamesWithGabe github.com/Mythical-Atlas/gabe-debug Here's the code on GitHub. Thanks for helping me out.

    • @bencorrell
      @bencorrell 4 роки тому

      @@GamesWithGabe I just cloned your repo to my Eclipse workspace and it has the same issue... I'll try Visual Studio Code and see if that helps.

    • @GamesWithGabe
      @GamesWithGabe  4 роки тому +5

      Hey RubiksJr. this issue was super weird, and I finally realized that even my build was running at 14 FPS! It seems like System.getNanoTime() is broken for some reason in java... So I switched all my Time.getTime() calls to glfwGetTime() and that seems to have fixed it. I will mention this in the next tutorial also. Thanks for catching this bug :)

    • @bencorrell
      @bencorrell 4 роки тому +1

      @@GamesWithGabe Thanks so much for figuring this out! I just tried changing it and it works for me too! I wonder if it's an issue with the newest version of Java? I'm running JDK 14.