%% E5 Lecture 7 % % To use without saving videos, comment out all calls to E5Image(). clear all clf clc %% Define a shape theta=0:10:360; %theta is spaced around a circle (0 to 360). r=1; %The radius of our circle. %Define a circular magenta patch. x=r*cosd(theta); y=r*sind(theta); myShape=patch(x,y,'m'); axis(20*[-1 1 -1 1],'square'); grid on; %% Move it across the screen T=0.25; %Delay between images for x0=-15:2.5:15, set(myShape,'Xdata',x0+x); %Translate by x0 pause(T); %Wait T seconds end %% Move it across the screen (different technique) T=0.25; %Delay between images x0=linspace(-15,15,20); for i=1:length(x0), set(myShape,'Xdata',x0(i)+x); %Translate by x0 pause(T); %Wait T seconds end %% Move it by giving it a velocity in x vx=5; %velocity in meters/sec. dt=0.25; %time step x0=-10; for t=0:dt:4, set(myShape,'Xdata',x+x0); %Translate by x0 pause(dt); %Wait dt seconds x0=x0+vx*dt; end %% Move it by giving it a velocity in x and y vx=5; %velocity in meters/sec. vy=10; dt=0.25; %time step x0=-10; y0=-10; for t=0:dt:3, set(myShape,'Xdata',x+x0,'Ydata',y+y0); %Translate by x0.y0 pause(dt); %Wait dt seconds x0=x0+vx*dt; y0=y0+vy*dt; end % %% Add gravity vx=5; %velocity in meters/sec. vy=15; dt=0.2; %time step x0=-15; y0=0; for t=0:dt:4, set(myShape,'Xdata',x+x0,'Ydata',y+y0); %Translate by x0, y0 pause(dt); %Wait dt seconds x0=x0+vx*dt; y0=y0+vy*dt; vy=vy-9.8*dt; end %% Add a border and collisions vx=5; %velocity in meters/sec. vy=20; dt=0.15; %time step x0=-14; y0=0; hold on; plot([-15 -15 15 15 -15],[15 -15 -15 15 15],'b','LineWidth',2); for t=0:dt:10, set(myShape,'Xdata',x+x0,'Ydata',y+y0); %Translate by x0, y0 pause(dt); %Wait dt seconds x0=x0+vx*dt; %update x postion y0=y0+vy*dt; %update y postion E5Image('add'); vy=vy-9.8*dt; %update y velocity if x0>=15-r, %check for collision with right side vx=-vx; %if so, reverse x velocity x0=15-r; %reset x position so it touches wall elseif x0<=-15+r,%check for collision with left side... vx=-vx; x0=-15+r; end if y0>=15-r, %check for collision with top vy=-vy; y0=15-r; elseif y0<=-15+r,%check for collsion with bottom vy=-vy; y0=-15+r; end end E5Image('save','E5Lect7_Ex6',dt); %% Add inelastic collisions vx=5; %velocity in meters/sec. vy=20; dt=0.1; %time step x0=-14; y0=0;% hold on; plot([-15 -15 15 15 -15],[15 -15 -15 15 15],'b','LineWidth',2); p=0.7; for t=0:dt:15, set(myShape,'Xdata',x+x0,'Ydata',y+y0); %Translate by x0, y0 pause(dt); %Wait dt seconds x0=x0+vx*dt; %update x position y0=y0+vy*dt; %update y position vy=vy-9.8*dt; %update y velocity if x0>=15-r, %check for right wall vx=-vx*p; %if hit, reverse (and decrease) velociy x0=15-r; elseif x0<=-15+r, %left wall vx=-vx*p; x0=-15+r; end if y0>=15-r, %top vy=-vy*p; y0=15-r; elseif y0<=-15+r, %bottom vy=-vy*p; y0=-15+r; end end %% Animate until done vx=5; %velocity in meters/sec. vy=20; dt=0.1; %time step x0=-14; y0=0;% hold on; plot([-15 -15 15 15 -15],[15 -15 -15 15 15],'b','LineWidth',2); p=0.7; while (abs(vx)>2) | (abs(vy)>2) | (y0<-15+2*r), set(myShape,'Xdata',x+x0,'Ydata',y+y0); %Translate by x0, y0 pause(dt); %Wait dt seconds x0=x0+vx*dt; %update x position y0=y0+vy*dt; %update y position vy=vy-9.8*dt; %update y velocity if x0>=15-r, %check for right wall vx=-vx*p; %if hit, reverse (and decrease) velociy x0=15-r; elseif x0<=-15+r, %left wall vx=-vx*p; x0=-15+r; end if y0>=15-r, %top vy=-vy*p; y0=15-r; elseif y0<=-15+r, %bottom vy=-vy*p; y0=-15+r; end end