We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi All,
Looking for a way to make a sphere (or anything really) rotate in 3d to look at the camera or another object. It seems so simple, but it's driving me crazy. I've got a pretty close (I think) solution, but I feel like I'm just inputting the wrong values or using the wrong math method.
here's where I'm at:
PVector eye, pos;
void setup() {
size(800, 800, P3D);
smooth(8);
eye = new PVector(0.0f, 0.0f, -1000.0f);
pos = new PVector(0, 0, 0);
}
void draw() {
background(0);
lights();
// eye.x=0;
// eye.y=0;
// eye.z=-1000;
camera(eye.x, eye.y, eye.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
translate(pos.x, pos.y, pos.z);
//PVector rMatrix = faceCamera(eye.get(), pos.get());
PVector rMatrix = faceCamera(new PVector(mouseX+width/2, mouseY+height/2, -1000), pos.get());//simulate camera movement
rotateX(rMatrix.x);
rotateY(rMatrix.y);
rotateZ(rMatrix.z);
sphere(200);
}
void keyPressed() {
if(keyCode == 38) {
eye.y -=5;
}
if(keyCode == 40) {
eye.y +=5;
}
if(keyCode == 37) {
eye.x -=5;
}
if(keyCode == 39) {
eye.x +=5;
}
println(eye);
}
PVector faceCamera(PVector c, PVector p) {
c.normalize();
p.normalize();
float dx = p.x - c.x;
float dy = p.y - c.y;
float dz = p.z - c.z;
PVector ang = new PVector();
ang.x = atan2(dz, dy);
ang.y = atan2(dx, dz);
ang.z = atan2(dy, dx);
return ang;
}
I'm using the mouse to represent the camera to speed up testing, but in the end, I'll have it always pointed to the camera. I'd like to avoid using a camera library like Proscene just to keep things simple, but I see that there is an example that works for what I'm trying to do.
Any suggestions would be awesome!
thanks! ak
Answers
I did a similar thing once.
I rotated a cam around a scene.
When I wanted to display a text looking towards it I just said
So I used the camAngle and just rotated the letter back to it.
I can't apply this to your code, but just to get you going
here
to try it, use l and r
( or + and - )
Awesome Chrisir, This looks very promising!
Ill let you know how it works out!
Thanks!
Hmm, it's close, but it looks like it's more of an orthographic camera solution. It always looks forward, which is good, but it isn't always looking straight at the camera. Here's how I implemented it:
I think I can adapt a formula I used for a different project to solve from this though. Ill post my results.
Thanks! ak