1 Reply Latest reply: Oct 31, 2009 7:46 AM by 843853 RSS

    CompareTo for two boxes(rotated rectangles)

    843853
      I basically have a bunch of cubes in two point perspective that need to be drawn to the screen. I have all of the points calculated in an array for each cube, but I only need the first four points(bottom of the box) to actually tell if its front or not. The bottom of the cubes never overlap thanks to collision detection. What I am trying to do is create a compareTo method in the class that will always put the box that is in front later in an array, so it will be drawn to the screen after the one behind it. All of the cubes are below the horizon line

      [Program full screen|http://img.photobucket.com/albums/v148/jakster4u/1-2.jpg]
      [Example overlapping|http://img.photobucket.com/albums/v148/jakster4u/3.jpg]

      This is what I have so far but it only works some of the time and I was hoping their was a more elegant way than adding more if statements.
      The point at 0 in the array would be the lower point of the bottom, 1 the right point of the bottom, 2 the left point of the bottom, 3 the upper point of the bottom.
      public int compareTo(Object that)
          {
               if(that instanceof IsoBox)
               {
                    //same point
                    if(this.points[0][0]==((IsoBox)that).points[0][0] && this.points[0][1]==((IsoBox)that).points[0][1])
                         return 0;
                    //left of point less than y
                    if(this.points[1][0]<((IsoBox)that).points[0][0] && this.points[1][1]>((IsoBox)that).points[2][1])
                         return 1;
                    //right of point and less than y
                    if(this.points[1][0]>((IsoBox)that).points[0][0] && this.points[2][1]>((IsoBox)that).points[1][1])
                         return 1;     
                    return -1;
               }
               return -1;
          }
      [Base that needs comparing|http://mad.printf.net/Perspective/PerspectiveDiagrams/PerspectiveDiagram2.jpg]

      I'm doing this for fun so any help or suggestions would be welcome.

      Also all of the boxes are placed randomly, with random sizes using collision detection at the start. A player can move the clear box around the other cubes, and fire bullets off the of the other cubes.

      [Bullets!|http://img.photobucket.com/albums/v148/jakster4u/2.jpg]