Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  compareTo(obj o) help - My implementation of it sucks :D  (Read 1411 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2011-01-26 19:30:45 »

Hey JavaGaming!  Cheesy

Never before has something worked for me, until just recently. This is because of you guys, and explosively learning
my ways with games programming  Tongue

I'm over stuff like walking in a tilemap, which you know I have had trouble with.

So a massive thanks to you  Kiss

Now to my question:
I got a fingerpoint (a huge one) to how I should proporly Z-order my tiled-landscape-thing (it
s top-down, like Zelda). I'm using the comperable interface on my "entity" object to do it.
I have a feeling I might be doing it in a bad way though, since it does not always work..
Have a look:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public int compareTo(Entity o) {
        if (this.coordY > o.coordY) {
            return 1;
        } else if (this.coordY == o.coordY) {
            if (this.priority > o.priority) {
                return 1;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }


It's designed to make the objects in the scene that has the biggest Y value, the first on the list (a linkedlist btw).
If they have the same Y, the priority int should count, and the one with the biggest priority should be first.

..I do have a feeling I might be doing this waaaay wrong, hence the half-funcionality..

Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #1 - Posted 2011-01-26 19:47:31 »

1  
2  
3  
4  
5  
   public int compareTo(Entity o) {
      if (coordY == o.coordY)
         return priority - o.priority;
      return coordY - o.coordY;
   }

beware, this is just a guess and maybe stupid

lots of sillystupid games: http://www.emaggame.com
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2011-01-26 19:53:37 »

1  
2  
3  
4  
5  
   public int compareTo(Entity o) {
      if (coordY == o.coordY)
         return priority - o.priority;
      return coordY - o.coordY;
   }

beware, this is just a guess and maybe stupid
That looks correct to me.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2011-01-26 19:56:32 »

1  
2  
3  
4  
5  
   public int compareTo(Entity o) {
      if (coordY == o.coordY)
         return priority - o.priority;
      return coordY - o.coordY;
   }

beware, this is just a guess and maybe stupid

Actually, this is almost correct. The problem is that if compareTo returns 0, it is assumed that the objects are equal which they are not. If you were to use your compareTo implementation in a TreeSet lots of objects would not not make it into the set, as the TreeSet thinks it already has them.

You might have better luck with this one:
1  
2  
3  
4  
5  
   public int compareTo(Entity o) {
      if(this==o || this.equals(o)) return 0;
      int ret = (coordY == o.coordY) ? (priority - o.priority) : (coordY - o.coordY);
      return (ret==0) ? 1 : ret;
   }

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

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2011-01-26 20:29:23 »

Thank you very much guys Cheesy I found out that my movement was not working correctly, upon holding the key down and that was actually causing the bug.
Again, that just showed me a different bug in the compareTo method, and then we're back  Tongue
Rivens code solves that  Smiley



Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2011-01-27 19:55:14 »

Actually, this is almost correct. The problem is that if compareTo returns 0, it is assumed that the objects are equal which they are not. If you were to use your compareTo implementation in a TreeSet lots of objects would not not make it into the set, as the TreeSet thinks it already has them.

You might have better luck with this one:
1  
2  
3  
4  
5  
   public int compareTo(Entity o) {
      if(this==o || this.equals(o)) return 0;
      int ret = (coordY == o.coordY) ? (priority - o.priority) : (coordY - o.coordY);
      return (ret==0) ? 1 : ret;
   }

Aah, of course. I forgot that caveat. It is sort of weird, though, to return 1 for something that is equivalent but not equal. Then again there's not much else you can do. I guess in this case that sorts it to the right which is fine.

See my work:
OTC Software
Offline nsigma
« Reply #6 - Posted 2011-01-28 11:07:06 »

1  
2  
3  
4  
5  
   public int compareTo(Entity o) {
      if(this==o || this.equals(o)) return 0;
      int ret = (coordY == o.coordY) ? (priority - o.priority) : (coordY - o.coordY);
      return (ret==0) ? 1 : ret;
   }

I'm not sure this quite fulfils the requirements for compareTo() either as it isn't transitive.  For x and y that have equal coords and priority, x.compareTo(y) should be opposite to y.compareTo(x), but they'll both return 1.  Whether this is an issue depends on how you use the Entity.  I had an issue with this a while back, and ended up implementing the last line something like -
1  
return (ret==0) ? hashCode() - o.hashCode() : ret;

Not sure if anyone's got a better idea.

Incidentally, while checking the docs for the exact wording for compareTo() I noticed that it doesn't strictly have to be consistent with equals() - guess it depends where you're using it but seems like a bad habit to get in to!

Yes, yes, I know, the pedants are revolting!  Grin

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2011-01-28 15:34:01 »

Quote
It's designed to make the objects in the scene that has the biggest Y value, the first on the list (a linkedlist btw).
If they have the same Y, the priority int should count, and the one with the biggest priority should be first.

What you said here makes me think you want the list to be sorted in descending order (to re-phrase, elements at the end have low coordY and priority). Doesn't Java sort lists in ascending order by default, so shouldn't we reverse the implementation in Riven's compareTo() so that it returns -1 when this.coordY > o.coordY?

Like so?
1  
2  
3  
4  
5  
public int compareTo(Entity o) {
    if(this==o || this.equals(o)) return 0;
    int ret = (coordY == o.coordY) ? (o.priority - priority) : (o.coordY - coordY);
    return (ret==0) ? 1 : ret;
}

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.

CogWheelz (17 views)
2014-07-30 21:08:39

Riven (23 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43
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!