• Create Account

# Ball to Circular Wall Interior Collision Response

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1tom_mai78101  Members   -  Reputation: 478

Like
0Likes
Like

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.

### #2tom_mai78101  Members   -  Reputation: 478

Like
0Likes
Like

Posted 31 July 2012 - 03:22 AM

Fixed. I have resolve this problem.

public void reflectResponse() {
for (int i = 0; i <= 1; i++) {
position[i] -= speed[i];
speed[i] *= 0.992f;
}
}

public void reflect(Hole h){
//R = -2*(V dot N)*N + V
//N is normalized.
double nx = (this.position[0]+this.diameter/2) - (h.x+16);
double ny = (this.position[1]+this.diameter/2) - (h.y+16);
double nd = Math.hypot(nx, ny);
if (nd == 0)
nd = 1;
nx /= nd;
ny /= nd;
double dotProduct = this.speed[0]*nx+this.speed[1]*ny;
this.speed[0] += (float)(-2*dotProduct*nx);
this.speed[1] += (float)(-2*dotProduct*ny);
}


Basically, what it does is, the ball will go along the outer circle, reflecting among the normals it touches. The ball itself will go in a circle along the outer circle. I decided this would be a better option, than to mimic the actual reflection of bouncing off the outer circle's interior wall.

But I would hope that someone else can improve the code I've given above, so that balls can bounce off round walls like how light travels, instead of rolling around the wall.

### #3DerekEhrman  Members   -  Reputation: 127

Like
0Likes
Like

Posted 31 July 2012 - 12:39 PM

First, I would store a radius instead of a diameter (why do the division constantly when what you really want is the radius? Just do the division once and you have the value to work from!).

Second, I would make the hole itself also have a radius member so you can remove what appear to be *magic numbers* (where is 17 from?). I know the radius is constant in this example, but it would be much cleaner and cooler to support any sized hole, right?

Now, your core collision detection is algorithm is doing more work than it needs to be (hypot uses the very expensive square root function) and is not an accurate representation of the object (I am assuming this is why you used the value 17 instead of the actual diameter value in your test?)

I think you can greatly simplify the algorithm to something more like:
* calculate the distance from the center of the hole to the center of the circle
* if this value is greater than or equal to the radius of the hole, we have a collision

### #4tom_mai78101  Members   -  Reputation: 478

Like
0Likes
Like

Posted 01 August 2012 - 03:06 AM

The radius part is possible for me to simplify the calculations.

The constant value 17 is a constant that I tend to tweak around before.

About the "calculating the distance from center of hole to center of circle" part, What else should I use to calculate the distance, other than doing a Math.sqrt(dx*dx + dy*dy)? Isn't Math.hypot() easier? Or doing (distance*distance > dx*dx+dy*dy) is easier on the CPU?

The collision detection process is done above. However my center of hole and center of circle were mostly set on the top left corner of the bitmap. It's not entirely in the center of the bitmap, so I did a little more math to calculate the exact center of both of the bitmaps (Hole and Ball).

And I was afraid of the collision response (reflect()) method is not actually optimized. I guess I can put that to rest for a bit.

### #5tom_mai78101  Members   -  Reputation: 478

Like
0Likes
Like

Posted 21 August 2012 - 05:13 AM

Apparently, I probably didn't let anyone else know about this algorithm's weakness. That's right, there's a weakness. And today, I've just realized it and made a fix, which made this algorithm even better, as if it's like magic.

The only weakness to this algorithm, is "how you execute your collision detection". If you detected an object A has collided with circle B, there will be three types of conditions you may or may not allow.
• Object A is inside of Circle B.
• Object A is on Circle B.
• Object A is outside of Circle B.
With this algorithm,
• If Condition #1 is met, Object A will bounce in Circle B. This is what I always wanted in my first post.
• If Condition #2 is met, Object A will circle the center of Circle B; will move along the rim of Circle B. Think of this as an object flying around in a hollow rim of the circle. It's just that the object itself clipped through the walls of circle B.
• If Condition #3 is met, Object A will bounce away from Circle B, like in billiards.
Just a heads up, and hoped you enjoyed it.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS