Advertisement

Camera Movement

Started by March 30, 2002 03:13 PM
19 comments, last by ThinIce 22 years, 10 months ago
Hello, I''m working on a demo, using OpenGL, and I want to make the camera move on it''s own basically, Where the user sits back and watches, and the camera / View does all the work, automatic movement. Someone suggested I record KeyFrames, but I''m afraid I do not know much about keyframes. Could someone fill me in on that subject, or point me in a direction where I can learn about them. Or maybe someone could make another suggestion / Idea on how to do so. I want a product such as the 1st place contestant in the Halloween contest NeHe has held, he had the camera move on a path. I want something similar to that. Thanks in advance, Any ideas are welcome!
=ThinIce=
After reading a section on Camera Control Techniques in Game Programming Gems, I was able to implement a camera script class that read a table of camera positions and orientations and used either bspline (close to specified values) or catmull-rom (pass-through/on specified values) to position the camera smoothly. It was alot easier than I envisioned.

The table could be saved and loaded as a plain-text file, and the script created on the fly by recording a single camera position/orientation per some time-interval. I setup a few keys to work like a recorder:

alt-r toggle record w/ on-screen annotation of frame index
alt-p toggle playback
alt-s save script
alt-l load script

Then when my scene was constructed, I started recording, and just did a manual flythrough of the path I wanted, at the speed I wanted, then stopped recording and saved it. Then either the code or a keystroke could load and run the script.

I haven''t used it much, so there may be more tweaks to make (like code to ensure constant speed vs. programmed speed, autosave, single-run/loop, etc)

zin

zintel.com - 3d graphics & more or less
zintel.com - 3d graphics & more or less
Advertisement
Yes, but how would I store the data, like what kind of stuff would I save, I know I''d need to save the positioning, and stuff.

Could you link or email me a demo, or the source of the code, so I could look at it? Thx in advance!
=ThinIce=
> Yes, but how would I store the data

In a float array or list. In a file, as plain ascii text.


> like what kind of stuff would I save

I just saved the x/y/z position, and pitch/heading/roll orientation, 6 vars. One every second.

I don''t have any code posted that has it, but in 2 weeks my Lord of the Rings app will be on NeHe. Its just basic c code, roughly:

//----------------------------------------------------

int nodes[100][6]; // table
int nnodes=0; // stored nodes
int maxnode=99;

