% Movie of cartoon of forces acting on electron from EM wave

clear; close all;
vid = VideoWriter('EMforces.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 30;
open(vid);

myBlue = [0.4 0.44 0.73];

figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7);
set(gca, 'Projection','perspective');
hold off;
lp1 = [0.4 -0.4 1.7];
lp2 = [0.4 -0.4 -0.7];
LL = 4*pi;
wavex = 0:pi/90:LL;
wavey = 0.0*wavex;

for i = 0:pi/90:LL
    newplot;
    r = 0.5;
    hold on;
    mArrow3([0 0 0],[LL 0 0],'color','black','stemWidth',0.04,'tipWidth',0.2,'facealpha',1);
    [x,y,z] = sphere(70); surf(x*r + pi,y*r,z*r,'FaceColor',[0.28 0.32 0.64],'EdgeColor','none');
    [x,y,z] = sphere(70); surf(x*r + 2.5*pi,y*r,z*r,'FaceColor',[0.28 0.32 0.64],'EdgeColor','none');
    % incoming EM-field
    wavez = 4*sin((wavex) + (-i));
    plot3(wavex,wavey,wavez,'color',myBlue, 'LineWidth', 2.5); % B-field
    plot3(wavex,wavez,wavey,'color','red', 'LineWidth', 2.5); % E-field
    mArrow3([pi 0 0],[pi -4*sin(i) 0],'color','red','stemWidth',0.1,'tipWidth',0.2,'facealpha',0.5); %
    mArrow3([2.5*pi 0 0],[2.5*pi -4*cos(i) 0],'color','red','stemWidth',0.1,'tipWidth',0.2,'facealpha',0.5); %
    mArrow3([pi 0 0],[pi 4*sin(i) 0],'color',myBlue,'stemWidth',0.1,'tipWidth',0.2,'facealpha',0.7); %
    mArrow3([2.5*pi 0 0],[2.5*pi 4*cos(i) 0],'color',myBlue,'stemWidth',0.1,'tipWidth',0.2,'facealpha',0.7); %
    
    axis square;
    axis off;
    light('Position',lp1,'Style','infinite');
    light('Position',lp2,'Style','infinite');
    xlim([0 LL]);
    ylim([-LL/2 LL/2]);
    zlim([-LL/2 LL/2]);
    view(50,25);
    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off;
end
hold off;
% Output the movie as an mpg file
close(vid);