Results 1 to 5 of 5
  1. #1
    Naxix is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default How to find the edges/border of a rect? (java.awt)

    So, i made an rect constructor, and made the rect object, but i can't get the math right, on how to find the edges/border of it. I need to "detect" when something hits one of the four egdes. But my code makes both objects unable to move at all.

    I got this:
    Java Code:
    	public boolean toPaddle(){
    		
    		if(x_pos < paddle.getX() || x_pos > paddle.getWidth() || y_pos < paddle.getY() || y_pos > paddle.getHeight()){
    			
    			speedx = (rand.nextInt(2)+1)-4;
    			return true;
    			
    		}
    		return false;
    	}
    and the methods for my paddle egdes:

    Java Code:
    	public int getX(){
    		return p_width;
    	}
    	
    	public int getY(){
    		return p_height;
    	}
    	
    	public int getWidth(){
    		
    		int w = x_pos + p_width;
    		return w;
    	}
    	
    	public int getHeight(){
    		
    		int h = y_pos + p_height;
    		return h;
    	}
    Regards Naxix

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to find the edges/border of a rect? (java.awt)

    The code for the paddle edges is incorrect. They should return x_pos, y_pos, x_pos+p_width and y_pos+p_height respectively.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: How to find the edges/border of a rect? (java.awt)

    To detect if something hits a box you want to determine if the point lays on the box. Given some rectangle with an x and a y coordinate, the points which lie on the rectangle are as follows:
    Java Code:
    (x, y), (x+1, y), (x+2, y)... (x+width, y)
    (x, y+1), (x, y+2)... (x, y+height)
    (x+width, y), (x+width, y+1), (x+width, y+2)... (x+width, y+height)
    (x, y+height), (x+1, y+height), (x+2, y+height)... (x+width, y+height)
    The easiest way to determine if something hits is to check if the point is in range.

    Java Code:
    if point is in range (x, x+width)
      determine whether the points y is close to top or bottom
    if point is in range (y, y+height)
      determine whether the  points x is close to left or right
    I wish I hadn't been lazy writing my tutorial, it would probably help you out with this.

    My best advice is to visualize the box and the (x,y) point you want to check and figure it out by hand.

  4. #4
    Naxix is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to find the edges/border of a rect? (java.awt)

    Quote Originally Posted by sunde887 View Post
    To detect if something hits a box you want to determine if the point lays on the box. Given some rectangle with an x and a y coordinate, the points which lie on the rectangle are as follows:
    Java Code:
    (x, y), (x+1, y), (x+2, y)... (x+width, y)
    (x, y+1), (x, y+2)... (x, y+height)
    (x+width, y), (x+width, y+1), (x+width, y+2)... (x+width, y+height)
    (x, y+height), (x+1, y+height), (x+2, y+height)... (x+width, y+height)[
    The easiest way to determine if something hits is to check if the point is in range.

    Java Code:
    if point is in range (x, x+width)
      determine whether the points y is close to top or bottom
    if point is in range (y, y+height)
      determine whether the  points x is close to left or right
    I wish I hadn't been lazy writing my tutorial, it would probably help you out with this.

    My best advice is to visualize the box and the (x,y) point you want to check and figure it out by hand.
    So, how would i make the if-statement check if the object is in range? Since it's a square, i'm not sure how to do it, but i guess something with math.sqrt, would be involve?

    Regards Naxix
    Last edited by Naxix; 10-17-2011 at 08:51 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to find the edges/border of a rect? (java.awt)

    Quote Originally Posted by Naxix View Post
    So, how would i make the if-statement check if the object is in range? Since it's a square, i'm not sure how to do it, but i guess something with math.sqrt, would be involve?

    Regards Naxix
    Don't complicate matter more than necessary; if you hav a rectangle defined by xl, xh, yl and yh (the x and y coordinates of the corners) and a point x,y, that point is inside the rectangle if xl <= x <= xh and yl <= y <= yh. You should be able to implement that in Java ....

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 10-15-2011, 11:56 PM
  2. User-Controlled Tree Edges
    By robbie.26 in forum AWT / Swing
    Replies: 4
    Last Post: 07-03-2011, 07:21 AM
  3. creating nodes and edges in java
    By ankit1801 in forum New To Java
    Replies: 4
    Last Post: 03-19-2011, 07:15 AM
  4. MouseEntered to rect
    By g6pd in forum New To Java
    Replies: 3
    Last Post: 03-10-2011, 02:52 PM
  5. Rectangle with rounded edges??
    By orchid in forum Java 2D
    Replies: 1
    Last Post: 05-10-2007, 03:31 AM

Posting Permissions

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