Java Game Programming #10 – Camera Class

Produce your own Java game from scratch with this very simple, yet very powerful course available right now

In this video we add the camera class into our game. Now we can roam around the level all we want. If you have any questions then be sure to leave a comment or contact me on twitter; realtutsgml. If you learned something then be sure to leave a like, comment, and favorite.

Have you ever wanted to create games? Have you ever gotten fed up with it being to difficult? Well now is the time to thank me and possibly subscribe because you have just found the channel for you! Game Maker Tutorials, Java Game Programming, Unity3D Tutorials, Batch, C++ and much much more! This is the channel for you, the one stop shop for an exploration of your hidden talent as a game developer. Unleash your potential and go wild with imagination when you finally figure out how to make any game you want!

Visit CodingMadeSimple for more exclusive tutorials and get the help you need to succeed as your very own indie game developer!

Follow me on twitter for exclusive content and interaction with me!

Game Maker Studio: Programming
Game Maker Studio: Tutorial
Java Programming
Game Programming
Game Tutorial
Programming Tutorial






32 responses to “Java Game Programming #10 – Camera Class”

  1. MatthewJackson 265 Avatar

    my camera is inverted. pls help

    nevermind i fix it. i forget the "-" on the "player".

    my mistake was: x = player.getX() + Game.WIDTH/2;

    and i fix it to: x = -player.getX() + Game.WIDTH/2;

  2. JavaFromAsia Avatar

    Anyone plz help me fix this glitch when i intialize the camera class my game become sotilare!!!!

  3. Ozterkvlt Avatar

    awesome, how would you snap the camera though? so it wont show outside the borders of the level?

  4. Xenko Vence Avatar

    How do you get the camera to stop at x=0, x=the width of the game, and the same for y, so that you can't see beyond the stage?

  5. MCT3M Avatar

    Guys: If you wanna have a Y coord in the camera class, change the x and y variables to int and just cast for the player coords. If you use floating point integers (float) for the Y coordinates, it'll start shaking and being wonky.

  6. Frank Marsh Avatar

    I think it'd be more efficient if the player was not in the handler's list of objects and was rendered separately.

  7. Frank Marsh Avatar

    I'm gonna code wall-jumping!

  8. Jabba The Hutt Avatar

    Could u please make a tweening video

  9. nguoi nghe doc sach Avatar

    Why some time you added or removed some code before you start your lecture ? It make me confuse somtime why don't you doing this job during you lecture so that we can easilly to follow ? btw could we have some code of all the lecture pls?

  10. Mitch Avatar

    In the previous video you said you would explain how to fix the sticky key problem of the character sometimes stopping moving.

    Could you explain this please?

  11. space1188 Avatar

    can you please explain g2d.translate for me. i dont understand

  12. chri991000 Avatar

    if you want the smooth camera:
    float xTarg = -player.getX() + Game.WIDTH/2;

    x += (xTarg – x) * (Number between 0 and 1)

  13. MLech Avatar

    Tip for eclipse users. Hold alt+shift + s and than select generate getters and setters. Makes life easier.

  14. Theo Retisch Avatar

    For everyone whos camera is way faster than the player make sure u delete the line in ur "game" class.
    g2d.translate(cam.getX(), cam.getY()); <———— DELETE THIS!!!!!!!!!

     //DRAW HERE

    g.fillRect(0, 0, getWidth(), getHeight());

    g2d.translate(cam.getX(), cam.getY()); //begin of cam


    g2d.translate(cam.getX(), cam.getY()); // end of cam

  15. FinBoyXD Avatar

    You should be writing the minus marks in Class Game render() -method this way:

    g2d.translate(-camera.getX(), -camera.getY()); //begin of camera
    g2d.translate(camera.getX(), camera.getY()); //end of camera


    g2d.translate(camera.getX(), camera.getY()); //begin of camera
    g2d.translate(-camera.getX(), -camera.getY()); //end of camera

    it's then easier to control the camera's tick() -method: 
    x = player.getX() – Game.WIDTH/2;

  16. Hanz Hhh Avatar

    What is this actually called, Game Scrolling?

  17. Adam Brodin Avatar

    This was really cool 🙂
    Great tutorials overall!

  18. Galaxy Box Avatar

    Could anyone help me for some reason my camera goes way faster than my player and the left side it kinda glitches out and when i move right the blocks start glitching out and my player just glitches and the player falls right through the block leaving a blue trail if you want i could send you my code

  19. GV Rainfall Avatar

    Wouldn't it be better to pass the camera instance to the handler's tick method and check if the object is a player in the loop that ticks all the objects?

    Also, I think it's "tweening", not tweAning 😉

  20. Diego MC Avatar

    it doesn't work for me

  21. Alan Burns Avatar

    I came here for a camera script, I left with more.

  22. Epicnerd73 Avatar

    Awesome! Thanks 😀

  23. Yasif Mohamed Avatar

    can we add a actual rear camera view as game background ? i coudnt solve it ..please help

  24. PlayThroughChildhood Avatar

    The camera doesn't move at all for me. i rewatched the video about 3 times and it still didn't work. 

  25. JRPG MAKER Avatar

    i have an bug when i move the player the camera go inverted

  26. Jami Lu Avatar

    Last video you said to show smooth movement in this case. When a "movement button" is pressed really fast. It stops moving, and then continues just as thinking a while. But I never saw you to show, Please can you show it?

  27. Guy Shuster Avatar

    Hey, I didnt understand how the translate command works,can you explain it more deeply?

  28. Ross Binden Avatar

    The translate method in Graphics2D solves an issue I've been having for a couple of days – thanks.

  29. Jonathan Runeke Avatar

    Hey, i have a problem. When i run the program and start to move, my player starts to shake a lot. I have no idea how to fix this and thats why i posted this here, so if someone here in the comments know whats wrong PLEASE respond to this. Thanks!

  30. XceriasNationPyah Avatar

    Christ sake i did this and At the start the game is half the screen and the other half is the game but you cant control it.. If you start moving to the right you fix it but than the blocks on the bottom start getting messed up.. whelp.. is there something wrong with my x = -player.getX() + Game.WIDTH/2;

  31. XceriasNationPyah Avatar

    Could you show us how to make our maps with arrays so we can use multiple types of blocks and stuff.. Because I dont just want grass blocks I want stone and wood boxes 

Leave a Reply

Your email address will not be published. Required fields are marked *