• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
tom_mai78101

Ball to Circular Wall Interior Collision Response

4 posts in this topic

[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
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
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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0