1

So i'm creating a frogger game and i've just about got everything but I'm running into one small problem, The collision system I have is very inconsistent, I've been trying to find out whats what but I can't quite figure out what it is. I want it to stop right when it hits the rectangles. any help would be appericated.

import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
public class frogger
{
   static double frogX = .5;
   static double frogY = .02;
   static double frogW = .02;
   static double frogH = .02;
   static double halfWidth = .05;
   static double halfHeight = .02;

   static double speeds[] = {.02, .03, .035 , .040, .043, .046, .060, .070};

   static Rectangle2D.Double[] rectangles = {new Rectangle2D.Double(.45 , .12 , .1 , .04), new Rectangle2D.Double(.45 , .27 , .1 , .04), new Rectangle2D.Double(.45 , .42 , .1 , .04),
   new Rectangle2D.Double(.45 , .57 , .1 , .04), new Rectangle2D.Double(.45 , .72 , .1 , .04), new Rectangle2D.Double(.45 , .87 , .1 , .04), new Rectangle2D.Double(.45 , .92 , .1 , .04)};

   public static void drawscene()
   {
      StdDraw.clear();
      StdDraw.setPenColor(StdDraw.GREEN);
      StdDraw.filledRectangle(frogX, frogY, frogW, frogH);      
      StdDraw.setPenColor(StdDraw.RED);
      for(int i = 0; i < rectangles.length; i++)
      {
      StdDraw.filledRectangle(rectangles[i].x + halfWidth, rectangles[i].y - halfHeight, halfWidth, halfHeight);
      }
      StdDraw.show(1000/24);  
   }



   public static void updateDirection() 
   {

     for(int i = 0; i < rectangles.length; i++){
         rectangles[i].x += speeds[i];
     }

     for(int i = 0; i < rectangles.length; i++){
         if(rectangles[i].x + halfWidth >= .98 || rectangles[i].x + halfWidth <= .02){
             speeds[i] *= -1;
         }
     }



      if (StdDraw.isKeyPressed(KeyEvent.VK_UP))
      {
          frogY += .01;
      }
      else if(StdDraw.isKeyPressed(KeyEvent.VK_DOWN))
      {
         frogY -= .01;
      }
      else if(StdDraw.isKeyPressed(KeyEvent.VK_LEFT))
      {
          frogX -= .01;
      }
      else if(StdDraw.isKeyPressed(KeyEvent.VK_RIGHT))
      {
          frogX += .01;
      }

   }

   public static void main(String[] args)  
   {
      outerloop: 
      while(true)
      {
         frogger.drawscene();
         frogger.updateDirection();
         if (frogX  >= 1 )
         { 
            break;
         }
         if (frogX  <= 0)
         {

            break;
         }   
         if(frogY >= 1)
         {
            break;
         }

         Rectangle2D.Double frog = new Rectangle2D.Double(frogX - frogW, frogY + frogH, frogW * 2, frogH * 2);

         for(int i = 0; i < rectangles.length; i++){
             if(frog.intersects(rectangles[i])){
                 break outerloop;
             }
         }

      }

      // put whatever you want to display at the end of the game here


   }
}
kyle
  • 9
  • 2

1 Answers1

1

So, I'm nearly positive it's related to the inaccuracy of doubles and floats in Java:

Why not use double or float to represent currency?

I've never used the library you're working with here. The recommended solution when you require finer accuracy is typically to work with the BigDecimal class. However, I can't say I'm sure of how you would do that using this library.

Community
  • 1
  • 1
Mike
  • 522
  • 2
  • 14
  • Yes I'm using StdDraw.java but i've downloaded std.lib and put it into my jgrasp – kyle Dec 04 '15 at 00:02
  • Just of curiousity, did you get this to work yet? I haven't done a game like this in a while. I took your code and converted all of your ```double```s into ```BigDecimal```s, performing all the operations using the ```BigDecimal``` functions and that seemed to resolve the inaccuracy to some degree. However, I didn't have time to wrap my head around the collision logic you had (ie. the collision logic wasn't performing as I expected). – Mike Dec 04 '15 at 11:10
  • Well the idea I had is where it draws the rectangles the checks if the frog has collided with the rectangles which makes the collision really in consist, I'm trying to figure out the big decimal class but we've never learned about it so I'm confuesd on how to implement it – kyle Dec 05 '15 at 04:48
  • @kyle I would start with a simpler version of this program and build it up from there. Your current program has multiple rectangles with various lengths and heights, travelling at various speeds, in various directions. All of these variables will make your job of debugging the situation much more difficult. I would suggest starting with one 'frog' and one stationary 'obstacle' with both a height and width that are easy to compute in your head (ie. length and width of 0.5 instead of 0.592). Print their coordinates to `System.out` as you move your frog. See what you can deduce from that. – Mike Dec 05 '15 at 12:04