HowToC:FollowAnObject
From S2PLOT
(Difference between revisions)
												
			
			| Revision as of 23:39, 19 August 2008 Dbarnes (Talk | contribs) (→How to follow an object with the camera) ← Previous diff  | 
				Revision as of 23:40, 19 August 2008 Dbarnes (Talk | contribs) (→How to follow an object with the camera) Next diff →  | 
			||
| Line 25: | Line 25: | ||
| void callback(double *t, int *kc) { | void callback(double *t, int *kc) { | ||
| // ... | // ... | ||
| - | |||
| if (camfollow >= 0) { | if (camfollow >= 0) { | ||
| - | + | XYZ position, up, vdir; | |
| - | + | ss2qc(&position, &up, &vdir, 1); | |
| - | + | XYZ vright = CrossProduct(vdir, up); | |
| + | vdir = VectorSub(position, iP); | ||
| + | Normalise(&vdir); | ||
| + | up = CrossProduct(vright, vdir); | ||
| + | ss2sc(position, up, vdir, 1); | ||
| } | } | ||
| + | // ... | ||
| } | } | ||
| </pre></code> | </pre></code> | ||
Revision as of 23:40, 19 August 2008
How to follow an object with the camera
Using handles, you can easily detect when an object has been right-clicked. With two pretty simple bits of code, you can then make the camera keep focussed on that object.
1. use a handle callback to register the most recently selected handle:
int camfollow; // initialised to -1 in main
unsigned char *selected; // array storing selected state of all handles
void pickcb(int *id) {
  // ...
  // toggle state:
  selected[*id] = (selected[*id] + 1) % 2;
  if (selected[*id]) {
    camfollow = *id;
  } else {
    camfollow = -1;
  }
  s2togglehandle(*id);
}
2. in the standard dynamic callback, set the camera position if required XYZ *iP; // array storing positions of all handles
void callback(double *t, int *kc) {
  // ...
  if (camfollow >= 0) {
    XYZ position, up, vdir;
    ss2qc(&position, &up, &vdir, 1);
    XYZ vright = CrossProduct(vdir, up);
    vdir = VectorSub(position, iP);
    Normalise(&vdir);
    up = CrossProduct(vright, vdir);
    ss2sc(position, up, vdir, 1);
  }
  // ...
}
						
			
		