Collision detection and solving of a spherical ball in a cylindrical boundary
6 views (last 30 days)
Show older comments
I'm trying to write a code in which a spherical ball moving at a given velocity in a solid cylindrical boundary in which when it makes contat with the boundary there should be an elastic collision that reflects the ball. However im unsure on how to detect the collision and solve it for the new reflected velocity.
This is my code so far:
Obj = [0 0 0];
velocity = [2e-8 2e-8 2e-8];
for i = 1 : 20
clf
r = 5e-7;
[X,Y,Z] = cylinder(r);
h = 20e-7;
Z = Z*h-2*r;
cyl = surf(X+0.5e-6,Y,Z,'FaceColor','none');
rotate(cyl, [0 1 0], 90)
hold on
[x,y,z] = sphere;
radius = 5e-8;
plot3(radius*x+Obj(1,1),radius*y+Obj(1,2),radius*z+Obj(1,3),'k');
xlabel('x')
ylabel('y')
zlabel('z')
Obj = Obj + velocity;
view([-30,6])
set(gca,'xlim',[-5e-7 15e-7])
set(gca,'ylim',[-10e-7 5e-7])
set(gca,'zlim',[-1e-6 1e-6])
drawnow
end
any help would be appreciated thanks.
0 Comments
Answers (1)
Andrew Ouellette
on 11 Nov 2022
Hi Victor,
You can detect a collision between the ball and the cylinder if the distance between the center of the ball and the outside of the cylinder is less than or equal to the radius of the ball.
To do that, you will need to increase the number of points along the circumference of the cylinder such that the gap between the points is less than the radius of the ball.
I have modified your code below to show an example of how to detect collisions:
Obj = [0 0 0];
velocity = [2e-8 2e-8 2e-8];
r = 5e-7;
radius = 5e-8;
for i = 1:20
clf
N = ceil(2*pi*r/radius);
[X,Y,Z] = cylinder(r,N);
h = 20e-7;
Z = Z*h-2*r;
cyl = surf(X+0.5e-6,Y,Z,'FaceColor','none');
rotate(cyl, [0 1 0], 90)
hold on
[x,y,z] = sphere;
plot3(radius*x+Obj(1,1),radius*y+Obj(1,2),radius*z+Obj(1,3),'k');
xlabel('x')
ylabel('y')
zlabel('z')
Obj = Obj + velocity;
view([-30,6])
set(gca,'xlim',[-5e-7 15e-7])
set(gca,'ylim',[-10e-7 5e-7])
set(gca,'zlim',[-1e-6 1e-6])
drawnow
displacements = Obj(2:3)-[cyl.YData(1,:) cyl.YData(2,:);cyl.ZData(1,:) cyl.ZData(2,:)]';
distances = vecnorm(displacements,2,2);
collision = any(distances <= radius);
if collision
fprintf('Collision occurred on iteration %g',i);
break;
end
end
As far as updating the velocity after that point, it is up to you which method you wish to use for the update.
0 Comments
See Also
Categories
Find more on Graphics Performance 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!