Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [RPG] How do you move all of these things?  (Read 1427 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-05-19 02:58:07 »


I've been thinking about this since I first managed to get player/map movement working a while back. How do you move the map, camera, npcs and the player's character? The way that I've been doing it seems really crude and it puts a lot of limitations on how I do things, I'd like to change the way I'm doing it atm but I'm not sure of any other ways to do this.

The way that I currently do everything is... The player's character is always centered on the screen, everything else is moved around the player to make it look as if the player is moving. By doing it this way I'm unable to allow the window to be resized because it'll throw off all of the math which I use to draw everything, the player's speed can only be slightly increased before it goes into super speed and looks terrible and I won't be able to do decent looking cutscenes with this setup.

So, how would you go about moving the player's character, camera, npcs, etc... in a 2D game? (Just theory, I'm not looking for any code.)
Offline DrZoidberg

JGO Coder

Medals: 21

« Reply #1 - Posted 2013-05-19 03:20:17 »

The player, all the characters and objects in the game and the camera all have a position. That position is not the absolute position on the screen. The positions are always relative to the game map. So when you move or zoom the camera the position of everything else in the game is not affected by that. The screen coordinates are calculated only in the rendering code.
Offline nerb
« Reply #2 - Posted 2013-05-19 06:01:50 »

Exactly what DrZoidberg said. Decouple the view from the world.

For example, treat your view as a 'camera', which itself has a position. In your case the camera position is updated when the player position changes, keeping the player in the centre of the screen. When it comes to rendering everything, just convert their world coordinates to 'view' coordinates. In a general case this will be worldCoords - cameraCoords.

It will free you from your issues with screen resizing, unless of course you wanted to implement scaling.

This will make your camera object more versatile and reusable too. For example, you could implement scrolling, or control the camera for certain animations or events. Furthermore, you no longer have to update every object in the world when your player moves; you are just updating the player and camera position.

EDIT: (And a bit off-topic). I see you are working on an RPG. As your game expands in size, you could use this camera approach with your world design to optimize things a bit. For example, with a small game there are no problems iterating through each object and converting world coordinates to screen coordinates, and drawing them if they are within the viewport. However if you had a lot of objects you may end up with a lot of unneeded calculations. You could for example break your world/map up into 'segments'. You could then quickly calculate which segments are currently within or near the viewport, and only do the 'world to screen' calculations for objects that are in those segments, and hence are potentially visible.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-05-19 21:58:02 »

Thanks for the replies, I was actually had a similar idea floating around in my head. I'll give it a shot and see if it works out. ^.^  Smiley
Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (159 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!