Results 1 to 10 of 10
  1. #1
    rmac362 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Bullet/shield collision detection question

    I am working on a space invaders clone just for fun and learning purposes. I am currently working on the shields that hover above the space ship. I have the shields drawn and collision detection works, sort of. Each shield is a grid of fragments in an Array List. each fragment is supposed to disappear when hit with a bullet. Some of them do just that. but certain rows of the shields are ignored by the bullets.After shooting the shields, I end up with this:
    Bullet/shield collision detection question-shieldexample.jpg
    The 2nd,3rd,4th,6th, and 7th row seem to be ignored by the bullets. I am using rectangles and the intersect method to detect collisions.
    Has anyone ever seen something like this before?

    The following code handles the collision:

    Java Code:
     public void collision(ArrayList<Shield> shields, ArrayList<Rectangle> shieldRect) {
    
            for (int i = 0; i < shields.size(); i++) {
    
                for (int j = 0; j < bullets.size(); j++) {
    
                    Rectangle bullet = bullets.get(j).bounds();
    
                    collision = shieldRect.get(i).intersects(bullet);
                    shields.get(i).setHit(collision);
                    if (shields.get(i).getHit()) {
                        shields.remove(i);
                        shieldRect.remove(i);
                        i--;
    
                        if (collision) {
                            bullets.remove(j);
                            j--;
                            collision = false;
                        }
    
                    }
                }
            }
        }
    Attached GamePanel class txt file.

    Is there anything about this code that looks funny or might be causing this problem?
    Attached Files Attached Files
    Last edited by rmac362; 01-14-2015 at 04:09 PM. Reason: attached txt file containing GamePanel class . couldn't put it in the post. "page not found" error.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Bullet/shield collision detection question

    What have you done to debug this yourself? When you don't detect a collision do you print the bounds of the bullet and the shield to see what is going on? This may not be related by why are you checking for a collision inside the block of code starting at line 16? To get there (line 11) a collision must have happened.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    rmac362 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Re: Bullet/shield collision detection question

    Thanks for the reply. The code outputs the position of each fragment of the first shield and its corresponding rectangle, and reprints them when I shoot the shields(after the hit shield is removed). All rectangles seem to be in the right place. I guess I can track the bullet rectangles as well to see if anything is funny. It just seems like the bullet rectangle is intact because it hits the shield in the fifth row after skipping over the 2nd,3rd, and fourth.


    Collision is run in the GameUpdate() portion of the attached class, so the collision is checked for on every tick.

    Edit: went back and did what you suggested. I track the bullet rectangles bounds info. At each tick, the rectangle moves with the bullet. This gave me an another idea. I slowed the bullets way down and it now hits all of the shields. I now have a new problem, the bullets are way too slow. But at least its a NEW problem. lol.

    lesson learned : never assume anything when debugging.

    Thanks so much for your help! If you have any general ideas as to how I might speed the bullets back up AND hit all of the shields, I would love to hear your suggestions. Is it just a matter of compromising or is there definitely a better way to do this? I am pretty new to all of this and you have already helped me a ton! Thanks again.
    Last edited by rmac362; 01-14-2015 at 04:58 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Bullet/shield collision detection question

    I assume you are moving the bullets by incrementing them. By how much? And are you using any of your own threads in the application).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    rmac362 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Re: Bullet/shield collision detection question

    I originally had the bullets moving at a speed of 20 per tick. I have since adjusted it to go much slower. and changed the size of the shield fragments and found an ok compromise where the bullets aren't too slow but still hits and removes all of the shields.

    I assume that if I had a better understanding of threads, I would be able to make this work without the compromises?




    This is some of the code from run(). I should mention that I wrote this code based on a tutorial for a different game. I do not understand it as well as i probably should.



    Java Code:
    long startTime;
            long URDTimeMillis;
            long waitTime;
            long totalTime = 0;
    
            int frameCount = 0;
            int maxFrameCount = 30;
    
            long targetTime = 1000 / FPS;
            // game loop
            while (running) {
                startTime = System.nanoTime();
                gameUpdate();
                gameRender();
                gameDraw();
    
                URDTimeMillis = (System.nanoTime() - startTime) / 1000000;
    
                waitTime = targetTime - URDTimeMillis;
    
                try {
                    Thread.sleep(waitTime);
                } catch (Exception e) {
                }
    
                totalTime += System.nanoTime() - startTime;
                frameCount++;
                if (frameCount == maxFrameCount) {
                    averageFPS = 1000.0 / ((totalTime / frameCount) / 1000000);
                    frameCount = 0;
                    totalTime = 0;
                }
    
            }
    
        }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Bullet/shield collision detection question

    One thing you can do is to use one of the timer classes. I presume you know about the event handling system. So you can set up a timer and have it fire off an event every N milliseconds. The event handler can then do the repainting and other book keeping as necessary. There are two timer classes. One is java.util.Timer and the other is javax.swing.Timer. Read about them both to see which best fits your environment. And I recommend you write a small program to just get familiar with the timers before integrating them into your program.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Bullet/shield collision detection question

    Good learning project. Now you have to ask yourself a question: do you settle for making the problem go away with your current workaround of slowing things down, or do you really want to learn something here and properly fix it so your collision detection works even when the bullets move at ludicrous speed and thus from one frame to the next can completely skip over one or more blocks?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    rmac362 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Re: Bullet/shield collision detection question

    jim829,
    Thanks! I will start there.

    gimbal2,
    Definitely. Learning is why I'm here! Ludicrous speed?! My brains might go into my feet! lol

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Bullet/shield collision detection question

    Well consider this then: collision detection is not generally done after you move the bullet when its already too late; it is done before you move. "I am going to move from position X to position Y. Do I hit something along the way?"

    The steps in your code then would be:

    - calculate position Y based on position X
    - collision(X, Y)
    - update()

    While right now in your code it is:

    - update()
    - collision(Y)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    rmac362 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Re: Bullet/shield collision detection question

    So, just to see if I understand you.

    -I could change collision() to use the current Y coordinate of the bullet's rectangle.

    -Since the preferred speed of the bullet is 20 per update,
    i would check for collision on each bullet position from the current Y all the way through y+20

    -and if rectangles intersect in any of these positions the shield gets tagged as Hit
    then the bullet is actually moved and bullets/shields are removed as necessary.

    basically instead of only checking for collision where the bullet currently is, i'm also checking for collision everywhere the bullet is going to be until the next update?

Similar Threads

  1. Collision detection
    By NewCsStudent in forum New To Java
    Replies: 8
    Last Post: 05-01-2014, 10:05 AM
  2. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 01:45 PM
  3. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 04:29 PM
  4. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  5. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •