Roll and bounce
#1 Members  Reputation: 122
Posted 06 July 2001  09:53 AM
#2 Members  Reputation: 122
Posted 07 July 2001  01:33 AM
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.
#3 Staff Emeritus  Reputation: 2076
Posted 07 July 2001  03:05 PM
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
F_{T} = F_{1} + F_{2} + ... + F_{n}
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.

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.
#4 Members  Reputation: 122
Posted 07 July 2001  08:50 PM
&(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
#5 Anonymous Poster_Anonymous Poster_* Guests  Reputation:
Posted 07 July 2001  10:03 PM
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?
#6 Staff Emeritus  Reputation: 2076
Posted 07 July 2001  10:10 PM
&(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 yaxis friction
...
ball_y += ball_vy;
Hope I''ve clarified things.
#7 Members  Reputation: 164
Posted 08 July 2001  02:14 PM
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.
#9 Members  Reputation: 122
Posted 09 July 2001  02:51 AM
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.
#11 Members  Reputation: 122
Posted 09 July 2001  07:36 AM
I already have my ball bouncing and sliding, the problem is to determine when the balkl should slide and when to bounce. I hope I can figure it out from your replies!
However... There is still the problem to get the ball to roll or rotate correctly. I thought I had solved it by always rotating about the vector perpendicular to the ball''s velocity (the cross product of the velocity vector and the ball''s upvector). This works fine  most of the time. But when the ball gets a sudden change in direction I sometimes get a strange result. The ball seems to rotate 180 deg about its local yaxis (upaxis) and then everything is fine until it suddenly happens again. And the strange thing is that it doensen''t happen every time the ball changes direction... Just sometimes.
Any ideas on how to solve this problem?
Thanks!
/K
#12 Members  Reputation: 164
Posted 09 July 2001  12:47 PM
As for the sudden changes in direction... not sure exactly. Can you figure out if there''s a threshold for the change? Does it only happen if the change in direction is greater than a certain amount? (ie a change in direction greater then 90 degrees)
#13 Members  Reputation: 122
Posted 14 July 2001  01:10 PM
sorri for this kind of stupid quesiton i m asking but.. i have learnt about dot and cross product before, but none of them used in real applications like game design, can u advise me a book that i can reference to or explain a bit abt how
1) for the collision detection (dot product)
how can i find the vector of the velocity and can the normal be just any normal on the plane? and why?
2) according to the description previously explained, it does not apply to 45 degree angle or does it? what i mean is it is obvious that if a ball bounces 45 degrees on to a plane it will definitely bounce 45 degrees the other way
o
\  /
\  /
___\/___
#15 Members  Reputation: 164
Posted 14 July 2001  03:19 PM
You probably have two components for the ball''s velocity right? Like one variable representing how fast the ball is moving left or right and one representing how fast it''s moving vertically. Those two components make up a 2D velocity vector. I''ll call the two components xv and yv for now.
For your surface''s normal vector you should pick a normal that points away from the surface, not into it. For example if the surface you''re representing were the surface of the earth, the normal vectors would point into the air, not towards the centre of the earth. I hope that''s clear. You should pick unit vectors to save calculations later (just divide each component by the vector''s magnitude).
To find the angle between the surface normal and the ball''s velocity, take the dot product of the two vectors (using component form*) and then divide by the magnitude of the velocity and the magnitude of the normal vector** (if you picked a unit vector for the surface normal then you just divide by the velocity''s magnitude, saving you a divide operation). The result will be the cosine of the angle between the two vectors. Then just use the acos function to find the angle (don''t forget it will return radians not degrees).
I might be misunderstanding your second point, but I think there''s some confusion over what I meant by bounce and roll. By bounce I mean that the ball is reflected off the surface at some angle, not necessarily straight up, and by roll I meant the ball basically sticks to the surface and slides across it.
Hope that helps.
*u dot v = u1*v1 + u2*v2
** because u dot v also equals u*v*cos(theta)
#16 Members  Reputation: 122
Posted 15 July 2001  02:24 AM
(XP) dot N < e
what does it really mean by (XP) why is it not the velocity V ? Sorry to say this...can u also give a little explaination like the dot product for the cross product too? I will really appreciate it... maybe i will find my prob after a few hints
^^ ta~
#17 Members  Reputation: 164
Posted 15 July 2001  10:47 AM
The formula (X  P) dot N is the shortest distance from the object X to the plane P (if XP and N are normalized). I won''t bother proving this, just trust me on it. In his article he''s simulating very tiny objects with a radius close to zero, so he''s checking if the distance from the plane to the object is close to zero. In your case you should compare the distance to the radius of the object.
distance = (X  P) dot N
if (ABS(distance  radius) < epsilon) then collision
where epsilon is some arbitrary small number. You use < eps instead of ==0 because with floating point numbers there''s always some innacuracy.
Don''t forget that at high speeds your objects might pass through surfaces because the distance test will fail  if an object of radius 1 is 2 units from a surface it won''t register a collision, but if it moves 4 units in the next frame it will be on the other side and still won''t register a collision. To get around this you calculate the distance from the object''s current position to the plane, then the distance from the plane to where the object _will_ be after updating. If one is negative and one positive, the object is going to pass through.
To answer your question, you don''t use velocity when calculating the distance because the velocity is irrelevant to distance. Velocity only matters when you''re trying to find the angle between the surface''s normal vector and the trajectory of the object, because that angle determines how the object bounces off  like you said before, if an object hits at 45 degrees then it bounces off at 45 degrees.
Search gamedev or google for the definition of dot and cross products. You should be able to find millions of explanations on the web.
#18 Members  Reputation: 122
Posted 15 July 2001  11:16 PM
The "flippnig" thing happens when there is a sudden change in the direction. For example it happenss if the ball is rolling up a slope and when it has lost all its speed it changes direction and start rolling down the slope instead. It also happens if the ball bounces off a wall...
I would appreciate any more help i could get on this!
(Is my way of thinking completely off track or is it just some small thing I am forgetting?)
Thanks!
/K
#19 Members  Reputation: 351
Posted 15 July 2001  11:19 PM
quote:
Original post by Bpanda
that pic was suppose to show that the ball is bouncing 45 degree in and out.. don''t know why it didn''t show that
Gamedev''s BBS seems to strip most spaces. So if you try to use spaces for formatting or indending it won''t work. This is a annoying here as spaces are useful for formatting many maths formulae, such as ones with subscripts and superscripts in. Does anyone know a workaround ? Or can this be fixed ?