3

enter image description hereI would like to implement the fly-through effect as shown in this nice example. First I created a figure from matrix hm, a 300X300 matrix by using the surf function:

surf(hm); 

Then, I defined an animated line from variables x, y, and z and displayed on the figure as follows:

curve = animatedline;
curve.LineWidth = 6;
curve.Color = [ 1 0 1];
for i = 1:length(x)
  addpoints(curve, x(i), y(i), z(i));
  drawnow;
end

Then, I wanted to implement the fly-through effect so that the camera will move along the line. I tried this code piece I took from the example above and I slightly modified it:

for i = 1:length(x)
  campos([(x(i) -5), (y(i)-5), 0]);
  camtarget([x(i), y(i), z(i)]);
  drawnow;
end

But the camera doesn't move as I intended. What am I doing wrong?

1 Answers1

0

If you want to mimic the behavior of the linked example, you need to have both the camera target and camera position moving along your curve defined by (x, y, z). The way you've written it above, the camera target moves along the curve, but the camera position is always offset from the target by (-5, -5) in the xy plane and sitting at z = 0. If you want the camera to follow the curve behind the target, you should try something like this:

for iPoint = 6:numel(x)
  campos([x(iPoint-5) y(iPoint-5) z(iPoint-5)]);  % Note the index is shifted, not the value
  camtarget([x(iPoint) y(iPoint) z(iPoint)]);
  drawnow;
end

If you don't want the camera moving along the same curve, and instead want it to always be at a fixed offset from your moving camera target, you could try this:

offset = [-5 -5 0];  % X, Y, and Z offset from target
for iPoint = 1:numel(x)
  campos([x(iPoint)+offset(1) y(iPoint)+offset(2) z(iPoint)+offset(3)]);
  camtarget([x(iPoint) y(iPoint) z(iPoint)]);
  drawnow;
end

Finally, if you'd like to control the speed of the animation, you can replace the drawnow command with a call to pause. Note that a call to pause is equivalent to a call to drawnow in that it will force an update of graphics objects. You can also animate graphics using a timer object, as I illustrate in this answer.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • I tried your suggestion but the camera still moves different from what I intended. – New programmer Jul 17 '18 at 12:03
  • I edited my question and added the figure with the animated line to further explain my intention. I am trying to make the camera follow the line but a little bit behind it. It would be great if I be able to change the speed so it can move slowly. Thanks for your help! – New programmer Jul 17 '18 at 12:11
  • @Newprogrammer: It's still a little unclear exactly the sort of camera movement you want, but I added another option to my answer, along with how you can control the speed. – gnovice Jul 17 '18 at 20:07