 # Why isn't the box in the center of the screen?

``````float x=height/2;
float y=width/2;
float z=0;

void setup(){

size(400,400,P3D);
}
void draw(){
print(x,y,z);
background(255);
translate(x, y, z);
noFill();
box(160);
print(z);
z++;
}
``````
2 Likes

i’ve never used push and popmatrix i tried all these and none of them worked

``````float x=height/2;
float y=width/2;
float z=0;

void setup(){

size(400,400,P3D);
}
void draw(){
pushMatrix();
print(x,y,z);
background(255);
pushMatrix();
translate(x, y, z);
popMatrix();
noFill();
box(160);
print(z);
z++;
}
``````
``````float x=height/2;
float y=width/2;
float z=0;

void setup(){

size(400,400,P3D);
}
void draw(){
pushMatrix();
print(x,y,z);
background(255);
translate(x, y, z);
noFill();
box(160);
print(z);
popMatrix();
z++;
}
``````
``````float x=height/2;
float y=width/2;
float z=0;

void setup(){

size(400,400,P3D);
}
void draw(){
pushMatrix();
print(x,y,z);
background(255);
translate(x, y, z);
noFill();
box(160);
print(z);
z++;
popMatrix();
}
``````

sorry. you can ignore what i said i didn’t grok what the problem was.

Your definitions of `x` and `y` happen before `setup()` and since you use `height` and `width` (that are not yet set, you get unexpected results.

Also you define you x and y as float numbers but you are doing an integer division so you’ll always get an integer result.

``````float x, y;
float z = 0;

void setup(){
size(400, 400, P3D);
x = height / 2.0;
y = width / 2.0;
}
``````
1 Like

also is there a way to make the box not square?

I don’t want to be rude but part of your job is to at least look at the reference before asking a question.

You have the answer AND an example in the doc:

I actually did, no idea how i missed that one. i checked for this problem too and couldn’t find it.
why is there only one box in this code and not 2?

``````float x;
float y;
float z=0;
PImage shrek;

float x2;
float y2;
float z2=0;

void setup() {
size(400, 400, P3D);
x=width-60;
y=height/2;

x=0;
y=0;
}
void draw() {
background(255);
translate(x, y, z);
strokeWeight(10);
fill(0,170,255);
box(60, height-20, 1000);
push();
translate(x2,y2,z2);
box(60, height-20, 1000);
pop();
}
``````

You’re drawing the two boxes exactly at the same spot, as x2, y2, z2 are all set to 0.

i changed it a bit, it’s still not working

``````float x;
float y;
float z=0;
PImage shrek;

float x2;
float y2;
float z2=0;

void setup() {
size(400, 400, P3D);
x=width-60;
y=height/2;

x2=0;
y2=0;
}
void draw() {
background(255);
translate(x, y, z);
strokeWeight(10);
fill(0,170,255);
box(60, height-20, 1000);
push();
translate(x2,y2,z2);
box(60, height-20, 1000);
pop();
}
``````

It’s still the exact same problem. Look up “translate” in the reference.

Hi there,

(Woops looks like this was solved prior…)

It appears that you are assigning your values for x and y before width and height are specified in setup().
Step 1: Declare your variables x,y and z.
step 2: In setup assign their values (once size if defined).

Note; float and integers will be set to 0 when created so no need to add z = 0 (unless you really want to. ) Once size has run and width and height are defined, x and y will work as you wish.

Code works for me now.

float x,y,z;

void setup(){
size(400,400,P3D);
x = height/2;
y = width/2;

}

void draw(){
background(255);
noFill();
rectMode(CORNER);
translate(x, y, z);
box(160);
print(z);
z++;
}

Hope this helps.
Code away!

Cheers,
JS