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  
  2D RPG graphics. Walking behind and in front of objects  (Read 20084 times)
0 Members and 1 Guest are viewing this topic.
Offline Genius Coder

Junior Devvie

Medals: 1
Exp: 4 years

« Posted 2014-02-04 19:12:02 »

So im currently working on an RPG that has the player and the currently walk in front if all objects. What would be the best way to add a depth of field so that the player can walk infront of an object if they are infront of it and behind it if they are behind. I was thinking that i could just print two images for each item and have the correct one displayed depending on the players position

I love making games...
Offline UprightPath
« Reply #1 - Posted 2014-02-04 19:26:48 »

Fake it. Change the order of drawing objects (The moving ones) such that they're drawn after the things that they are in front of, but before things they are in back of. If it's an isometric game, this means you draw objects based on their Y value and that you draw tiles/floors/walls before other objects.

I'm not sure how "good" this will be for a Java 2D based engine though. Your idea of making several different sprites for the same object may work better (It's certainly viable for a small number of variations) but the number of different sprites might be high if you have a lot of dynamic objects (Players moving, Monsters moving, dropped items, etc.).

The main difference is the cost trade off. The 'Fake it' could, on a map with a large number of dynamic objects, require more calls to draw. Your idea would require the same number of draw calls (one per tile/object) but it increases the number of sprites you need by around 2 (This is if EVERY object in the game has an overlap image). If there are more than just the player on the screen moving then this number could be drastically larger.

I, personally, can't say which is better in a Java 2D environment but someone else might be able to. (Do note that at the same time it might not matter! Depending on the size of your drawing space-- the number of tiles/objects you're drawing-- the difference might be small enough not to matter.)

Offline nerb
« Reply #2 - Posted 2014-02-04 23:30:26 »

If you had some sort of collection of all objects and entities (such as an ArrayList), then you could sort them prior to each render using a custom comparator, sorted by their z value (which as UprightPath has pointed out, this would likely be the y value).

I'm not sure what the performance would be like, but you could probably work out a few optimisations, particularly if the game is tile-based. (i.e. associating objects and entities with tiles, and only 'depth sorting' for visible tiles).

Anyway, that's generally how I've got around it in the past. Although I've only ever tried it in small, menial projects. No idea how it would go with a decent sized project.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ctomni231

JGO Wizard

Medals: 99
Projects: 1
Exp: 7 years

Not a glitch. Just have a lil' pixelexia...

« Reply #3 - Posted 2014-02-05 02:13:05 »

Yeah, if you are talking about 2D java, a simple array to change the draw order of the Y elements should be enough.

If the main character is the only entity moving in the world and the camera follows it, you can actually focus your drawings around the character. In other words, you leave a small radius around the character untouched, and then only redraw the elements around the character that need the depth.

If you have many moving entities, an array based on Y location can work. I like this method a lot because when you are dealing with many entities, you can reorder the entities on the fly as you need them drawn.

As the above poster said, you have to fake it. It is all about the draw order and positioning that shows depth.

Offline Genius Coder

Junior Devvie

Medals: 1
Exp: 4 years

« Reply #4 - Posted 2014-02-05 06:14:55 »

Im confused as to how i could reorder the objects in an array list. Any source?

I love making games...
Offline nerb
« Reply #5 - Posted 2014-02-05 06:28:02 »

Im confused as to how i could reorder the objects in an array list. Any source?

Here's what appears to be an alright tutorial:

Essentially you'll need to create and use your own implementation of this:

If you need more resources to work with, google "Java comparators", "java sorting ArrayLists" or similar. There's a fair bit out there on the subject.

PS. Here is a quick and dirty comparator I previously used to sort some 'Sheep' objects based on their y position:

import java.util.*;

public class SheepZComparator implements Comparator<Sheep> {

   public int compare(Sheep lhs, Sheep rhs) {

      if(lhs.yPos < rhs.yPos) {
         return -1;
      } else if (lhs.yPos > rhs.yPos) {
         return 1;
      } else {
         return 0;


Then in the update loop, prior to rendering, I call:

Collections.sort(sheep, comparator);

Where 'sheep' is an ArrayList of Sheep, and 'comparator' is the aforementioned Comparator. In your case, you may want all game objects and entities to implement a common interface, and base your comparator on that.
Offline Genius Coder

Junior Devvie

Medals: 1
Exp: 4 years

« Reply #6 - Posted 2014-02-05 20:27:18 »

I ended up using my own wersion where i just drew the image correspondent to the players position but thankyou for all the advice

I love making games...
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!