How do I do collision detection here? ( moving sphere hitting a static sphere )

Started by
2 comments, last by Zakwayda 16 years, 3 months ago
Hi, I am very new at computer graphics. I have a practice, but I am really stuck, I read everywhere on the net, and in a book I have with me (real-time rendering by Moller) the past few days. But couldn't implement it properly : I have a moving sphere that I choose it's direction and speed. It bounces inside a box. And in this small application I can insert static balls ( balls that don't move when they get hit by that moving ball ). As I understand Collision detection is done in three steps : 1- Detecting whether or not a collision has happened ( will happen ). ( already been done ) 2- Finding the collision intersection between the colliding objects. ( here is where I am stuck now ) 3- Deciding the proper response to that collision. ( haven't reached this part yet ) In the application only one ball is moving, all the other ball(s) ( if any ) do not move. So, finding the intersection I think should be straight forward. Subtracting the center coordinates of the first ball from the second ball and dividing it by two, then adding the result onto the first ball. It doesn't matter which whether the moving ball is the first or the second, no? Doing that gives us the collision point. Then we must find the normal of that point, I know it is VERY simple, but I must admit that I can't figure it out... --- As for the third part, I will leave it later, when I understand the second, and implement it correctly. Your kind help is mostly appreciated! zgzg2020
Advertisement
It might help to reformulate the problem in a different way. If you think about it, colliding a moving sphere against a static sphere is equivalent to intersecting the line segment representing the motion with the static sphere "expanded" by the radius of the moving sphere.

So, if you have a sphere radius r0 moving from point p0 to point p1, and a static sphere of radius r1 at location c, then you can find the point of intersection (if any) by intersecting the line segment (p0, p1) with a static sphere of radius (r0 + r1) at c. This will give you a time and point of intersection, and the collision normal is simply the normalized vector from c to the point of intersection.

Hope that helps!
Thank you for your kind reply.

I could not benefit well from it, though... It is because of my rather primitive skill at both 3D graphics and vector mathematics, nor can I imagine what you say ( or others say ) well in the aspect of the program I am now working on. My 3D graphics teacher gave us an assignment. Like always, he wrote us a program and removed parts of the code, and asked us to write the missing parts so it will operate in the manner he wants. I personally dislike this method of teaching when the prerequisites of the course are very far from the course content. But here is not the place for such woes.
Quote:Original post by zgzg2020
Thank you for your kind reply.

I could not benefit well from it, though... It is because of my rather primitive skill at both 3D graphics and vector mathematics, nor can I imagine what you say ( or others say ) well in the aspect of the program I am now working on. My 3D graphics teacher gave us an assignment. Like always, he wrote us a program and removed parts of the code, and asked us to write the missing parts so it will operate in the manner he wants. I personally dislike this method of teaching when the prerequisites of the course are very far from the course content. But here is not the place for such woes.
d00fus's suggestion is really what you're after, but if the sphere isn't moving too fast, you can probably get away with a simpler method (which is in fact similar to what you described in your first post).

Since it's homework I'm not going to write any code here, but basically:

1. Find the distance between the two spheres
2. If it's less than the sum of their radii...
3. Normalize the vector between the two spheres - this is your collision normal
4. Move the second sphere away from the first along the collision normal by a distance equal to the difference between the sum of the radii and the distance between the sphere centers, plus a small epsilon to keep them from intersecting at the beginning of the next update (that sounds complicated, but draw it on paper and it should make sense)
5. Reflect the sphere's velocity vector around the collision normal

This topic is closed to new replies.

Advertisement