Bouncing balls flashing animation
Afficher commentaires plus anciens
I modeling balls bouncing physics behavior with matlab. Below is my balls animation code, if i using one ball then bouncing animation is fine, but if i insert more than one ball then balls animation is flashing.
ball_rad = [1,1,1]; % ball radius in metres
ball_pos = [0 10;0 5;0 6]; % ball (x,y) position in metres
ball_vel = [1 1;1 1;1 1]; % ball (x,y) velocity in metres per second
ball_spin = [0,0,0]; % ball spin in radians per second
ball_angle = [0,0,0]; % ball angle in radians
tic
while ~stop
amount = size(ball_rad);
for i=1:amount(1,2)
% draw ball
ball_h = rectangle('Position', [ball_pos(i,:)-[ball_rad(i) ball_rad(i)] ball_rad(i)*2 ball_rad(i)*2], 'Curvature', [1 1]);
offset = ball_rad(i) * [cos(ball_angle(i)) sin(ball_angle(i))];
p1 = ball_pos(i,:) + offset;
p2 = ball_pos(i,:) - offset;
line_h = line([p1(1) p2(1)], [p1(2) p2(2)], 'Color', 'k');
% draw frame and pause to achieve required fps
drawnow
tic
% erase ball
delete(ball_h);
delete(line_h);
if exist('plot_h','var'), delete(plot_h); clear plot_h, end
% Update vertical ball velocity from gravitational acceleration
ball_vel(i,:) = ball_vel(i,:) + [0 g/fps];
ball_pos_old = ball_pos(i,:);
% Update ball position from ball velocity
ball_pos(i,:) = ball_pos(i,:) + ball_vel(i,:)/fps;
% Update ball angle from ball spin (angular velocity)
ball_angle(i) = ball_angle(i) + ball_spin(i)/fps;
% loop through surfaces
for s = 1 : num_surfaces
surface = surfaces{s};
% compute surface normal such that it's on the ball's side
normal = [surface(2,2)-surface(1,2) surface(1,1)-surface(2,1)];
normal = normal / norm(normal);
vel_dir = ball_vel(i,:) / norm(ball_vel(i,:));
if normal*vel_dir' < 0
normal = -normal;
end
% compute the position of the edge of the ball nearest the surface
edge_offset = normal * ball_rad(i);
edge_pos_old = ball_pos_old + edge_offset;
edge_pos = ball_pos(i,:) + edge_offset;
% ball collides with surface if the line segment between its
% current and old position intersects the surface
if lines_cross(edge_pos_old, edge_pos, surface(1,:), surface(2,:))
ball_vel_old = ball_vel(i,:);
% compute the collision point from the point of intersection
intersect_pt = lines_intersection(edge_pos_old, edge_pos, surface(1,:), surface(2,:));
% rotate velocity vector onto horizontal surface
co = normal(2);
si = normal(1);
rot = [-co -si; si -co];
ball_vel_rot = ball_vel(i,:) * rot;
% update velocity parallel to surface and spin
ball_vel_rot_tmp = (1/7)*(5 - 2*h_restitution)*ball_vel_rot(1) - (2/7)*(1 + h_restitution)*ball_rad(i)*ball_spin(i);
ball_spin(i) = -(5/(7*ball_rad(i)))*(1 + h_restitution)*ball_vel_rot(1) + (1/7)*(2 - 5*h_restitution)*ball_spin(i);
ball_vel_rot(1) = ball_vel_rot_tmp;
% update velocity perpendicular to surface
ball_vel_rot(2) = ball_vel_rot(2) * -restitution;
% rotate velocity vector back to original surface
ball_vel(i,:) = ball_vel_rot * rot';
% update position relative to intersection to correct overshoot
overshoot = edge_pos - intersect_pt;
overshoot = ball_vel(i,:) * norm(overshoot) / norm(ball_vel_old);
ball_pos(i,:) = intersect_pt + overshoot - edge_offset;
end
end
end
end
Réponses (0)
Catégories
En savoir plus sur Stability Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!