If condition to check for contact of two balls

1 view (last 30 days)
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 Comments
Muthu
Muthu on 21 Jan 2013
Edited: Muthu on 21 Jan 2013
I tried seeing the values and setting the if condition accordingly but still it doesnt execute the if loop.
Image Analyst
Image Analyst on 21 Jan 2013
Edited: Image Analyst on 21 Jan 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.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 21 Jan 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...

More Answers (1)

Thorsten
Thorsten on 21 Jan 2013
Edited: Thorsten on 21 Jan 2013
You forgot the "x = " in the third row. Should read
x = get(h2(2), 'xdata');
  4 Comments
Image Analyst
Image Analyst on 21 Jan 2013
Edited: Image Analyst on 21 Jan 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 on 21 Jan 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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!