• Create Account

### #Actualtom_mai78101

Posted 20 July 2012 - 03:48 AM

Here's a picture showing a red ball inside a hollow, circular wall. Since both the circular wall and the ball have the (x,y) positions, we can manage to calculate the distance in between, shown as a thick black line.

Therefore, we have a collision detection algorithm, written below:

public boolean checkCollision(Hole h){
float dx = (this.position[0] + (this.diameter / 2)) - (h.x + 16);
float dy = (this.position[1] + (this.diameter / 2)) - (h.y + 16);
double distance = Math.hypot(dx, dy);
return distance < (double)(17 - this.diameter / 2);
}


The Hole class is the hollow, circular wall. (I tend to use simpler names for objects you see here.) The size of the Hole is 32, which is a constant size and also the diameter of the Hole. 16 is the radius. All (x,y) origins of the positions are given at the object's upper left corner.

Based on my understanding, I have heard that I need to use this:

double angle = Math.atan2(dx, dy);
ball.speed[0] = Math.cos(angle);
ball.speed[1] = Math.sin(angle);


In order to calculate how the red ball should move correctly. I tried, and it seems the ball always go out of bounds, so I'm not entirely sure what else I could do about it.

Can anyone enlighten me? Thanks in advance.

### #1tom_mai78101

Posted 20 July 2012 - 03:24 AM

Here's a picture showing a red ball inside a hollow, circular wall. Since both the circular wall and the ball have the (x,y) positions, we can manage to calculate the distance in between, shown as a thick black line.

Therefore, we have a collision detection algorithm, written below:

public boolean checkCollision(Hole h){
float dx = (this.position[0] + (this.diameter / 2)) - (h.x + 16);
float dy = (this.position[1] + (this.diameter / 2)) - (h.y + 16);
double distance = Math.hypot(dx, dy);
return distance < (double)(17 - this.diameter / 2);
}


The Hole class is the hollow, circular wall. (I tend to use simpler names for objects you see here.) The size of the Hole is 32, which is a constant size and also the diameter of the Hole. 16 is the radius. All (x,y) origins of the positions are given at the object's upper left corner.

Based on my understanding, I have heard that I need to use this:

double angle = Math.atan2(dx, dy);
ball.speed[0] = Math.cos(angle);
ball.speed[1] = Math.sin(angle);


In order to calculate how the red ball should move correctly. I tried, and it seems the ball always go out of bounds, so I'm not entirely sure what else I could do about it.

Can anyone enlighten me? Thanks in advance.

PARTNERS