• Post Reply Bookmark Topic Watch Topic
  • New Topic

2D Camera tracking Player  RSS feed

 
Will Ashton
Greenhorn
Posts: 22
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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!
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!