# Ball in a complex 2D environment

1. Nov 20, 2009

### StefenRoebke

I am working on a simulation of a ball moving in a complex 2d environment. I am looking for a single physics model that will handle the movement of the ball properly. Here is exactly what I have:

Gravity applied in the -y direction
Friction and wind resistance
collision detection with the environment.

The environment consists of many walls, as well as ramps of varying angles, and eventuually some more complex structures.

The issue I am running in to is mainly as follows:

When should the ball bounce of a surface it collides with, and when should it roll along it?

Say the ball is moving along a floor parallel to the x axis. It comes across a ramp of 45 degrees. Does it roll up it? Does it bounce off? What about 20 degrees? What about 80 degrees?

What if the ball is in free fall and it hits a ramp. Does it bounce off the ramp? Does it roll down the ramp?

Everything I know about physics tells me there should be a single mathematical model that handles this, and I don't need a lot of checks in the simulation code for the state of the ball, but I am at a loss. Any advice, help, resources, or just a push in the right direction would be much appreciated!

2. Nov 20, 2009

### belliott4488

What you're describing is actually a fairly complex simulation, if you really want to make it physically realistic. The most correct answer to your questions would require you to decide things like: How elastic is the ball, i.e. how much energy is lost on each collision? Do you incorporate rotational effects, i.e. if the ball hits a wall at an angle other than 90 degrees, does some of its energy go into rotational motion? You mention friction - that's important, but it also brings in some arbitrary values, i.e. what coefficients of friction apply here? Same with wind (actually a subset of the friction issue) - what is the coefficent of drag for the ball?

You'll probably have to simplify the problem by making certain simplifying assumptions to make it tractable; people here might be able to help with that. Before that, however, I have some basic questions about your simulation.

1. You say it's 2-D, and you mention a presumably vertical direction, y, in which gravity acts, and then a horizontal direction x. Are those the only directions, i.e. the ball is not rolling on a table top? If it is on a table, and you have ramps, that's 3-D.

2. Can you tell us what kind of behavior you envision for the ball? E.g. do you want the ball to come to rest after it's rolled around if no one is imparting additional energy to it, or does it just keep bouncing and rolling around as long as the simulation is active? How important is that it be physically realistic (this gets to the point above)? Is this meant to predict real-world behavior, hence a high requirement of realism, or is it okay for it to look "believable", as for a video game?

Sounds like a fun project; let us know more, and we'll see if we can help!

3. Nov 20, 2009

### StefenRoebke

Yeah, I just experimentally found decent CoF and drag for the ball, as well as a coefficient of restitution.

I want it to be as physically accurate as possible, so I do want some energy lost to adding rotation to the ball (its actually a giant tire that won't fall over so rotation is important, but for the simulation, treating it as a ball works just fine)

1.) It is only 2D with the Wheel moving in both the X and Y directions. The user can apply a variable force to the ball at any time in any 2D direction. All of this motion is already coded and working. Here is an ASCII view of how it might look:

|
|...O ->ball's motion........___
|................____........../.....\
|.............../......\ ____/........\
|_________/..........................\__________

2.) The ball would eventually would come to rest (and does when rolling on a flat surface) It is for a video game project so "Close enough to look real" is what I need, but I do want it to be as real as possible, though I don't need angle-acurate rotation, or perfect motion.

4. Nov 20, 2009

### Gerenuk

Once you find that one equation for collision it's probably easy to do.

In a time interval dt the ball moves on with its current velocity, then gravity acts, simplified wind resistance acts (disregard Magnus force?), and if the ball crosses a line (ramp or floor or anything) then the ball velocity and ball rotation changes (as to bring the ball above the ramp again).

Implementing this with the one correct mathematical equation should deal with all issues. Basically instead of perfect rolling the ball would make infinite bounces of a ramp.

Oh, I have a better idea. The collisions should be continuous force that depends on the distance to the ramp. This way rolling friction and laws of physics are easier to handle with.

So apart from rolling you can starting with the following:
$$\vec{x}'=\vec{x}+\vec{v}\mathrm{d}t$$
$$\vec{v}'=\vec{v}+(-\hat{j}g-\hat{v}c_w\vec{v}^2+F_r)\mathrm{d}t$$
where $F_r$ is the force due to ramps. This force is something like 1/((distance ball center to ramp surface)-(radius ball)*0.95)^2 in the direction pointing away from the ramp (and only taken for closeby ramps). That's probably all you need for a rotationless simulation. I'll think about how to include rotations...

5. Nov 20, 2009

### StefenRoebke

rotation is pretty simple to approximate. If the ball is colliding with a surface for a frame's time interval, rotate the ball by (distance traveled*Pi)/(2*radius) radians, then set the rotational velocity to the rotation/frame's time incase the ball leaves the surface

The infinite bounces solution is a problem because the ball will lose energy with each bounce.

6. Nov 20, 2009

### belliott4488

Okay, I think that defines the problem well enough to get started - missing details will reveal themselves in due course.

I would attack it this way, off the top of my head:

