Jump to content
  • Advertisement
Sign in to follow this  

Simple Ball-Wall Collision

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a simple question about collision with a wall by a round object. I have been looking for an answer on the internet for a few days now, but without finding any real results. Here's my problem: I have an object (spherical) in 2d or 3d space (don't really care at this point). I also have a plane (a wall) to detect collision with. I know how to detect a collision using the radius and the difference in virtual space. My ball has a velocity vector and I'm using vectors for it's position as well. I want the ball to intersect the plane, detect a collision, and bounce in a fairly realistic manner off the wall. When I searched for an answer, I mostly found examples on games like Pong where you check which wall was hit, and if it's the top or bottom walls, just flip the y component of the velocity. But since walls aren't always horizontal or vertical, I realised that this wouldn't work in most of my situations. I also found information on Dot and Cross product equations and after discussing it with my math teacher, I figured out how to use them to find angles between vectors and normals to planes. I think that maybe I will need to use one of those to get the normal to the wall and then flip my velocity over that normal or something. Let's assume I'm using a 3d system. I also want to know how to find the force the wall gives on the ball so I can get the impulse and maybe loss of velocity. Is there some math I'm missing? I'm just finishing Intro Physics 12 and Math Principles 12 this week, so I don't have much higher knowledge of these applications.

Share this post

Link to post
Share on other sites
OK so there are a few things that you need to start off with, I presume that you are using time based movement (i.e. calculating the new position of objects after a discrete time step?) The diagram below should illustrate the problem.

Before the time step (time t0) the position of the ball is p0, after the time step (t1) the ball will have moved to position p1. But this is not correct because at some other time (t) between t0 and t1, the ball intersects with the wall, so instead of calculating the position of the ball at time t1, the position of the ball needs to be calculated at time t, and the appropriate response taken from there (more on that in a minute) But for now here is what you need to calculate the time t to which you need to integrate to (Assuming that t0 is 0) :

r = [ b - p0 + (v0 * t) + (a * t^2) ] . N

r is the sphere radius
b is a point on the plane
p0 is the position at t0
v0 is the sphere velocity at time t0
a is the sphere acceleration
N is the plane normal
t is what you are trying to find

You can solve this quadratic equation, Taking the smallest positive real solution, if t < (t0 + t1) then that is the point that you should integrate to, otherwise there is no collision within the time step (Similarly if there are no positive or no real solutions)

OK so now you have the point to integrate forward to, you need to know how to respond appropriately. This is done by simply flipping the velocity vector about the plane normal. This is done by projecting the velocity vector onto the plane normal (with a dot product). You can also define a coefficient of restitution for approximating a non-elastic collision, this is denoted by e and is simply the ratio of velocity loss and should be a value between 0 and 1 to calculate the new velocity :

v1 = v0 - (1 + e)(v0 . N)N

v1 is the velocity after the impact
v0 is the velocity before the impact
e is the coefficient of restitution
N is the plane normal

You should have pretty much everything you need now. The only other thing to remember is that the simulation is running on time steps, and the is a little bit of time that has not been accounted for here, because you just integrated up to the point of collision. So the extra little bit needs to be integrated after the new velocity of the ball is found, i.e. after finding the new velocity integrate again by t1 - t. Hope this helps and you understand what I mean :D best of luck.

Share this post

Link to post
Share on other sites
Thanks for the solution, Moagstar. I think that will solve the problem of where the ball goes after the collision. My only problem is that I'm not quite sure what you mean by hte term "integrate" as I havn't taken any Calculus.

Though not as effective, my collision detection method is to check if the ball's position is within a radius of a wall using some sphere collision and then I can calculate the difference of overlap to move it back if I want (for camera collision). Is your method for detecting when the sphere passes the wall completely? I am trying to understand how you have done this.

I read it a few times and what I think it means is that when you know the sphere has passed the wall completely (too much speed), you use that formula to calculate the amount of time to move backwards to get to the actual point of collision. Is this right so far?

I would also like to know how the dot product works in the second equation. I know you can get the angle between two vectors with
theta = cos^-1(V1.V2)
if V1 and V2 are normalized. But what does V1.V2 mean exactly?

