Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

2D Camera tracking Player

Posts: 27
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'm trying to program another 2D-Top-Down game in Java and for the first time ever my gameworld (currently represented by a huge BufferedImage, later hopefully by around 200x200 16x16px tiles) is larger than my screen. Therefore I tried to make the camera move around, so that the player is always in the middle of the screen. Pretty basic.
My current approach sets my gamepanel to the size of the gameworld and moves the camera around with translating the graphics-object. This part works principally fine, but it totally screws up my MouseMotion-Events, with which I am controlling the players location. I tried to fix the screwed up MouseEvent.getX()/getY() results in various ways, but I can't get it to work properly
My question is, how do you guys track the player with the camera? Is there another approach I could try or do you know any tutorials/blogs which cover this topic? I'm totally clueless
I hope that this question isn't considered to general. If you need more data feel free to ask, but note, that I'm writing from my cellphone, since my laptop is short on internet in the moment.

Thank you!
Posts: 1042
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 3 ways of dealing with this:

1 - make your player stationary, middle of the screen, then move your background accordingly. For this you need an animated scrolling viewport that is painted onto your display object from your main map and a game array that your player moves though.
2 - movement on current screen until you go off a side. this you have to translate according to your movement off the screen and onto the next screen. (this works well for games to have on screen maps and grid systems to allow mouse click movements)
if you press up and go off the top, your X position does not change, but your Y has to be changed to the bottom of the adjacent screen when it is loaded.
3 - limited local motion in a screen with animated scrolling when offset is more than some pre-chosen distance.
move say half way up or across the screen, then scrolling starts in the appropriate direction.

In all of these methods, you have to keep a separate array of your playing field to track where your player is located.

#2 IMO is the easiest to implement--you make tiles and populate a map with the tiles. Each tile object knows it's own domain and how to interact with the player. You can also implement mouse click movement and easiest path for movement algorithms with this.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic