Jump to content

  • Log In with Google      Sign In   
  • Create Account


Roll and bounce


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
18 replies to this topic

#1 Remover   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 July 2001 - 09:53 AM

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

Sponsor:

#2 Grudzio   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 July 2001 - 01:33 AM

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.

#3 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 07 July 2001 - 03:05 PM

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 implemented


int ball_x, ball_y; // ball 2D position vector

int ball_vx, ball_vy; // ball 2D velocity vector

int ball_ax, ball_ay; // ball 2D instantaneous acceleration vector


// Initialization here. Set initial ball position, velocity and accel


while( !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.

#4 Bpanda   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 July 2001 - 08:50 PM

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

#5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 07 July 2001 - 10:03 PM

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?



#6 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 07 July 2001 - 10:10 PM

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.

#7 Dobbs   Members   -  Reputation: 164

Like
Likes
Like

Posted 08 July 2001 - 02:14 PM

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.

#8 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 08 July 2001 - 04:33 PM

Dobbs is perfectly right in all his assertions - some glaring oversights of mine. Thanks!

#9 Grudzio   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 July 2001 - 02:51 AM

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.

#10 Grudzio   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 July 2001 - 03:15 AM

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.

#11 Remover   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 July 2001 - 07:36 AM

Thanks for your help!
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 up-vector). 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 y-axis (up-axis) 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 Dobbs   Members   -  Reputation: 164

Like
Likes
Like

Posted 09 July 2001 - 12:47 PM

If the ball is close to a surface, check the angle between the ball''s direction vector and the surface''s normal vector. If it''s close to 90 degrees then the ball should roll, otherwise it should bounce.

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 Bpanda   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 July 2001 - 01:10 PM

hi~
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
\ | /
\ | /
___\|/___



#14 Bpanda   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 July 2001 - 01:11 PM

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

#15 Dobbs   Members   -  Reputation: 164

Like
Likes
Like

Posted 14 July 2001 - 03:19 PM

I don''t know of any specific books, sorry. Any decent introduction to game programming book should have a section on vectors, dot products, etc. and their uses. Search gamedev and other game programming sites like it for articles on vectors. I''ll give you a basic explanation here.

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 Bpanda   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 July 2001 - 02:24 AM

Thank you for the explaination... i think i have found where my problem is for the dot product.. but one more thing... in the collision response:Bouncy, Trouncy fun tutorial (www.gamasutra.com/features/20000208/lander_01.html) it mentioned that the collision occurs if
(X-P) dot N < e
what does it really mean by (X-P) 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 Dobbs   Members   -  Reputation: 164

Like
Likes
Like

Posted 15 July 2001 - 10:47 AM

X-P is a vector from the object X to any point on the plane P. Just find the vector from X to any of the vertices of your surface, then normalize.

The formula (X - P) dot N is the shortest distance from the object X to the plane P (if X-P 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 Remover   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 July 2001 - 11:16 PM

About rolling thing again...
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 johnb   Members   -  Reputation: 347

Like
Likes
Like

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 ?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS