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

Thread: Collision Detection Between Two Squares

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

    Post Collision Detection Between Two Squares

    Heya,

    I have a program with predefined objects(squares) playerObject & stillObject; that have methods to get their x,y coordinates called getX() & getY().
    playerObject: moves with the arrow keys of the keyboard via an already provided game engine.
    stillObject: is set in a location on the screen.
    Both objects have measurements of 100x100.

    My problem is I'm having trouble defining a collision detection statement, which needs to account for the stillObject changing positions in debugging tests, so it can't be hard-coded.
    So far what I have covers collisions between the top and left sides of the stillObject against playerObject by minusing the distance between them and if it's bigger than 0 than the objects have collided and the same for -200, which should be the other side of stillObject(which is what I thought), but it doesn't seem to be working that way for me.

    if ((((stillObject.getX() + 100) - playerObject.getX()) > 0) && ((stillObject.getY() + 100) - playerObject.getY()) > 0) {
    	    	collide = true;
    	    } 
     
    if ((((stillObject.getX() - 100) + playerObject.getX()) < -200) && ((stillObject.getY() - 100) + playerObject.getY()) < -200) {
                    collide = true;
    }

    Any push in the right direction would be great, thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Collision Detection Between Two Squares

    The way I debug these kinds of problems is by taking a piece of paper and drawing the shapes all the different ways that there can be a collision and then looking at the x,y & width,height of both shapes at each point of collision to come up with the tests needed to detect it.

    The Rectangle class has methods that could be used.

    Note on the posted code: You should NOT hard code the values 100 and 200 in the tests. Use variables that can be changed and whose names define what their values are.
    Last edited by Norm; August 22nd, 2012 at 07:20 AM.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Collision Detection Between Two Squares

    Quote Originally Posted by Norm View Post
    The way I debug these kinds of problems is by taking a piece of paper and drawing the shapes all the different ways that there can be a collision and then looking at the x,y & width,height of both shapes at each point of collision to come up with the tests needed to detect it.

    The Rectangle class has methods that could be used.

    Note on the posted code: You should NOT hard code the values 100 and 200 in the tests. Use variables that can be changed and whose names define what their values are.
    I tried drawing it out, and this is the best thing I could come up with. I thought using those values wouldn't count as hard coding since I'm calculating them along with the current x,y values( getX() & getY() ), so even if the coordinates of the stillObject changes, it should still work. The measurements of the squares aren't going to change, just their location so in theory these values would still work, right?

    The objects are drawn using a custom method, and it's not actually clear how they're drawn so I'm not sure whether it's drawn from the x,y being the top left corner like usual, or from the middle.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Collision Detection Between Two Squares

    measurements of the squares aren't going to change,
    In general its better to use variables. There might be a time when they will be changed.
    Using a named variable also makes the code more understandable.

    I'm not sure whether it's drawn from the x,y being the top left corner like usual, or from the middle.
    You will have to know which way is used so you know where the shapes are to be able to detect a collision.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection Between Two Squares

    Thanks Norm, I'll find out how the rectangles are drawn then I'll try reworking my code

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Collision Detection Between Two Squares

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/62432-collision-statement-between-squares.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection Between Two Squares

    Thanks for that, I didn't know there were cross posting rules. Usually I get arrogant posts just telling me I crossed posted and not mentioning any rules. I'll post up a link next time.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Collision Detection Between Two Squares

    This is a confusing way to compare locations:
    (stillObject.getX() - 100) + playerObject.getX()) < -200)
    what is the code supposed to be doing?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Collision Detection between Sprites and Images?
    By Montario in forum AWT / Java Swing
    Replies: 29
    Last Post: June 13th, 2012, 10:15 AM
  2. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 02:12 PM
  3. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  4. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM