Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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 503 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-05-19 02:58:07 »

Hey,

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

Senior Duke


Medals: 15



« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!