If condition to check for contact of two balls

Hi,
I have a problem where I drop 2 balls and when they come in contact they should rebound. I have no problem in dropping or rebounding the balls. The main place where I have a problem is giving the condition of the contact. I am using an *if-else loop to check for contact * and I track the centre points of the balls at each time step to check for contact. In my code, the condition for contact never gets satisified even though I can see the balls merging. Any help would be appreciated. Here is the part of the code where I have stored the co-ordinates of the centers of the two balls in separate x and y variables and calculated the distance between the centers..
%x1,y1 is the co-ordinate of center of one fiber and x,y that of the other
x1=get(h2(1),'xdata');
y1=get(h2(1),'ydata');
x=get(h2(2),'xdata');
y=get(h2(2),'ydata');
d=((x1-x)^2 + (y1-y)^2)^0.5;
if d<=(2*r)
-------------
-------------
-------------

6 comentarios

Image Analyst
Image Analyst el 21 de En. de 2013
Are you using the debugger to step through your code? If you did, this kind of thing would be caught quickly. Also, please see the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F. And how are you incrementing x1,y1 and x and y? Are you sure they ever get close enough for an interaction (bounce or intersection)?
Muthu
Muthu el 21 de En. de 2013
Yes, I used the debugger and I found that it never goes inside the if-loop which means the condition never gets satisfied. As for x1,y1,x and y- the first ball will be dropped into a box and it settles after a point. Then I drop the 2nd ball and the co-ordinates of its centre point(which is saved in a plot) will be tracked at each time step for contact using velocities and displacements.
Image Analyst
Image Analyst el 21 de En. de 2013
If you're using the debugger then you will know all the coordinates and you can figure out why it's never satisfied. Take off the semicolons to make it easier to see the values.
Jan
Jan el 21 de En. de 2013
Btw.: SQRT is faster than ^0.5, but for comparing the distance, it is much cheaper to omit the square root, but compare with the squared (double) radius.
Muthu
Muthu el 21 de En. de 2013
Editada: Muthu el 21 de En. de 2013
I tried seeing the values and setting the if condition accordingly but still it doesnt execute the if loop.
Image Analyst
Image Analyst el 21 de En. de 2013
Editada: Image Analyst el 21 de En. de 2013
Tell us the values of x1, y1, x, and y for which you think it should enter the "if" block but it doesn't. And tell us d and r in that case.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 21 de En. de 2013
I think your tolerances are too small, basically they're zero. Maybe widen it:
if d<=(2*r) * 1.05 % If it's within 5%, or whatever...

1 comentario

Muthu
Muthu el 31 de En. de 2013
Thanks, this was the problem. I totally overlooked it.

Iniciar sesión para comentar.

Más respuestas (1)

Thorsten
Thorsten el 21 de En. de 2013
Editada: Thorsten el 21 de En. de 2013
You forgot the "x = " in the third row. Should read
x = get(h2(2), 'xdata');

4 comentarios

Muthu
Muthu el 21 de En. de 2013
Sorry my bad. It was in the code. I made a mistake while copying.
Thorsten
Thorsten el 21 de En. de 2013
Ok. Then maybe r is changing inside your loop?
Image Analyst
Image Analyst el 21 de En. de 2013
Editada: Image Analyst el 21 de En. de 2013
So go up and correct it. Next time you can use control-c and control-v to avoid such mistakes. So your problem remains then.
Muthu
Muthu el 21 de En. de 2013
My r is not changing as it is the radius of the ball and its a constant. Both balls are of the same radius.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 21 de En. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by