# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.6957214,"math_prob":0.99574566,"size":3287,"snap":"2021-43-2021-49","text_gpt3_token_len":1030,"char_repetition_ratio":0.14620773,"word_repetition_ratio":0.26045626,"special_character_ratio":0.36842105,"punctuation_ratio":0.2449465,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99306375,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-27T10:09:13Z\",\"WARC-Record-ID\":\"<urn:uuid:25718016-df1a-4728-81b9-c4445b44f75f>\",\"Content-Length\":\"38757\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0a26411b-a581-4639-aeee-a1d8c170d4fc>\",\"WARC-Concurrent-To\":\"<urn:uuid:a6bf7b4f-021f-4ab1-a207-5179f2f37302>\",\"WARC-IP-Address\":\"64.62.250.110\",\"WARC-Target-URI\":\"https://discourse.processing.org/t/why-isnt-the-box-in-the-center-of-the-screen/32580\",\"WARC-Payload-Digest\":\"sha1:PKEOD2N63YX5KAPZC5ZEAZYA52DNLM4A\",\"WARC-Block-Digest\":\"sha1:4F7NNVDCRTTPJEVCG27TWPCHBIR7ZZVR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323588113.25_warc_CC-MAIN-20211027084718-20211027114718-00559.warc.gz\"}"}