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 5 of 5

Thread: Multiple Collision Detection in Invaders Game

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Multiple Collision Detection in Invaders Game

    Ok so I'm not getting any errors with this code or anything, but there is a small problem. Basically I'm making an invaders game and to hold the different invaders I have an 2D array called invaders (all of the Invader objects extend Rectangle2D.Double). The laser object is basically just another Rectangle2D.Double that is fired by the player at the invaders. I have a timer firing every 85 milliseconds, and every time it fires it does the following check to see if there is a collision between the laser and any of the invaders; if there is it removes the laser and replaces the hit invader with a blank one. The problem is that the way I'm currently checking them causes the program to miss the collisions of some of the invaders because it goes through the invaders 2D array row by row checking for collisions. Can anyone tell me a more efficient way of checking the collisions all at the same time (or at least quicker) than going through row by row?

    Here is the collision check:
    if(laser != null && laser.hasFired)
    	for(int row = 0;row < 5;row++)
    		for(int col = 0;col < 11;col++)
    			if(laser.getBounds2D().intersects(invaders[row][col].getBounds2D()) && invaders[row][col].alive){
    				laser.setFired(false);
    				invaders[row][col].isAlive(false);
    				score += 10;
    				for(int x = 0;x < 5;x++)
    				for(int y = 0;y < 11;y++)
    					invaders[x][y].increaseSpeed();
    			}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,218
    Thanks
    52
    Thanked 2,295 Times in 2,266 Posts

    Default Re: Multiple Collision Detection in Invaders Game

    miss the collisions of some of the invaders because it goes through the invaders 2D array row by row checking for collisions.
    Not sure how it misses collisions. If it tests all the contents of the array, how does it miss any?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Collision Detection in Invaders Game

    Quote Originally Posted by Norm View Post
    Not sure how it misses collisions. If it tests all the contents of the array, how does it miss any?
    I'm honestly not completely sure. I think it has something to do with the timing of the laser moving upwards and which collision it happens to be checking at that moment. It does register some collisions, just not all of them.

    --- Update ---

    The fact that the laser is also on a separate timer for movement with a shorter firing interval might have something to do with it. Could that be the problem?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,218
    Thanks
    52
    Thanked 2,295 Times in 2,266 Posts

    Default Re: Multiple Collision Detection in Invaders Game

    Are things moving while the test is made for collisions?

    Time how long it takes to check all the elements in the array? Is that too long for the animation?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    jm24 (January 25th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Collision Detection in Invaders Game

    Actually that was the problem haha. I messed with the timer a little bit and got it to work. Thanks anyway though

Similar Threads

  1. Trouble with Collision Detection
    By Gravity Games in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 6th, 2012, 02:00 PM
  2. Collision Detection Between Two Squares
    By thegreatzo in forum Loops & Control Statements
    Replies: 7
    Last Post: August 22nd, 2012, 09:13 AM
  3. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 04:11 PM
  4. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 02:12 PM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM