tom_mai78101 693 Report post Posted July 20, 2012 [img]http://i.imgur.com/E1jRy.png[/img] 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: [CODE] 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); } [/CODE] 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: [CODE] double angle = Math.atan2(dx, dy); ball.speed[0] = Math.cos(angle); ball.speed[1] = Math.sin(angle); [/CODE] 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. 0 Share this post Link to post Share on other sites
tom_mai78101 693 Report post Posted July 31, 2012 Fixed. I have resolve this problem. [CODE] 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); } [/CODE] 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. 0 Share this post Link to post Share on other sites
DerekEhrman 127 Report post Posted July 31, 2012 I would change a few things about your collision detection routine. 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 * add to this value, the radius of the circle * if this value is greater than or equal to the radius of the hole, we have a collision 0 Share this post Link to post Share on other sites
tom_mai78101 693 Report post Posted August 1, 2012 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. 0 Share this post Link to post Share on other sites
tom_mai78101 693 Report post Posted August 21, 2012 Apparently, I probably didn't let anyone else know about this algorithm's [b]weakness[/b]. 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 "[b]how you execute your collision detection[/b]". If you detected an object A has collided with circle B, there will be three types of conditions you may or may not allow.[list] [*]Object A is inside of Circle B. [*]Object A is on Circle B. [*]Object A is outside of Circle B. [/list] With this algorithm,[list] [*]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. [/list] Just a heads up, and hoped you enjoyed it. 0 Share this post Link to post Share on other sites