Effectively emulating a laser pointer for my cat using Matlab

I am trying to write code using matlab that emulates a laser pointer so that my cat enjoys chasing it on the screen. This is what I have done so far:

figure('menubar','none','color','k') h = plot(0,'r.','MarkerSize',20); xlim([-1 1]); ylim([-1 1]) axis off phi1=(1+sqrt(5))/2; phi2=sqrt(3); step= 0.0001; % change according to machine speed for t=0:step:100 set(h,'xdata',sin(t+phi1*t),'ydata',cos(phi2*t)) drawnow end 

The "problems" with this code are as follows:

  • the pointer moves more or less at a constant speed and does not slow down to the nearest stop, and then continues unexpectedly.

  • The trajectory repeats somewhat, although I tried to do this using irrational numbers, the general movements are continuous from right to left. I think a sharper change in trajectory will help.

I know this is not a traditional programming issue, but still I want to solve the programming problem. I would appreciate your help and, of course, opened up to new ways to answer my question, which does not use the code that I added.

+7
matlab motion game-physics
source share
1 answer

A brilliant question, so good that I thought I would take 15 minutes of my life to go by myself. After YouTube’s extensive laser research, I thought using motion equations to move between random points would work well:

 n = 20; %number of steps pos = [0,0]; % initial position vel = 4; % laser velocity acc = 400; % laser acelertation dt = 0.01; % timestep interval figure set(gcf,'Position',get(0,'Screensize')); for i=1:n point = rand(1,2); dist = 1; while dist > 0.05 % loop until we reach the point plot(pos(1),pos(2),'o','color','r','MarkerFaceColor','r') axis equal xlim([0,1]) ylim([0,1]) drawnow % create random point to move towards dist = pdist([point;pos],'euclidean'); % calculate the direction & mag vector to the point dir = (point-pos)/norm((point-pos)); mag = norm(point-pos); % update position displ = vel*dt - 0.5*acc*mag*dt^2; pos = pos + dir*displ; end end 

Play with the parameters until you find what your cat likes: 0)

+3
source share

All Articles