Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (806)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  Z-Ordering technique?  (Read 5268 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob Pickens
« Posted 2014-12-03 23:23:07 »

I'm trying to make a game with similar mechanics to Dead Pixel:



Minus the zombies.

Any how, in the game when a player is lower on the screen the zombies are drawn behind him and when he is higher than the zombies the zombie are drawn in front of him. I tried to prototype a system like that myself and i called it a DepthRenderer. This is what I did:

DepthRenderer Class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
import org.newdawn.slick.Graphics;

import com.pickens.enemies.EnemyController;
import com.pickens.player.Player;

public class DepthRenderer {

   Player p;
   EnemyController ec;
   
   public DepthRenderer(Player p, EnemyController ec) {
      this.p = p;
      this.ec = ec;
   }
   
   public void render(Graphics g) {
      ec.drawBack(g);
      p.render(g);
      ec.drawFront(g);
   }
   
   public void update() {
      ec.updateDepth();
   }
   
}



EnemyController Class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
public class EnemyController {

   public ArrayList<Enemy> enemies = new ArrayList<Enemy>();
   public ArrayList<Enemy> front = new ArrayList<Enemy>();
   public ArrayList<Enemy> back = new ArrayList<Enemy>();
   
   Enemy temp;
   
   Player p;
   
   public EnemyController(Player p) {
      this.p = p;
   }
   
   public void drawBack(Graphics g) {
      for(int i = 0; i < back.size(); i++) {
         temp = back.get(i);
         
         temp.render(g);
      }
   }
   
   public void drawFront(Graphics g) {
      for(int i = 0; i < front.size(); i++) {
         temp = front.get(i);
         
         temp.render(g);
      }
   }
   
   public void update() {
      for(int i = 0; i < enemies.size(); i++) {
         temp = enemies.get(i);
         
         temp.update();
      }
   }
   
   public void updateDepth() {
      for(int i = 0; i < enemies.size(); i++) {
         temp = enemies.get(i);
         
         if(temp.getBounds().getY() >= p.getBounds().getY()) {
            front.add(temp);
            back.remove(temp);
            System.out.println("Added front");
         }else{
            System.out.println("Added back");
            back.add(temp);
            front.remove(temp);
         }
      }
   }
   
   public void addEnemy(Enemy e) {
      enemies.add(e);
   }
   
   public void removeEnemy(Enemy e) {
      enemies.remove(e);
   }
   
}


However the game only draws the enemies in front of the player. The player can never get in front of the enemies even if he is lower than the enemy. So what am I doing wrong? In my mind I thought this would work. Tongue

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-12-03 23:40:52 »

This is called z-ordering btw.

I recommend ditching the "front" and "back" and instead storing a z-value with each entity. (just like you would with x or y)

On render:
    sort list of all entities (by z value), including player -> render them

"Updating depth" is handled automatically by the sorting.
Offline Jacob Pickens
« Reply #2 - Posted 2014-12-03 23:42:34 »

This is called z-ordering btw.

I recommend ditching the "front" and "back" and instead storing a z-value with each entity.

On render:
    sort list of all entities (by z value), including player -> render them

"Updating depth" is handled automatically by the sorting.

How would you go about this ordering? With a for-loop or something?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #3 - Posted 2014-12-03 23:43:45 »

Collections.sort();


No need to get fancy.


(This does however make a copy array each time, which hurts the soul)
Offline Agro
« Reply #4 - Posted 2014-12-04 00:42:06 »

instead of storing a z value just sort by -y (negative y).

Offline Jacob Pickens
« Reply #5 - Posted 2014-12-04 00:44:03 »

What would be the code for sorting by y?

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #6 - Posted 2014-12-04 00:50:34 »

Collections.sort(entities, (e1, e2) -> Integer.compare(e2.y, e1.y));


Or, pre Java8/implement Comparable:

1  
2  
3  
4  
5  
6  
7  
8  
// in Entity class, which implements Comparable<Entity>
public int compareTo(Entity e) {
    return Integer.compare(e.y, y);
}

...

Collections.sort(entities);


Could also use a anonymous class to replace the lambda, but that's nasty.

Note that if you implement Comparable, all ordered collections will order your entities this way.
Offline Jacob Pickens
« Reply #7 - Posted 2014-12-04 01:04:41 »

Thanks that worked! +1 to both of you. Thanks a lot!

Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2014-12-04 14:48:52 »

Or, pre Java8/implement Comparable:

1  
2  
3  
4  
5  
6  
7  
8  
// in Entity class, which implements Comparable<Entity>
public int compareTo(Entity e) {
    return Integer.compare(e.y, y);
}

...

Collections.sort(entities);


Offtopic:

java.util.Comparable
is such an anti-pattern for anything but Numbers... Emo There isn't a natural order for Entities. Use
java.util.Comparator
and let which ever code is going to sort those objects, determine which ordering strategy is appropriate:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
Comparator<Entity> yOrder = new Comparator<Entity>() {
    public int compare(Entity a, Entity b) {
        return Integer.compare(a.y, b.y);
    }
};
Collections.sort(entities, yOrder);



Comparator<Entity> strengthOrder = new Comparator<Entity>() {
    public int compare(Entity a, Entity b) {
        return Integer.compare(a.shield + a.hp, b.shield + b.hp);
    }
};
Collections.sort(entities, strengthOrder);

* Riven rambles...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline appel

JGO Wizard


Medals: 80
Projects: 4


I always win!


« Reply #9 - Posted 2014-12-08 23:50:36 »

I did a small framework called Apollo which somewhat solves this: http://gamadu.com/apollo/

You could assign a z-indexing to each entity and then maintain a rendering array ordered by that z-index, you would recalculate that rendering array each time there's a change, like a entity being created or deleted.

I decided to have a two layers of sorting, first I put the entities into their corresponding bucket, and then sort them inside each of their buckets. That improves performance and also allows for more easy layering of the entities, like you have grouping buckets like "foreground" or "background" or "effects". For some of these groups order matters for some they don't. Explosions in the effects group don't care if they're beneath of above other explosions if they're rendered additively so rendering them makes so sense.

But in the end this is quite simple, just z-index rendering. Your game looks simple enough so you don't have to go down the rabbit hole of useless optimizing.

Btw. you can just render according to Y-value.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (587 views)
2019-09-04 15:33:17

hadezbladez (5528 views)
2018-11-16 13:46:03

hadezbladez (2410 views)
2018-11-16 13:41:33

hadezbladez (5790 views)
2018-11-16 13:35:35

hadezbladez (1233 views)
2018-11-16 13:32:03

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

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

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

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

nelsongames (5125 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!