1. Get the center of mass (COM) motion right first. Use the rule that the angle of incidence = the angle of reflection for collisions with ramps (just reflect the velocity vector component perpendicular to the wall/ramp surface). Just doing that with no energy losses or rotational motion should be a good start to see if things look right. Basically, you should have a ball bouncing around all over the place and should be able to see if it looks realistic.

2. Add in an energy loss due to inelasticity of the ball at every collision. I'm not sure how to model this accurately, but I'd try a loss factor that is proportional to the normal velocity first, just thinking that the faster the ball is moving (in the direction perpendicular to the surface), the more it's compressed and the more energy that gets lost. Maybe it should be a higher-order function of the velocity, but I would start with that and see how it looks - let the ball bounce up and down vertically against a horizontal surface and watch how its maximum height on each bounce decreases, and see if it looks right. (You might want to code a lower velocity threshold below which you force it to go to zero, so that you don't continue to compute motion past the point where it's visible - just reducing the energy by some factor less than 1 will never cause it to come to complete rest.)

3. Add air resistance for static wind, i.e. no wind motion for now (it will be easy enough to add that, if you need it, once you get the basic effects down). That would probably be best as an acceleration in the negative velocity direction, proportional to the square of the velocity magnitude. You'll have to experiment to get the right value of the ballistic coefficient.

5. Add rotational effects. This seems very tricky. The component of velocity parallel to the surface of collision will be the key, so you might be able to get away with simply doing an energy transfer between linear kinetic energy and rotational energy, e.g. if there is no initial rotational motion (omega = 0), decrease 1/2 mv^2 by an amount proportional to v_parallel and add that amount to 1/2 I omega^2 (which you accomplish by decreasing v and increasing omega). Then you have to think about how to modify the angle of reflection .... maybe it's enough that v_normal changes direction and decreases according to the inelastic losses (above), and v_perpendicular stays the same direction, but changes magnitude according the rotational contribution. You'll have to figure out how to incorporate the rotation prior to the collision, however - if it's fast enough, it will actually increase the speed in the direction parallel to the surface.

Let's deal with 5. more carefully once you get there -- 1-4 should keep you busy for a little while.

- bruce

7. Nov 20, 2009

### Gerenuk

Rotation is more tricky I think. There are friction issues and other contact issues for rotating balls. I think your model is the one with a very sticky ball.

Bounces are not a problem. They are meant to dissipate energy in the direction perpendicular to the ramp, so that the ball can roll.

8. Nov 20, 2009

### Gerenuk

After second thought, this way isn't good for a ball that should eventually being able to roll. It is important to have a finite time during which the ramp can act. So the ramp is rather like a hard trampoline.

9. Nov 20, 2009

### StefenRoebke

well I had bouncing working properly, but the ball was bouncing all the time, which is the crux of my problem. I need it to roll up the ramps and down the ramps, instead of bounce off of them. If the ball is traveling on a flat surface in the direction of the +x axis, then hits a 45 degree ramp, I want the normal of the velocity to be (sqrt(0.5), sqrt(0.5)) but the collision code would make it (0,1)

but sometimes the ball SHOULD bounce off ramps, depending on the velocity vector, and angle of the ramp.

10. Nov 20, 2009

### Gerenuk

You should not have instant bounces, but rather one with an equation similar to my F_R. With the proper equation a very flat bounce will eventually become an almost straight rolling.

11. Nov 20, 2009

### Gerenuk

The latter is the case for real physics of a perfectly bouncy ball. A ball with some coefficient of restitution would probably roll up the ramp.

12. Nov 20, 2009

### StefenRoebke

So maybe calculate the bounce in an arbitraty space (aspace) where the line of the ramp lies on the x-axis, then only negate and apply a coefficient of restitution to the aspace y component of the velocity of the ball?

13. Nov 20, 2009

### Gerenuk

Yes, I think so. Apply this coefficient only to the direction perpendicular to the flat surface.

The ball might bounce up the ramp. To damp this bouncy behaviour I would suggest to use my continuous model.

14. Nov 20, 2009

### StefenRoebke

k, thanks for the insight, I am going to mess around with it a little.

15. Nov 20, 2009

### Gerenuk

Hmm, actually maybe the continuous model isn't that good. It captures the physics better and easier, but it requires too many frames to be simulated.

Let me know if you find a good equation, that yields some rolling.

Also it would be nice if a rotating ball that hits a surface would go off to one side and at the same time lose the equivalent energy from its rotation to the kinetic energy.

16. Nov 20, 2009

### StefenRoebke

Got some decent results:

Took the surface normal at the point of collision.
Protected the Velocity vector on the normal.
Multiplied that by the CoR and negated

Took the tangent vector at the point of collision
Projected Velocity vector on the tangent

Added the 2 together to get the new velocity vector

Here is a kinda jumpy video of the results (could take a few to download):
http://www.mediafire.com/file/nwgoinljcym/temp.avi [Broken]

you can see it bounces off the ramp first but that can be avoided by making a more gradual ramp first. Also, I haven't implemented in the air rotation yet, so it just stops rotating in the air.

Last edited by a moderator: May 4, 2017