Roll and bounce

Started by
17 comments, last by Remover 22 years, 9 months ago
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
Advertisement
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)
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
\ | /
\ | /
___\|/___

Bret Yen Ting Lin
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
Bret Yen Ting Lin
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)
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~
Bret Yen Ting Lin
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.
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
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 ?
John BlackburneProgrammer, The Pitbull Syndicate

This topic is closed to new replies.

Advertisement