We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Camera follow moving object.
Page Index Toggle Pages: 1
Camera follow moving object. (Read 6423 times)
Camera follow moving object.
Jan 11th, 2010, 7:47am
 
I want the camera "view" to follow the object as it moves.

Code:
 int Line_X, Line_Y, Line_Z;

void setup() {
size(640, 360, P3D);
fill(204);
}

void draw() {
lights();
background(0);

// Change height of the camera with mouseY
camera(0, 0, 220, // eyeX, eyeY, eyeZ
0.0, 0.0, 0.0, // centerX, centerY, centerZ
0.0, 1.0, 0.0); // upX, upY, upZ

stroke(255);
line(-100 + Line_X, +Line_Y, Line_Z, 100 + Line_X, 0 + Line_Y, 0 + Line_Z);
Line_X++;
}



Why does not this work:  
Code:
camera(Line_X, 0, 220, // eyeX, eyeY, eyeZ
0.0, 0.0, 0.0, // centerX, centerY, centerZ
0.0, 1.0, 0.0); // upX, upY, upZ


I want the "camera" to follow the line. Huh


I have also tried:
Code:
beginCamera();
translate(-Line_X,0,0);
endCamera();


Code:
beginCamera();
translate(Line_X,0,0);
endCamera();


Re: Camera follow moving object.
Reply #1 - Jan 11th, 2010, 12:25pm
 

Hello!

It is pretty hard to see the line flying
when the camera is at the same speed, because
from the camera's perspective, the line always looks
the same.

Note the box which is faster.

Greetings,

Chrisir

Sorry, couldn't paste using processing's copy for discurse function.



int Speed = 3;

int Line_X = 0;
int Line_Y = 80;
int Line_Z = -100;

int Line_Y_Add = -30;


void setup() {

 size(640, 360, P3D);
 fill(204);
 noSmooth();

}

void draw() {

 lights();
 background(color(0,0,54));
 MyNiceFloor();
 stroke(255);

 line ( 100 + Line_X, Line_Y, Line_Z,
 120 + Line_X, Line_Y_Add + Line_Y, 10 + Line_Z);

 MyBox ( Line_X* 2, Line_Y, 10,
 4, 7, 10,
 0, 0, 0 );

 camera(Line_X+5, +Line_Y+10, Line_Z-20, // eyeX, eyeY, eyeZ      
 100 + Line_X, +Line_Y, Line_Z,          // centerX, centerY, centerZ        
 0.0, 1.0, 0.0);                         // upX, upY, upZ

 Line_X+= Speed;

}

void MyNiceFloor() {

 noStroke();

 for (int i = 0; i < 40; i = i+1) {
   for (int j = 0; j < 40; j = j+1) {

     if (i%2 == 0) {
       if ((j%2 == 0)) {
         fill (255,0,0);
       }
       else
       {
         fill ( 103 );
       }
     }  
     else {
       if (j%2 == 0) {
         fill ( 103 );
       }
       else
       {
         fill (255,0,0);
       }
     }

     pushMatrix();
     translate (40*i+100,160,40*j-640 );
     box (40,7,40);

     popMatrix();

   } // for
 } // for


}

void MyBox ( float x,float y,float z,
float w,float h,float d,  
float RotateX, float RotateY, float RotateZ ) {

 noStroke();
 fill (#FEFF03);

 pushMatrix();

 rotateX ( radians(RotateX));
 rotateY ( radians(RotateY));
 rotateZ ( radians(RotateZ ));
 translate ( x,y,z );
 box ( w,h,d );

 popMatrix();

}
Re: Camera follow moving object.
Reply #2 - Jan 11th, 2010, 12:27pm
 
you could still use [ code ][/ code ] Smiley
Re: Camera follow moving object.
Reply #3 - Jan 11th, 2010, 12:50pm
 
I will not draw background(0). so the line will leave a trail : ) that was my  
idea Huh
Re: Camera follow moving object.
Reply #4 - Jan 11th, 2010, 1:07pm
 
FWIW, here's another little demo showing a basic, modulated 3rd person camera:

Code:
import processing.opengl.*;
import toxi.geom.*;

// camera view target
Vec3D target=new Vec3D();
// camera offset
Vec3D eyeMod=new Vec3D(0,0,200);

void setup() {
size(1024,576,OPENGL);
}

