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:

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:

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

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

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:

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.

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.

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

Quote:

Originally Posted by

**sunde887** 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:

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.

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

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

Quote:

Originally Posted by

**Naxix** 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