Roll and bounce

Started by
17 comments, last by Remover 22 years, 9 months ago
Hi! I want to be able to roll and (when neccecary) bounce a sphere in an arbitrary shaped landscape... I''m looking for ideas for when to roll and when to bounce the ball and how to implement those ideas. Thanks in advance! (Sorry for my bad english) /K
Advertisement
I would say that ball rolls when it has contact with the ground.
In this situation you can model ball''s movement using Newtons laws F=ma. Assuming that the only force acting is gravity, we have F=mg=ma. So on flat surfaces ball moves with constant speed and on slopes it (de)accelerates with a=g*sin(alpha) where alpha is slope''s angle. Now bouncing. Ball bounces when it rolls and then hits something, or falls (from a cliff) and then hits the ground. In the first case just change the directon of speed.
The second case: falling ball. You have to apply rule that angle of refraction equals angle of falling. Angle of falling is equal to angle between normal of the surface and ball''s speed.
But there is one problem when the ball starts bouncing it will never stop. (like in flipper game). You have to assume that after each bounce ball loses part of it''s energy. It can be implemented by stating that after n - bounces ball rolls again, where n is some integer.
The last thing is to model ball''s fall. Just use equations for free fall. So, in x - direction ball moves with constant speed, and in y - direction ball accelerates with a = g.

I hope it will help you.

K.
Grudzio gives excellent explanations and implementations of pretty much everything; I just wished to refine the bouncing ball aspect.

Rather than arbitrarily limiting the ball to n bounces, use the natural force of gravity (already included in your simulation) and a summation of forces action upon a body, ie

FT = F1 + F2 + ... + Fn

In this case, we wish to aggregate the current force of the ball and the effects of gravity (otherwise the ball will alter direction and accelerate/decelerate too rapidly). In the specific case of the bouncing ball, when the ball hits the surface, set the magnitude of its force vector to be the signed opposite of its value prior to surface contact and the direction of the force vector to be the "mirror image" of its prior dirction (with the surface normal translated to point of contact and a vector perpendicular to the ball direction defining the plane of the "mirror").

When the ball starts to rise, the height it attains is damped by gravity (which acts in the opposite direction) resulting in a natural parabolic path. The force of the ball the next time it hits the ground is less than before, resulting in a smaller bounce... and so on until the ball resumes simply rolling.

The above explanation may be confusing, so I include a pseudocode implementation for a 2D bouncing/rolling ball demo.

  // This pseudocode demo models the motion of the ball in the XY plane. Certain functions are// assumed to be defined and implementedint ball_x,  ball_y;   // ball 2D position vectorint ball_vx, ball_vy;  // ball 2D velocity vectorint ball_ax, ball_ay;  // ball 2D instantaneous acceleration vector// Initialization here. Set initial ball position, velocity and accelwhile( !done ){  // check for termination  // do collision detection and physics  if( collision() )  {    // vector between ball position and collision plane is ball velocity    // set new ball velocity direction based on above    &(ball_x, 0, ball_y) = cross_product((ball_x, ball_y, 0),                                         (pane_x, plane_y, 0));  }  // do acceleration modelling  ax += gx;   // gx can be small negative value to simulate atmospheric friction  ay += gy;   // gy = acceleration due to gravity  // move ball  ball_vx += ax;   // physically valid for instantaneous values, assuming t = 1  ball_vy += ay;  ball_x += vx;  ball_y += vy;  // draw ball}  


The above pseudocode handles regular motion as well as bouncing, collisions with inclined surfaces (eliminating sines and cosines through the use of the cross product - I used a strange notation to mean the vector (ball_x, 0, ball_y) is assigned the resultant of the cross product) and acceleration due to slopes (I hope!), provided a collision is flagged every time the ball touches a surface.

---
Those who can do nothing criticize; those who can, critique.
Sorry~ but i don't quite get this function

&(ball_x, 0, ball_y) = cross_product((ball_x, ball_y, 0),plane_x, plane_y, 0))

what does the 2 zero mean? and how is cross product related in calculating the vector? also .. how do i know how much damping i should have?

ta~ ^^



Edited by - bpanda on July 8, 2001 4:39:57 AM
Bret Yen Ting Lin
Oluseyi Dude,

Maybe I do not get what you mean, but gravity cannot damp the momentum of the ball because it is a conservational force. Maybe you mean air resistance/friction?

I''m sorry, I used totally incomprehensible syntax there. By writing

&(ball_x, 0, ball_y)

I was trying to create a shorthand way of saying the vector resultant (x, y, z) of the cross product is assigned to the corresponding variables ball_x, 0 (meaning not assigned), ball_y. ie:

ball_x = x;
ball_y = z;

I use the 3D cross_product, setting the z values of both vectors being crossed to 0 (so we ignore the YZ and XZ planes). The reason I did it that way is that it eliminates me having to calculate the resultant angle manually. Cross product of two vectors yields a normal vector to the plane of the original vectors. Rotating that product onto the plane of the original vectors should yield a vector bang in the middle of the original two. So if one vector is the direction of the ball and the other is the plane collided with, the resultant is the direction of the ball after collision. There may be a few exceptions.

As for how much damping you should have, I did omit that. Sorry. You''ll need a vertical friction (friction of air, or space depending on the atmosphere you decide to simulate). Then the acceleration equation changes to

ball_vy += ball_ay - ball_fy; // fy is y-axis friction
...
ball_y += ball_vy;

Hope I''ve clarified things.
When you say rolling, do you actually want the spheres to rotate realistically or do you just mean you want them to slide down slopes? If you just want them to slide down slopes most of the code and ideas in this thread are fine, but if you want real rotation it''s a much different matter.

I do see some problems with Oluseyi''s code though:

ax += gx;
ay += gy;

ball_vx += ax;
ball_vy += ay;
ball_x += vx;
ball_y += vy;

That''s sort of right. Mostly the acceleration part is wrong. It''s not going to work properly for gravity, and for air friction it''s not great. Air friction depends on the object''s velocity, it''s not just some constant amount. You should use something like:

ball_vx *= air_friction;
ball_vy += gy * dt; //for Earth gy == -9.8
ball_x += vx * dt;
ball_y += vy * dt;

where dt is the change in time since the last update and air_friction is a constant between 0 and 1. The lower the value the stronger the air friction (probably best to use something close to 1 for realism). Using Oluseyi''s method ball_vx might decrease initially but it won''t approach zero, which is what air friction should do, and the object will accelerate downwards way too fast.

Also in the cross product line, I''m pretty sure ball_x and ball_y should be ball_vx and ball_vy.

The rest looks good though.
Dobbs is perfectly right in all his assertions - some glaring oversights of mine. Thanks!
I agree with Oluseyi that my previous idea of limiting ball bounces to arbtrary number is not good. So here is improved version. On impact ball loses some part of its energy whitch is absorbed by the ground.
So it results in smaller velocity. In pseudocode:

if collision_detected = true then begin
Ball_vy = k*Ball_vy
the rest of collison code
end else begin
rolling or free falling code
end

k describes how much energy was absorbed by the ground.(0
This effect can be used with or without air friction.

K.
In my last post, in last but one line, istead of mysteurious (0
should be written that K is greater than 0 and smaller than 1.
I dont know why my Netscape didnt want show those greater and smaller signs.

K.

This topic is closed to new replies.

Advertisement