void draw() {
background(255);
// smoothly update view target based on mouse position
target.interpolateToSelf(new Vec3D(mouseX-width/2,mouseY-height/2,0),0.05);
// manipulate/animate eye offset vector
eyeMod.set(100*sin(frameCount*0.02),0,200);
// make eye pos relative to target
Vec3D eyePos=target.add(eyeMod);
// set camera
camera(eyePos.x,eyePos.y,eyePos.z,target.x,target.y,target.z,0,-1,0);
// draw static objects at fixed coordinates
for(int i=0; i<10; i++) {
pushMatrix();
translate((i-5)*50,0,-50);
box(20);
popMatrix();
}
// draw focused object at camera's view target pos
translate(target.x,target.y,target.z);
box(50);
}


To have the camera stationary (not relative to the target), but still looking at the target replace these lines...

Code:
eyeMod.set(100*sin(frameCount*0.02),0,200);
// make eye pos relative to target
Vec3D eyePos=target.add(eyeMod);

with just...
Code:
Vec3D eyePos=eyeMod; 

Re: Camera follow moving object.
Reply #5 - Jan 11th, 2010, 1:09pm
 


your idea
quote :
Quote:
I will not draw background(0). so the line will leave a trail : ) that was my  idea  


won't work.

Look at my example:
The line stays put on the screen because it's synchronized with the camera.

For effects, make the camera faster than the line.

Greetings!

Re: Camera follow moving object.
Reply #6 - Jan 11th, 2010, 1:13pm
 
Do u have a link to the toxi lib ?
Re: Camera follow moving object.
Reply #7 - Jan 11th, 2010, 1:16pm
 
http://code.google.com/p/toxiclibs/
Re: Camera follow moving object.
Reply #8 - Jan 11th, 2010, 1:18pm
 
sure, http://toxiclibs.org/ - you'll only need to download & install the toxiclibscore package for this...
Re: Camera follow moving object.
Reply #9 - Jan 11th, 2010, 1:42pm
 
Thanks for all help!

but this seems not possible.

All i want is  to keep the Object line center and let it leave a trail behind as it moves forward.

In other words:
The main idea is to have the user follow the line. And the line will move randomly around X,Y.
Re: Camera follow moving object.
Reply #10 - Jan 11th, 2010, 2:07pm
 
1) Nothing is impossible! Smiley

2) With the added information now this is a bit different premise/objective than what you asked for originally: If the camera is always anchored to the line than you'll simply not see any trace in the projection because the line will always be in the same position. To get a trace of previous line/object positions you'll need to store them in an array/buffer of sorts and re-draw that history every frame, but NOT simply rely on not clearing the background...

Modified example:

Code:
import processing.opengl.*;
import toxi.geom.*;

// camera view target
Vec3D target=new Vec3D();
// camera offset
Vec3D eyeMod=new Vec3D(0,0,200);

ArrayList history=new ArrayList();

void setup() {
size(1024,576,OPENGL);
}

void draw() {
background(255);
// animate along Z
float currZ=200*sin(frameCount*0.01);
// smoothly update view target based on mouse position
target.interpolateToSelf(new Vec3D(mouseX-width/2,mouseY-height/2,currZ),0.05);
Vec3D eyePos=target.add(eyeMod);
camera(eyePos.x,eyePos.y,eyePos.z,target.x,target.y,target.z,0,-1,0);
// draw static objects at fixed coordinates
for(Iterator i=history.iterator(); i.hasNext();) {
Vec3D p=(Vec3D)i.next();
pushMatrix();
translate(p.x,p.y,p.z);
box(10);
popMatrix();
}
// draw focused object at camera's view target pos
translate(target.x,target.y,target.z);
box(10);
history.add(target.copy());
}


Makes sense?
Re: Camera follow moving object.
Reply #11 - Jan 11th, 2010, 2:33pm
 
Wow. really nice = ) Huh how would you make the "object" move at an random angle?

Guess its this line:
Code:
 target.interpolateToSelf(new Vec3D(mouseX-width/2,mouseY-height/2,currZ),0.05); 



So, can i:
vars  XSpeed,YSpeed,ZSpeed  and Diriction.

Code:
 target.interpolateToSelf(new Vec3D( XSpeed,YSpeed,ZSpeed ),0.05); 



Then random diriction and base of it it randomizes one of "Speed-vars" to a numer ex Random(5-20)
Re: Camera follow moving object.
Reply #12 - Jan 11th, 2010, 3:14pm
 
Thankx alot ! That code really helped me Toxic. I manage to finnish my projekt ! Smiley
Page Index Toggle Pages: 1