Sorry about my lack of comprehension, but I am very grateful for your response to my original problem.

Josh Rosario

Share this post

Link to post
Share on other sites
Thanks for the solution, Moagstar. I think that will solve the problem of where the ball goes after the collision. My only problem is that I'm not quite sure what you mean by hte term "integrate" as I haven't taken any Calculus.
Moagstar gave the 'exact' solution to the problem using calculus and integration, but a simplification that might be useful to you (and is commonly done in practice) is to assume that velocity is constant over the timestep and turn it into a linear equation. (Note that I'm not contradicting anything in moagstar's post, but just giving you some other things to think about). You basically then have a swept-sphere/plane or swept-circle/line test. This has been discussed recently, but I can't seem to find the threads atm.

Anyway, in brief, the signed distance from a point to a plane is (p.n)-d, where p is the point, n is the unit-length plane normal, and d is the perpendicular distance to the plane from the origin. (All this applies to circles and lines as well.) If you don't know d but have a point q known to be on the plane, you can find d as d = q.n.

The center of the sphere traces out the line C+tV, where C is the sphere center at time 0, and V is the velocity. Plug that into the plane distance equation and you get ((C+tV).n)-d. We are interested in the time (value of t) at which the sphere center is exactly a distance of r (the sphere radius) from the plane, so we want to solve for t as follows:

(C+tV).n)-d = r
(C.n)+(V.n)t-d = r
(V.n)t = r-(C.n)+d
t = (r-(C.n)+d)/(V.n)

Here I'm using '.' to indicate the dot product. If you're not sure how the dot product is defined, it is simply the sum of the products of the components, like this:

a.b = axbx+ayby+azbz

Back to the swept test, the point is to find not only if the objects collide (as with a static test), but when. This avoids cases like in moagstar's diagram where a static test would miss the intersection and the sphere would pass right through the wall. Once you've found the time of intersection, you can advance the sphere to that time (C += t*V), and reverse the velocity vector using the equation moagstar gave.

(Sorry moagstar if I've misinterpreted or misrepresented anything in your original post.)

Share this post

Link to post
Share on other sites
Original post by guitarguy310

My only problem is that I'm not quite sure what you mean by hte term "integrate" as I havn't taken any Calculus.

The problem is that in the real world time does not come in discrete steps, but when modelling physics on a computer you must calculate the position of an object, draw it, calculate the position, draw it, etc etc etc. So you are working in a number of discrete time steps. So you need to apply some discrete algorithm for calculating the new position and velocities after a certain amount of time has passed. One simple (and popular) solution is to use Euler integration, another example is Runge-Kutta, but for now it's probably best to stick with Euler because of the simplicity.

This article should give you a good introduction to euler integration.

So the basis of the simulation is to integrate your objects over a small time step for example 1/60 of a second* (see note) calculating the new position and velocity after that time. But during the time the objects may collide with other objects (either static-dynamic collisions or dynamic-dynamic collisions) so far we are only looking at static-dynamic collisions, once you have that solved you can look at dynamic-dynamic collisions. In each time step a collision may happen, so the way i work it is to calculate if a collision occurs first BEFORE integrating (so that you only perform the integration once) then integrate to that point, respond and integrate the remaining time step (again making sure that no collisions occur in the remaining time step)

*Note :(I hope this doesn't confuse you but it is probably best to run your simulation loop separate to your rendering loop, simulating as many times as is possible but only rendering e.g. 30, 40 60 or whatever times a second)

I hope that has answered this question (sorry the internet cafe I am in has disabled cut and paste arggh!) :


Is your method for detecting when the sphere passes the wall completely? I am trying to understand how you have done this.

I think jyk answered your question about the dot product :P,


Sorry moagstar if I've misinterprented

No problem, I think the only thing that you may have misinterpreted is that my solution is still based on euler integration and I am assuming that the velocity is constant over the time step for the purposes of calculating the new position.

[Edited by - moagstar on June 17, 2005 12:02:30 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!