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

Thread: collision

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default collision

    i am running into problem here. I have a arraylist called rect which store all bounds for ground. i am looping though it and checking for collision between player and arraylist bounds.

    tile map 2d array:

    1 = player
    2 = ground
    0 = background
     
    1,0,0,0
    2,2,0,2
    I want to print "FLAG 1" if its collisied but if its not than print "FLAG 2". I am not 100% sure but i think the problem is the loop.

    if arraylist size is 10 than, if player is standing on ground than it will print "FLAG 1" but than it will print 9 times "FLAG 2"


    public void collision(Player p){
    this.getBounds();for(int i = 0; i < rect.size(); i++){
        if(p.getBounds().intersects(this.rect.get(i)))
              System.out.println("FLAG 1");
        else
                     System.out.println("FLAG 2");
    }
    }

    not sure if i made my question clear. I want to only print "Flag 1" if its touch the ground. and when its not touching than i only want to print "Flag 2"


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: collision

    You're code is checking to see if the player intersects every part of the ground instead of if it intersects anywhere on the ground (big difference).

    A good method to do the latter:

    1. Initialize a flag which assumes the player is not intersecting anywhere with the ground.
    2. For each part of the ground, check to see if it intersects. If it does, set the flag and stop searching.
    3. If the flag indicates the player is intersecting the ground, output "Flag 1". Otherwise, output "Flag 2".

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: collision

    o found the problem i think. the problem is here. i am testing player intersects with the whole arraylist. so if arraylist size is 20 than it will print "hi" once and it will print nothing 19 times bc no else.

    for(int i = 0; i < rect.size(); i++){
    if(p.getBounds().intersects(this.rect.get(i)))
    System.out.println("FLAG 1");
    else
    System.out.println("FLAG 2");
    }

    so i was thinking some thing like this. this way it will only test if player is intersect any rect in arraylist.
    for(int i = 0; i < rect.size(); i++)
    {
          if(p.getBounds().intersects(this.rect.get(i))){
               flag = true;
              break;
          }
        else
           flag = false;
    }
     
    System.out.println(flag);
    if(flag )
       System.out.println("Flag1");
    else
    System.out.println("flag2");
    now flag prints:
    true
    false
    false
    false
    ....

    it should prints true, true, true, true, true, bc player is touching the ground rect from arraylist.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: collision

    Quote Originally Posted by game06 View Post
    it should prints true, true, true, true, true, bc player is touching the ground rect from arraylist.
    is touching, or is intersecting? Are you sure they are actually intersecting and not beside one another? Check the values of the bounding boxes manually when you believe they should be intersecting and see what the numbers show

Similar Threads

  1. Help with collision!
    By Dr.Code in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 4th, 2012, 04:09 PM
  2. Tile Collision
    By Tim_Wilson in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 07:40 PM
  3. Need help with object collision
    By xDeadScYthe in forum AWT / Java Swing
    Replies: 3
    Last Post: March 23rd, 2012, 07:10 AM
  4. Collision
    By risen375 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 28th, 2011, 07:02 AM
  5. Collision Detecting
    By iams3b in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 01:48 AM