void capture(void) { // called to capture one camera site
nodes[nnodes][0]=camera.pos.x; // save camera data
nodes[nnodes][1]=camera.pos.y;
nodes[nnodes][2]=camera.pos.z;
nodes[nnodes][3]=camera.orient.x;
nodes[nnodes][4]=camera.orient.y;
nodes[nnodes][5]=camera.orient.z;
if (nnodes}

//----------------------------------------------------

float position; // current position within current index, 0->.9999
int index; // current script index/frame, 0->maxnode;

void update(void) { called at the start of every frame
position+=speed*dt; // move forward
while (position>=1) { // onto next frame?
position-=1; // adjust position back 1
index=(index>=nnodes)?0:nnodes+1; // bump index around circularly
}
}

//----------------------------------------------------

void playback(void) { // called when setting camera in display
camera.pos.x=interpolate(index.x, nextCircularIndex.x, position);
camera.pos.y=interpolate(index.y, nextCircularIndex.y, position);
.
.
.
}

The code to save and load is straight c or c++ for string data, a sample of my saved script w/ x y z pitch heading roll, note I don''t use roll:

-48.260, 3.607, 48.956, -308.7, -80.0, 0.0
-24.939, 12.348, 59.700, -312.2, -59.7, 0.0
4.721, 22.591, 65.198, -311.2, -38.8, 0.0
35.327, 34.532, 61.631, -302.3, -11.2, 0.0
61.879, 49.858, 50.669, -295.6, -10.1, 0.0


I used to use a simple linear interpolater for orientation, and cosine interpolater for position, different methods yield different camera behaviours, like cosine interpolation will "pause" at every node, bspline is alleged to be the smoothest, thought it doesn''t actually go to every node, if that''s important...I can set which method in my camerascript class.

zin
zintel.com - 3d graphics & more or less
Woah, thats pretty cool, very automated and efficient from my first look. It''s going to a while for me to absorb it though.
Thanks though, You''ve been a great help!
=ThinIce=
Some code got dropped due to html interpretation and a typo:

void capture(void) { // called to capture one camera site
nodes[nnodes][0]=camera.pos.x; // save camera data
nodes[nnodes][1]=camera.pos.y;
nodes[nnodes][2]=camera.pos.z;
nodes[nnodes][3]=camera.orient.x;
nodes[nnodes][4]=camera.orient.y;
nodes[nnodes][5]=camera.orient.z;
if (nnodes .lt. maxnode) nnodes++; // dang lessthan brackets

obviously you want to increment the node counter after inserting the camera values, and perhaps use a linked list instead, this was my first attempt at writing the script, and I use arrays at first.

How do I insert code properly in a post, in copyable form?

zin

zintel.com - 3d graphics & more or less
zintel.com - 3d graphics & more or less
Advertisement
Ok I have never tried this before and I have basic knowledge of 3D, but wouldn''t it be easier and less memory taken up if you had keypoints then use vectors to get to that point e.g.

int CurKeyPoint; // To Know which keypoint u after
unsigned char KeyPoints[3];
KeyPoints[0].x=5;
KeyPoints[0].x=10;
etc just fill it in.

then give the demo a starting place 0,0,0,. If you want it to look really class use vectors and trig so that it slowly moves to the fist key point. Update movement once every second so it doesn''t get to the keypoints instantly.

If you can''t be bothred with that use shitty algorithm like thist
if(cameraXif(cameraYetc

when u reach the keypoint u do CurKeyPoint++;
then it will search 4 the next key point and so on. I hope this helps. but like I said I have never tried this and I have never done any 3D programming
who is it that keeps on nicking WizHarD name !! :P
quote:
Original post by zin
How do I insert code properly in a post, in copyable form?


Use and tags (without spaces!).

Newbie programmers think programming is hard.Amature programmers think programming is easy.Professional programmers know programming is hard.
Confusion. could you please repost all of the pevious code using the code braces? That way I can get a crystal clear idea. Thx for your help so far! Happy Easter btw!
=ThinIce=
Thanks NaliXL.
What I don''t like is the code isn''t cut/pastable. What about an html
 tag?   HYere it is w/ a code thing, I fixed some typos too.   Might still be some.#define MAXNODES 100int nodes[MAXNODES][6];   // tableint nnodes=0;                     // # stored nodesfloat position;                     // current position within current frame, 0->.9999int index;                           // current script index/frame, 0->maxnode;//--------------------------------------------------------void capture(void) {      // called to capture one camera site    if (nnodes>=MAXNODES) return;    nodes[nnodes][0]=camera.pos.x;      // save camera data    nodes[nnodes][1]=camera.pos.y;    nodes[nnodes][2]=camera.pos.z;    nodes[nnodes][3]=camera.orient.x;    nodes[nnodes][4]=camera.orient.y;    nodes[nnodes][5]=camera.orient.z;    nnodes++;}//----------------------------------------------------void update(void) {                  // called at the start of every frame    position+=speed*dt;             // move forward,  dt=frame to frame time    while (position>=1) {            // onto next frame?        position-=1;                    // adjust position back 1        index=(index+1)%nnodes;  // bump index around circularly   (big typo here before!)    }//----------------------------------------------------void playback(void) {         // called when setting camera in display    int nextIndex=(index+1)%nnodes;    camera.pos.x=interpolate(nodes[      index][0],                                       nodes[nextIndex][0], position);    camera.pos.y=interpolate(nodes[      index][1],                                       nodes[nextIndex][1], position);    camera.pos.z=interpolate(nodes[      index][2],                                       nodes[nextIndex][2], position);    camera.orient.x=interpolate(nodes[      index][3],                                          nodes[nextIndex][3], position);    camera.orient.y=interpolate(nodes[      index][4],                                          nodes[nextIndex][4], position);    camera.orient.z=interpolate(nodes[      index][5],                                          nodes[nextIndex][5], position);}//-----------------------------------------------------  zin    
zintel.com - 3d graphics & more or less

This topic is closed to new replies.

Advertisement