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.
Page Index Toggle Pages: 1
Camera Follower (Read 614 times)
Camera Follower
May 14th, 2009, 12:38pm
 
Hi I have been trying for a couple of hours to make a camera follow
a box(30) around the screen.

I can't seem to figure out how to lock the camera onto a coordinate and at the same time keep roughly the same distance to the target.
The camera(eyeX, eyeY, eyeZ, targetX, targetY, targetZ, 0, 1, 0) seems to be a simple method of controlling the camera at first, but I can't get it to keep up with my box sprite.

My initial idea was to make a small class called DriftCam that would use perlin noise to
position itself around a box, the camera would always be moving slowly around the box, I got that working. Next challenge was to move the box around in an OPENGL sketch and have the camera following it, still moving randomly around.

Code:

import processing.opengl.*;
import javax.media.opengl.*;

DriftCam drift;

void setup() {
size(400, 400, OPENGL);
background(0);

PVector c = new PVector(0, 0, 10); //camera eye start position
PVector t = new PVector(0, 0, 0); //target/box start position

drift = new DriftCam(c, t);
}

void draw()
{
background(0);
lights();
fill(255,0,0);
noStroke();
//new coordinate vector
PVector v = new PVector(0, frameCount, 0);
drift.setTarget(v);
//guess I need to setCam(?, ?, ?) here ....
drift.update();
//translate to the position the camera is looking at..
translate(p.x, p.y, p.z);
box(30);
}


And the DriftCamera class:

Code:

public class DriftCam
{
private PVector target;
private PVector eye;
private float xoff = 0, yoff = 0, zoff = 0;
private float radius = 150;
private boolean auto;

DriftCam(PVector c, PVector t)
{
target = t;
eye = c;
}
public void setTarget(PVector t)
{
target = t;
}

public void setCam(PVector c)
{
eye = c;
}

public void update()
{
xoff += random(0.001, 0.009);
yoff += random(0.001, 0.009);
zoff += random(0.00001, 0.0009);

eye.x = noise(xoff) * radius;
eye.y = noise(yoff) * radius;
eye.z = radius;//noise(zoff) * radius + 40;

camera( eye.x, eye.y, eye.z,
target.x, target.y, target.z,
0, 1, 0 );

}
}


The problem is that the camera target works fine, but how to move the camera around.
I been looking into my calculus regarding spheres and vectors, but I can't seem to find something that helps me here.
The example above will work, but behave as if the box slowly wanders out of the cameras view..

Dream scenario:
The box moves around randomly and instead of calling background(0) in each draw routine the boxes stays in the scene and draws a sort of 3D snake (like in the snake game) on the screen, the camera follows it around. Later on I will look at having the box not cross its own path, but for now the above would be nice to achieve.

Hope some of you guys out there can help me out as Im pushing my spacial intellect to it's limits  Cheesy
Page Index Toggle Pages: 1