Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Collision Check Error

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down Collision Check Error

    Okay, I've had this error for days and I just cannot place how to fix it.

    To pinpoint where the error was I put in debug messages in my code so that I knew where in the code it was freezing. However, it seems to freeze at different points:
    "Start of second loop: 5, 6"
    "Start of second loop: 12, 11"
    "Made dead enemy"
    "Start of second loop: 2, 36"
    "Start of second loop: 10, 17"
    "Start of first loop loop: 0"
    "Start of first loop loop: 5"
    My game involves a spaceship shooting lasers at enemies (original, I know) and this is my code for the collision check:

    //collisions
                    // - laser and enemy
                // i = current laser
                // ii = current enemy
                //cycles through all lasers
                for (i=0; i<totallasers; i++)
                {
                    //debug messages are drawn to screen
                    message = "Start of first loop: " + i;
                    //if current laser is active (lasers become inactive when they leave the screen or collide with an enemy
                    if (laser[i].active)
                    //cycles through all enemies
                    for (ii=0; ii<totalenemies; ii++)
                    {
                        //if current enemy is active (enemies become inactive when they leave the screen or collide with a laser
                        if (enemy[ii].active) {
                            message = "Start of second loop: " + i + ", " +  ii;
                            //if the centre point of the laser is in the enemies rectangular boundary
                            if (scr.PointCheck(laser[i].x+12,laser[i].y+2,enemy[ii].x,enemy[ii].y,enemy[ii].width,enemy[ii].height))
                            {
                                message = "Testing collision between " + i + " and " + ii;
                                //creates a "dead" enemy of the same type and in the same position as the current enemy that has just been hit
                                deadEnemy[totaldeadenemies] = new DeadEnemy(enemy[ii].type,enemy[ii].x,enemy[ii].y);
                                message = "Made deadenemy";
                                totaldeadenemies++;
     
                                //"destroys" current laser and enemy
                                laser[i].active=false;
                                enemy[ii].active=false;
                            }
                        }
                        message = "End of second loop";
                    }
                    message = "End of first loop";
                }

    The code for PointCheck() is this:

        public boolean PointCheck(int x, int y, int x2, int y2, int width, int height)
        {
            if (x>x2 && x<x2+width && y>y2 && y<y2+height) 
                return true;
            else
                return false;
        }


    The error seems to happen randomly so I can't really pinpoint what is wrong with the code. I added the "message = "blahblahblah"" which gets drawn to the screen so that I can see which part of the loops it freezes in, but it isn't the same one each time (it's frozen at first loop, second loop, testing collisions...).

    Any help would be really really appreciated. I've tried for days but I can't solve this problem and it's preventing me from working on the rest of the game

    If you need me to explain the code a bit more just say and I'll try to tell you what means what etc (some things I've done might not be fully self-explanatory).

    Thanks in advance ;D
    Last edited by Josh Yaxley; July 12th, 2010 at 11:20 AM. Reason: updating code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    Can you show the console showing what you have printed out as debug trace?

    I don't see any println() statements in the code you've posted.

    some things I've done might not be fully self-explanatory
    Program comments would help there. Put the comments in the program and then anyone can understand without having to ask you.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    Can you show the console showing what you have printed out as debug trace?
    The game runs as an applet and for some reason println() doesn't seem to work with it, so instead I draw the debug messages to the screen.

    I'll just play the game a bit until I get an error to give an example of the errors it gives:
    "Start of second loop: 5, 6"
    "Start of second loop: 12, 11"
    "Made dead enemy"
    "Start of second loop: 2, 36"
    "Start of second loop: 10, 17"
    "Start of first loop loop: 0"
    "Start of first loop loop: 5"

    As you can see it isn't always the same error, or even the same place in the code that it freezes each time.

    I've just updated the code in the first post to include comments that explain what is going on.

    Can anyone help me?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    The println() output goes into the browser's Java console.
    As you can see
    Sorry, your output example list makes no sense. I don't know what is an error vs normal output.

    Perhaps if you added some more text to the messages: "i=" + i and "ii=" + ii
    to put labels on the values shown
    Last edited by Norm; July 12th, 2010 at 11:02 AM.

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    Normal output would be "First loop broken" because this is at the end of the code, so when it is running normally it displays "First loop broken".

    However, when the game freezes, it displays a different message (examples are the ones in my previous reply).

    I think this means that the game is freezing in between the code and that it can't reach the end of the code because the message doesn't change (hence why I think the error is in that part of the code).

    Do you understand now?

  6. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    Normal output would be "First loop broken" because this is at the end of the code, so when it is running normally it displays "First loop broken".

    However, when the game freezes, it displays a different message (examples are the ones in my previous reply).

    I think this means that the game is freezing in between the code and that it can't reach the end of the code because the message doesn't change (hence why I think the error is in that part of the code).

    Do you understand now?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

     if (ii>=totalenemies) break;
     message = "Second loop broken";

    Can you explain what the message means? It looks like the message is output if you do NOT break.
    A better debug would be to show ii:
    message = "Second loop broken ii=" + ii;

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    If your explanation were included in with the output to show which ones show what, it would help.
    Having to scroll up and down as I read your later postings to see what you are talking about makes it hard to follow.

  9. #9
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    Okay I've updated my first post to show some of the error messages.

    And the whole "breaking the loop" thing isn't actually necessary (because the for loop will end anyway) it was just something I put in in the vain hope that it might make a difference but it didn't so I've taken it out now to avoid confusion (because I know that's not where the error is).

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    Ok, can you run a test now and copy the contents of the Java console and paste it here with comments showing where you think the problem is?

    A hanging/freezing program usually is an unending loop.

  11. #11
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    The console doesn't show anything, that's why I had to program my own debug messaging thing (basically just "message = 'string';" and "g.drawString(message,x,y);".

    The errors I get are the ones shown in the first post.

  12. #12
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    I've told you already it doesn't seem to write anything to the console which is why I programmed my own debug messaging into the code so that I could see whereabouts it was freezing.

    If it helps I'm using BlueJ to write, compile and run my code and my computer is an iMac running Mac OS X.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    OK, I know nothing about an iMac.
    Do you have any command prompt windows for running java from a commandline so you can see println() output?

  14. #14
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    There is a terminal but I've never used it.

    When I want to run my code I just use the run option in BlueJ

  15. #15
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    Oh wait I've figured out how to use the console but it gives me a reallyyyyy long message, shall I copy and paste all of it?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    Well for now, can you copy the first 20 and the last 20 lines with ... between to see if it could have any useful information.

  17. #17
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    Well I've had a look and the message it shows when the game freezes is this:

    Exception in thread "Thread-21" java.lang.ArrayIndexOutOfBoundsException: 19
    	at Main.run(Main.java:192)
    	at java.lang.Thread.run(Thread.java:637)



    Although it is sometimes slightly different:

    Exception in thread "Thread-25" java.lang.NullPointerException
    	at Main.run(Main.java:192)
    	at java.lang.Thread.run(Thread.java:637)

    Thank you for your helps so far.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    Those messages look very important for helping you find your problems.

  19. #19
    Junior Member
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Check Error

    Hmmm.. I can't make any sense of them though :/

    How am I supposed to interpret console messages?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collision Check Error

    The error messages you posted show what happened and where. For example:
    Thread-21" java.lang.ArrayIndexOutOfBoundsException: 19
    at Main.run(Main.java:192)
    On line 192 in Main there is an array index of 19 that is out of bounds (the array has too few elements)

Similar Threads

  1. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM
  2. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM
  3. bounding box collision
    By beechy34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2010, 08:58 AM
  4. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM