4

I have a graphical object which is moving along a trajectory. How can I make the camera follow the object?

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
littleGirl_dev
  • 131
  • 2
  • 8

2 Answers2

12

Let's draw a planet and its satellite, with the camera following the moon from a view directed toward the Earth. For example:

a = {-3.5, 3.5}; 
Animate[
 Show[
      Graphics3D[
           Sphere[3 {Cos@t, Sin@t, 0}, .5],  
                 ViewPoint -> 3.5 {Cos@t, Sin@t, 0},     
                 SphericalRegion -> True, 
                 PlotRange -> {a, a, a}, Axes -> False, Boxed -> False],
      myEarth], 
{t, 0, 2 Pi}]  

Where myEarth is another 3D Graphics (for reference).

enter image description here

Static vertical view:

a = {-3.5, 3.5}; 
Animate[
 Show[
      Graphics3D[
           Sphere[3 {Cos@t, Sin@t, 0}, .5],  
                 ViewPoint -> 3.5 {0,0,1},     
                 SphericalRegion -> True, 
                 PlotRange -> {a, a, a}, Axes -> False, Boxed -> False],
      myEarth], 
{t, 0, 2 Pi}]  

enter image description here

The trick is SphericalRegion -> True, without it the image perspective "moves" from frame to frame.

Edit

With two static objects:

enter image description here

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • +1. The OP mentions "2D" and "camera" in the question title, which confuses me as to whether a `Graphics` or `Graphics3D` solution is sought. You might consider adding the `Graphics` equivalent of your answer just to cover all the possibilities, e.g. `Graphics[..., PlotRange->{a, a} + f[t], ImagePadding->None, ...]` – WReach Mar 21 '11 at 13:22
  • 1
    ViewCenter and ViewVertical are also options that may be useful in this context. – Sjoerd C. de Vries Mar 21 '11 at 14:08
  • @WReach I guess "camera" and "2D" are mutually exclusive. As "camera" is rather difficult to misspell, seems to me that 2D should be 3D. Regrettably the OP didn't include a code snippet to understand the requirement better. Please feel free to add another answer for the 2D case if you think it is worth. – Dr. belisarius Mar 21 '11 at 14:50
  • Thanks alot for your great answer! It is very helpful. But I asked about 2D animation. – littleGirl_dev Mar 22 '11 at 19:14
  • There you have Simon's psychedelic (aka drunken) icon :D – Dr. belisarius Mar 22 '11 at 22:36
5

Since the question asks about 2D, here's how you can emulate a camera in 2D Graphics.

First, let's get the stackoverflow favicon.ico:

so = First@Import["http://sstatic.net/stackoverflow/img/favicon.ico"]

Well put this on top of some overlapping circles and make the "camera" follow the icon around by adjusting the PlotRange

Manipulate[Graphics[{
   Table[Circle[{j, 0}, i], {i, 0, 1, .1}, {j, {-.5, .5}}],
   Inset[so, pos, {0, 0}, .2]},
  PlotRange -> {{-.5, .5}, {-.5, .5}} + pos],
 {{pos, {0, 0}, ""}, {-1.4, -1}, {1.4, 1}, ControlPlacement -> Left}]

manipulate

To show how it works (with out putting the above into Mathematica), we need to animate it. Originally I chose a variable step random walk drunk = Accumulate[RandomReal[{-.1, .1}, {200, 2}]] but it was a unpredictable! So instead, we'll make the icon follow the ABC logo

drunk = Table[{1.5 Sin[t], Cos[3 t]}, {t, 0, 2 Pi, .1}];
Animate[Graphics[{
   Table[Circle[{j, 0}, i], {i, 0, 1, .1}, {j, {-.5, .5}}],
   Inset[so, drunk[[pos]], {0, 0}, .2]},
  PlotRange -> {{-.5, .5}, {-.5, .5}} + drunk[[pos]]],
 {pos, 1, Length[drunk], 1}]

animated

Simon
  • 14,631
  • 4
  • 41
  • 101
  • Looking at the comments of @belisarius' beautiful answer, I see that my solution is exactly what @WReach recommended... I think that this is the only sensible way to do this with 2D Graphics. – Simon Mar 21 '11 at 22:07