#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How To Follow A 3D Object?

edited May 2016

Hi, i'm making a program that displays a 3D model of a Nitrate molecule, however i can't seem to figure out how to get my labels for the Oxygen to follow their respective atoms. My code is all below, can someone tell me what i have to do to get the big "O" to follow a red sphere, however to not rotate with them and just follow them on the outside as if they were on an x y plane??? Also, i haven't yet removed some of the extraneous code from my previous failed attempts so try and ignore that.

``````float theta;
float thetaY;

void setup(){
size(800,800,OPENGL);
background(0);
noStroke();
textSize(40);
}

void draw(){
background(0);
fill(255,0,0);
translate(400,400);
lights();
//O
pushMatrix();
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(width/4,height/3.5);
sphere(30);
fill(0,255,0);
strokeWeight(5);
point(200,200,-100);
fill(255);
//Label
fill(100);
strokeWeight(5);
line(0,0,-width/2,-height/2);
fill(255,0,0);
popMatrix();
pushMatrix();
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(-width/4,height/3.5);
sphere(30);
popMatrix();
pushMatrix();
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(width/50,-height/3);
sphere(30);
popMatrix();
//N
pushMatrix();
translate(width/500,height/500);
fill(0,0,255);
sphere(30);
fill(255);
text("N",-width/50,height/50,100);
popMatrix();
//Bonds
fill(100);
Label();
}

void Label(){
text("O",200,200,100);
}

void mousePressed(){
println(mouseX,mouseY);
}
``````
Tagged:

• Bump

there are a lot of ways to do that

this one is fairly elegant, since the O looks always in the camera (and is not seen from the side or so)

``````//

float theta;
float thetaY;

void setup() {
size(800, 800, OPENGL);
background(0);
noStroke();
textSize(40);
}

void draw() {
background(0);
fill(255, 0, 0);
translate(400, 400);
lights();

//O
pushMatrix();
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(width/4, height/3.5);
sphere(30); // this is one red sphere
// the sphere was drawn at (0, 0, 0), store that location
float x = modelX(0, 0, 0);
float y = modelY(0, 0, 0);
float z = modelZ(0, 0, 0);
popMatrix();

// new !!!!!!!!!!!!!!!!!!!!!
// draw another item at the same (x, y, z) coordinate as the other
pushMatrix();
translate(x, y, z);
translate(-365, -400); // take into account first translate
Label();
popMatrix();

//  pushMatrix(); // new !!!!!!!!!!!!!!!!!!!!!
//   rotateY(theta);
//   rotateX(thetaY);
//  translate(width/4, height/3.5);
//  translate(40, 0, 0);
//  fill(255, 255, 0);
//  Label();
//  popMatrix();  // new !!!!!!!!!!!!!!!!!!!!!

pushMatrix();
fill(255, 0, 0);
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(-width/4, height/3.5);
sphere(30);
popMatrix();
pushMatrix();
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(width/50, -height/3);
sphere(30);
popMatrix();
//N
pushMatrix();
translate(width/500, height/500);
fill(0, 0, 255);
sphere(30);
fill(255);
text("N", -width/50, height/50, 100);
popMatrix();
//Bonds
fill(100);
//  Label();
}

void Label() {
text("O", 0, 0, 0);
}

void mousePressed() {
println(mouseX, mouseY);
}
//
``````
• thank you, this works perfectly! Also, i'm trying to work out how to create a cylinder to represent the bonds. Do you have any suggestions on how to do this, i'm still new to the 3d dimensional stuff.

• edited June 2014

hi,

I can show you this. You can either use the normal line since it also accepts 3D parameters (from xyz and to xyz) and not only 2D params (from xy and to xy)

also there is an old routine for boxy connection.

but you might want to look at the libraries such as Shapes 3D and peasyCam

http://www.processing.org/reference/libraries

;-)

``````//

float theta;
float thetaY;

void setup() {
size(800, 800, OPENGL);
background(0);
noStroke();
textSize(40);
}

void draw() {
background(0);
fill(255, 0, 0);
translate(400, 400);
lights();

//O
pushMatrix();
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(width/4, height/3.5);
sphere(30); // this is one red sphere
// the sphere was drawn at (0, 0, 0), store that location
float x = modelX(0, 0, 0);
float y = modelY(0, 0, 0);
float z = modelZ(0, 0, 0);
popMatrix();

// new !!!!!!!!!!!!!!!!!!!!!
// draw another item at the same (x, y, z) coordinate as the other
pushMatrix();
translate(x, y, z);
translate(-365, -400); // take into account first translate
Label();
popMatrix();

//  pushMatrix(); // new !!!!!!!!!!!!!!!!!!!!!
//  rotateY(theta);
//  rotateX(thetaY);
//  translate(width/4, height/3.5);
//  translate(40, 0, 0);
//  fill(255, 255, 0);
//  Label();
//  popMatrix();  // new !!!!!!!!!!!!!!!!!!!!!

pushMatrix();
fill(255, 0, 0);
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(-width/4, height/3.5);
sphere(30);
// the sphere was drawn at (0, 0, 0), store that location
float x1 = modelX(0, 0, 0);
float y1 = modelY(0, 0, 0);
float z1 = modelZ(0, 0, 0);
popMatrix();

pushMatrix();
stroke(255, 2, 2);
translate(-400, -400); // take into account first translate
strokeWeight(5);
//line(x, y, z,      x1, y1, z1); // works as well
myBox(x, y, z,
x1, y1, z1,
9,
color(255, 2, 2));
noStroke();
popMatrix();

pushMatrix();
theta = (PI*mouseX / width)*2;
thetaY = (PI*mouseY / height)*2;
rotateY(theta);
rotateX(thetaY);
translate(width/50, -height/3);
sphere(30);
popMatrix();
//N
pushMatrix();
translate(width/500, height/500);
fill(0, 0, 255);
sphere(30);
fill(255);
text("N", -width/50, height/50, 100);
popMatrix();
//Bonds
fill(100);
//  Label();
}

void Label() {
text("O", 0, 0, 0);
}

void mousePressed() {
println(mouseX, mouseY);
}

void myBox(float x1, float y1, float z1,
float x2, float y2, float z2,
float weight,
color strokeColour)
// draws a 3D line.
// was called drawLine; programmed by James Carruthers
// see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9
{
PVector p1 = new PVector(x1, y1, z1);
PVector p2 = new PVector(x2, y2, z2);
PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
float rho = sqrt(pow(v1.x, 2)+pow(v1.y, 2)+pow(v1.z, 2));
float phi = acos(v1.z/rho);
float the = atan2(v1.y, v1.x);
v1.mult(0.5);

pushMatrix();
translate(x1, y1, z1);
translate(v1.x, v1.y, v1.z);
rotateZ(the);
rotateY(phi);
noStroke();
fill(strokeColour);
// box(weight,weight,p1.dist(p2)*1.2);
box(weight, weight, p1.dist(p2));
popMatrix();
}

//
``````