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

Thread: intersects method

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

    Default intersects method

    ball.jpg

    here is what i trying to do and having no luck with it.

    1st check if ball and rectangle are touching
    2nd :
    if ball intersects rect from top than i want ball to go up.
    if ball intersect rect from bottom than i want ball to go down.
    if ball intersect rect from left than i want ball to go left.
    if ball intersect rect from right than i want ball to go right.

    1st part is easy. i'll i have to do is set up a getBounds() method on ball and rect. this is the red box u see on image. than test using intersects() method if two object are touching each other.

    2nd part i have no idea how to do. the intersects() method doesnt tell you if ball is touching the rectangle from top,bottom,right,or left. this is the problem!


    if(this.getBounds_ball().intersects(this.getBounds_box())) 
    {	
    	//How to test where the ball is touching the box!!!		
    }
    plz any ideas? every one i ask seem to explain to me how to test is two object are touching each other. BUT THIS IS NOT THE PROBLEM i already know how to do this. i need help with 2nd part. not 1st part.

    any ideas? plz let me know.


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

    Default Re: intersects method

    . the intersects() method doesnt tell you if ball is touching the rectangle from top,bottom,right,or left.
    You'll have to code that. Look at the locations of the centers of the shapes.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: intersects method

    ops u can igore the white line in image. i wasnt sure what i was doing there. but to find middle of shapes.

    (x+width/2)
    i am sure how to get started bc i do not even know what is the best approach.

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

    Default Re: intersects method

    Take a piece of paper and draw the shapes in the different positions and see what the relationships are between their centers.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: intersects method

    i am lil confuse how will that help me to find:

    if ball intersects rect from top than i want ball to go up.
    if ball intersect rect from bottom than i want ball to go down.
    if ball intersect rect from left than i want ball to go left.
    if ball intersect rect from right than i want ball to go right.

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

    Default Re: intersects method

    Look at the values for the x,y locations of the center of each of the shapes drawn on paper.
    For example:
    when one shape is above the other, what are their x and y values?
    when one shape is to the left of the other, what are their x and y values?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: intersects method

    if some one is having same problem as i do, here is a solution. i am not sure if this is the best way but it seem to be working for me. hope it helps.

    if(this.getBoundsBall().intersects(this.getBoundsRect())) 
    {	 
     
    	if(ballY+ballHeight >= rectY && ballY <= rectY)
    	{
    		System.out.println("collision from top");
    	}
    	if(ballY <= rectY+rectHeight && ballY+ballHeight >= rectY+rHeight)
    	{
    		System.out.println("collision from bottom");
    	}
    	if(ballX+ballWidth >= rectX && ballX <= rectX)
    	{
    		System.out.println("collision from left");
    	}
    	if(ballX <= rectX+rectWidth && ballX+ballWidth >= rectX+rectWidth)
    	{
    		System.out.println("collision from right");
    	}
    }

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM