Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  Image - Z Coordinate?  (Read 1647 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Devvie


Projects: 1



« Posted 2011-06-27 11:54:11 »

Hey!
Is there some way to draw my Image with a Z Coordinate not just x,y?
So that my spaceship is behind some rocks or before.

Im using Slick and g.drawImage().
Offline pitbuller
« Reply #1 - Posted 2011-06-27 12:13:33 »

Hey!
Is there some way to draw my Image with a Z Coordinate not just x,y?
So that my spaceship is behind some rocks or before.

Im using Slick and g.drawImage().
You need to draw them at right order. If you want use z coordinate make some array for all images and just sort it every time before drawing and then you can have this feature but with "some" overhead.
Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #2 - Posted 2011-06-27 12:45:43 »

So I can put all my images in an array and sort it?!
But how?

Like I have
Image Spaceship,Rock,DestroyesShip,Rock2.

How do I do it? I will look at Google.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2011-06-27 13:23:23 »

Make class that contains image and z coordinate. Then implement comparable interface.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
class ImageZ implements Comparable<Object>{
 Image im;
 float z;
 
 ImageZ(Image tmpIm,float tempZ){
  this.im = tmpIm;
  this.z = tempZ;
 }

public final float compareTo(Object o) {
 float tmpFloat = ((Cloud) o).getZ();
 return this.z - tmpFloat;
 }

float getZ() {
 return this.z;
 }
}


Then add draw method or get image method.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-06-27 14:59:16 »

Make class that contains image and z coordinate. Then implement comparable interface.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
class ImageZ implements Comparable<Object>{
 Image im;
 float z;
 
 ImageZ(Image tmpIm,float tempZ){
  this.im = tmpIm;
  this.z = tempZ;
 }

public final float compareTo(Object o) {
 float tmpFloat = ((Cloud) o).getZ();
 return this.z - tmpFloat;
 }

float getZ() {
 return this.z;
 }
}


Then add draw method or get image method.
Why give Comparable a parameterized type of Object? Why use a float for z?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class ImageZ implements Comparable<ImageZ> {
    private Image img;
    private int z;
   
    public ImageZ(Image img, int z) {
        this.img = img;
        this.z = z;
    }
   
    public int compareTo(ImageZ i) {
        return z-i.z;
    }

    public Image getImage() {
        return img;
    }
   
    public int getZ() {
        return z;
    }
}

Then, if your ImageZ objects are in an array, you use the Arrays.sort method, if you use an ArrayList, you use the Collections.sort method. Both are in java.util.

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #5 - Posted 2011-06-27 18:38:39 »

If you search this forum you can see some of the older topics on this problem. I made one a year ago I think, and I solved it with a comparator interface.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2011-06-28 17:24:43 »

You want float for Z because every other OpenGL param is a float, and what if you put something at depth 4 and something at depth 5, and then realize you want something at depth 4.5? You'd have to redo everything with an integer. I don't see any reason why you wouldn't use a float.

I'm not sure why he did Comparable<Object> either. Maybe he's used to Java 1.4-isms (everything being Object) and is just trying to avoid compiler warnings? I agree it seems like doing things half way.

See my work:
OTC Software
Offline pitbuller
« Reply #7 - Posted 2011-06-28 17:36:31 »

You want float for Z because every other OpenGL param is a float, and what if you put something at depth 4 and something at depth 5, and then realize you want something at depth 4.5? You'd have to redo everything with an integer. I don't see any reason why you wouldn't use a float.

I'm not sure why he did Comparable<Object> either. Maybe he's used to Java 1.4-isms (everything being Object) and is just trying to avoid compiler warnings? I agree it seems like doing things half way.

I agree with you at float. It just has to be float if there is any dynamic ordering. About (Object): That was just some legacy code without any thought.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2011-06-28 17:39:12 »

You want float for Z because every other OpenGL param is a float, and what if you put something at depth 4 and something at depth 5, and then realize you want something at depth 4.5? You'd have to redo everything with an integer. I don't see any reason why you wouldn't use a float.

I'm not sure why he did Comparable<Object> either. Maybe he's used to Java 1.4-isms (everything being Object) and is just trying to avoid compiler warnings? I agree it seems like doing things half way.

I agree with you at float. It just has to be float if there is any dynamic ordering. About (Object): That was just some legacy code without any thought.
Makes sense. I know some people who actually swear by keeping everything as Object and doing typecasting everywhere, and they can never really explain their reasoning to me. Usually it's "flexibility," but it's like... why?

See my work:
OTC Software
Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #9 - Posted 2011-08-05 10:32:20 »

Hm the guys in the other board dont know how to do it so I ask here.
So I made what you say and order all my images at Y Coords.

But how do I draw them now?

I made it like this but then it doesnt draw right becauce the actor wont be drawing all the time.
1  
2  
3  
4  
5  
6  
7  
for(int x=0;x<bank.ZOrder.length;x++)
                {
                if(!bank.ZOrder[x].getEvent().equals(bank.Player))
                  drawNPC(bank.ZOrder[x].getEvent(),g);
               else
                   drawPlayer(g);
                }

So how do I make it right? Do you know it?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #10 - Posted 2011-08-05 20:37:49 »

I don't understand what you're trying to do.

See my work:
OTC Software
Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #11 - Posted 2011-08-06 17:11:22 »



I want to draw the Event in the right Order.
If the player is behind some NPC or in Front of them.

So I put my Image in an Array an Sort them like some people told me in another board. But how do I draw it know?
So it is like on the Screen.
Offline Cero
« Reply #12 - Posted 2011-08-06 17:24:38 »

you can take a Z value
then sort by Z and draw lowest Z first (or the other way around)

but you can also: get the bottom y value (y+height) and sort is by this value

because thats the point where something/someone stands

you cant use just the y, because things have different sizes.

Once sorted you iterate over the images, in order, and draw the onces in the background first.
using (y+height) approach you would draw lower values first

bottom (y+height) is just a quick approach I can think of, might not work if there are flying things and such.

But it doesnt matter, if you CAN give an Z value for every object, you sort it and render in order from lowest to highest by that value (might be from highest to lowest, depends on you implementation)

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #13 - Posted 2011-08-06 17:44:19 »

Hello again!  Cool

EDIT: Read here first!
I didn't explain myself. When you have this problem, you don't want to artificially make up a new Z-layer. What you do, is to just figure out the order the things needs to be drawn in. Here, you always want to draw terrain first, then characters. Your problem, is "Which character is upper-most on screen?"  because that character should be drawn first!
You can have the entities in a Collection, and you can sort that so when you loop through it, it will hand you the characters starting with the upper-most one, and then slowly getting to the buttom of the screen. This ensures that the correct characters overlaps eachother. Now read what I originally posted.

If you have the entites that needs to be drawn in a Collection, you can very easily use Collection.sort, and get that *bluub* sorted according to whatever you want. It has to be variables you're 100% sure all entries in the Collection posses, so you'd need a Collection like ArrayList<Entity>, and you can sort according to variables in Entity.

You have two options for sorting:
Either, you implement an interface in Entity, so that it can be sorted, because Entity isn't known by Java to be an object that has a natural sorting. That's the Comparator-interface. Avoid that for this situation.
The other option, is to make a Comparator that compares the entities, according to a variable they have. In this case: The Y-coord.

Here's a class that can sort collections according to both X, and Y-coords.
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  
public class CompareUtils
{
   private static final Comparator<LivingEntity> Y_ORDER = new Comparator<LivingEntity>()
   {
      public int compare(LivingEntity e1, LivingEntity e2)
      {
         return Double.compare(e1.getY(), e2.getY());
      }
   };
   
   private static final Comparator<LivingEntity> X_ORDER = new Comparator<LivingEntity>()
   {
      public int compare(LivingEntity e1, LivingEntity e2)
      {
         return Double.compare(e2.getX(), e1.getX());
      }
   };

   public static Comparator<LivingEntity> getXOrder()
   {
      return X_ORDER;
   }

   public static Comparator<LivingEntity> getYOrder()
   {
      return Y_ORDER;
   }
}


Then, when you have a collection you want sorted, you simply make a call this way:
1  
Collections.sort(entities, CompareUtils.getYOrder());

The field "entities" is your collection here, with all your characters.

Then you can draw your characters like this:
1  
2  
3  
for(Entity e : entites) {
   //draw code here
}

and it will automaticly be in that correct order.

Cheers!

EDIT: In the code I gave you, you might want to change X from a double to an int if you're using tiles. Characters don't stand of half-tiles or 1/3 tiles, do they? Wink
The code is from a minor jam last week, and it was used with free-pixel movement.

Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #14 - Posted 2011-08-07 10:19:37 »

Thanks for your time.
As I wrote, I already sort everything xD"
I just dont know how to draw it now.

I draw it this way:
for(int x=0;x<bank.ZOrder.length;x++)  //x<numberofEventsYCoords
                {
                if(!bank.ZOrder
  • .getEvent().equals(bank.Player)) //IF ZOrder
  • is NOT the player then draw NPC
                 drawNPC(bank.ZOrder
  • .getEvent(),g);
              else
                   drawPlayer(g);  //ELSE draw the Player.
                }

So now I got this error becauce the player is not drawed all the time:
http://www.youtube.com/watch?v=5UqYfkUl6Ew

Maybe I know how to do it, I will test it later *grrr*

Offline Cero
« Reply #15 - Posted 2011-08-07 12:01:43 »

Another german guy huh =P

Well, once you have sorted it ( and its really sorted the right way) you can render it with

1  
2  
3  
for(Entity e : entites) { 
   //draw code here
}
As Mads said

I don't even know why you differentiate drawNPC and drawPlayer

Both things, npcs and player, and even enemies, ought to be the same class, or super class. I mean you can do a sub-class for each, but everyone should be the same super class, and be rendered in the same way

Entity npcs[]
Entity Player

When you create a sorted list, just throw the player in there too, sorted, and then just draw all

1  
2  
3  
for(Entity e : entites) { 
   e.draw(g); // or whatever
}


Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #16 - Posted 2011-08-07 12:11:50 »

Thanks for your time.
As I wrote, I already sort everything xD"
I just dont know how to draw it now.

I draw it this way:
for(int x=0;x<bank.ZOrder.length;x++)  //x<numberofEventsYCoords
                {
                if(!bank.ZOrder
  • .getEvent().equals(bank.Player)) //IF ZOrder
  • is NOT the player then draw NPC
                 drawNPC(bank.ZOrder
  • .getEvent(),g);
              else
                   drawPlayer(g);  //ELSE draw the Player.
                }

So now I got this error becauce the player is not drawed all the time:
http://www.youtube.com/watch?v=5UqYfkUl6Ew

Maybe I know how to do it, I will test it later *grrr*



If it's sorted just draw it like this:
1  
2  
3  
for(Character charac : characters) {
   // draw code here for the character (charac)
}

That looks all the Character in the characters-list.

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.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

toopeicgaming1999 (108 views)
2014-11-26 15:22:04

toopeicgaming1999 (94 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
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!