# P3D running extremely slow

**URL:** <https://discourse.processing.org/t/p3d-running-extremely-slow/30302>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 26, 2021, 6:52am UTC](https://discourse.processing.org/t/p3d-running-extremely-slow/30302 "2021-05-26T06:52:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![123](https://avatars.discourse-cdn.com/v4/letter/1/7feea3/32.png) [@123](https://discourse.processing.org/u/123)\
**Post date:** [May 26, 2021, 6:52am UTC](https://discourse.processing.org/t/p3d-running-extremely-slow/30302/1 "2021-05-26T06:52:17Z")

</div>

Guys pls help I don’t know why this code runs really slow.

//SEQUENCES OF MOVING CIRCLES  
float xPosition = 0;  
float yPosition;  
float aPosition = 0;  
float bPosition;  
float onePosition = 0;  
float twoPosition;  
float threePosition = 0;  
float fourPosition;  
float fivePosition = 0;  
float sixPosition;  
float sevenPosition = 0;  
float eightPosition;  
float ninePosition = 0;  
float tenPosition;  
float elevenPosition = 0;  
float twelvePosition;  
float thirteenPosition = 0;  
float fourteenPosition;  
float fifteenPosition = 0;  
float sixteenPosition;  
float seventeenPosition = 0;  
float eighteenPosition;  
float nineteenPosition = 0;  
float tweentyPosition;  
float tweentyonePosition = 0;  
float tweentytwoPosition;

//ANIMATED GIF  
PImage[] gifone = new PImage[67];  
int gifoneX = -100, gifoneY = 0;

void setup () {  
fullScreen (P3D);  
frameRate(50);  
smooth();  
noStroke ();

//sequences of moving circles  
bPosition = 100;  
yPosition = 150;  
twoPosition = 200;  
fourPosition = 250;  
sixPosition = 300;  
eightPosition = 350;  
tenPosition = 400;  
twelvePosition = 450;  
fourteenPosition = 500;  
eighteenPosition = 550;  
tweentyPosition = 600;  
tweentytwoPosition = 650;

gifone[0] = loadImage( “0.gif” );  
gifone[1] = loadImage( “1.gif” );  
gifone[2] = loadImage( “2.gif” );  
gifone[3] = loadImage( “3.gif” );  
gifone[4] = loadImage( “4.gif” );  
gifone[5] = loadImage( “5.gif” );  
gifone[6] = loadImage( “6.gif” );  
gifone[7] = loadImage( “7.gif” );  
gifone[8] = loadImage( “8.gif” );  
gifone[9] = loadImage( “9.gif” );  
gifone[10] = loadImage( “10.gif” );  
gifone[11] = loadImage( “11.gif” );  
gifone[12] = loadImage( “12.gif” );  
gifone[13] = loadImage( “13.gif” );  
gifone[14] = loadImage( “14.gif” );  
gifone[15] = loadImage( “15.gif” );  
gifone[16] = loadImage( “16.gif” );  
gifone[17] = loadImage( “17.gif” );  
gifone[18] = loadImage( “18.gif” );  
gifone[19] = loadImage( “19.gif” );  
gifone[20] = loadImage( “20.gif” );  
gifone[21] = loadImage( “21.gif” );  
gifone[22] = loadImage( “22.gif” );  
gifone[23] = loadImage( “23.gif” );  
gifone[24] = loadImage( “24.gif” );  
gifone[25] = loadImage( “25.gif” );  
gifone[26] = loadImage( “26.gif” );  
gifone[27] = loadImage( “27.gif” );  
gifone[28] = loadImage( “28.gif” );  
gifone[29] = loadImage( “29.gif” );  
gifone[30] = loadImage( “30.gif” );  
gifone[31] = loadImage( “31.gif” );  
gifone[32] = loadImage( “32.gif” );  
gifone[33] = loadImage( “33.gif” );  
gifone[34] = loadImage( “34.gif” );  
gifone[35] = loadImage( “35.gif” );  
gifone[36] = loadImage( “36.gif” );  
gifone[37] = loadImage( “37.gif” );  
gifone[38] = loadImage( “38.gif” );  
gifone[39] = loadImage( “39.gif” );  
gifone[40] = loadImage( “40.gif” );  
gifone[41] = loadImage( “41.gif” );  
gifone[42] = loadImage( “42.gif” );  
gifone[43] = loadImage( “43.gif” );  
gifone[44] = loadImage( “44.gif” );  
gifone[45] = loadImage( “45.gif” );  
gifone[46] = loadImage( “46.gif” );  
gifone[47] = loadImage( “47.gif” );  
gifone[48] = loadImage( “48.gif” );  
gifone[49] = loadImage( “49.gif” );  
gifone[50] = loadImage( “50.gif” );  
gifone[51] = loadImage( “51.gif” );  
gifone[52] = loadImage( “52.gif” );  
gifone[53] = loadImage( “53.gif” );  
gifone[54] = loadImage( “54.gif” );  
gifone[55] = loadImage( “55.gif” );  
gifone[56] = loadImage( “56.gif” );  
gifone[57] = loadImage( “57.gif” );  
gifone[58] = loadImage( “58.gif” );  
gifone[59] = loadImage( “59.gif” );  
gifone[60] = loadImage( “60.gif” );  
gifone[61] = loadImage( “61.gif” );  
gifone[62] = loadImage( “62.gif” );  
gifone[63] = loadImage( “63.gif” );  
gifone[64] = loadImage( “64.gif” );  
gifone[65] = loadImage( “65.gif” );  
gifone[66] = loadImage( “66.gif” );  
}

void mousePressed () {  
noLoop ();  
}  
void mouseReleased () {  
loop ();  
}  
void draw () {  
background(255);

//animated gif  
image( gifone[frameCount%67], gifoneX, gifoneY);  
gifoneX = gifoneX + 5;  
if ( gifoneX \> width + 10) {  
gifoneX = -250;  
}

loop();  
//sequences of circles  
//ab  
fill (0, 20, 220);  
ellipse(aPosition, bPosition, 50, 50);  
aPosition +=10;  
//xy  
fill (0, 20, 220);  
ellipse(xPosition, yPosition, 50, 50);  
xPosition +=20;  
//12  
fill (0, 20, 220);  
ellipse(onePosition, twoPosition, 50, 50);  
onePosition +=30;  
//34  
fill (0, 20, 220);  
ellipse(threePosition, fourPosition, 50, 50);  
threePosition +=40;  
//56  
fill (0, 20, 220);  
ellipse(fivePosition, sixPosition, 50, 50);  
fivePosition +=50;  
//78  
fill (0, 20, 220);  
ellipse(sevenPosition, eightPosition, 50, 50);  
sevenPosition +=60;  
//910  
fill (0, 20, 220);  
ellipse(ninePosition, tenPosition, 50, 50);  
ninePosition +=70;  
//1112  
fill (0, 20, 220);  
ellipse(elevenPosition, twelvePosition, 50, 50);  
elevenPosition +=70;  
//1314  
fill (0, 20, 220);  
ellipse(thirteenPosition, fourteenPosition, 50, 50);  
thirteenPosition +=80;  
//1516  
fill (0, 20, 220);  
ellipse(fifteenPosition, sixteenPosition, 50, 50);  
fifteenPosition +=90;  
//1718  
fill (0, 20, 220);  
ellipse(seventeenPosition, eighteenPosition, 50, 50);  
seventeenPosition +=100;  
//1920  
fill (0, 20, 220);  
ellipse(nineteenPosition, tweentyPosition, 50, 50);  
nineteenPosition +=110;  
//2122  
fill (0, 20, 220);  
ellipse(tweentyonePosition, tweentytwoPosition, 50, 50);  
tweentyonePosition +=120;

loop ();  
//interactive spheres  
//sphere1  
translate(150, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 1);  
sphere(30);  
noStroke();  
directionalLight(250, 230, 255, -1, 0, 0);

//sphere 2  
translate(200, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 4);  
sphere(20);  
noStroke();  
directionalLight(250, 230, 255, -1, 0, 0);

//sphere 3  
translate(300, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 4);  
sphere(30);  
noStroke();  
directionalLight(250, 230, 255, -1, 0, 0);

//sphere 4  
translate(400, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 4);  
sphere(40);  
noStroke();  
directionalLight(250, 230, 255, -1, 0, 0);

//sphere 5  
translate(500, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 4);  
sphere(50);  
noStroke();  
directionalLight(250, 230, 255, -1, 0, 0);

//sphere 6  
translate(600, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 4);  
sphere(60);  
noStroke();  
directionalLight(250, 230, 255, -1, 0, 0);

//sphere 7  
translate(700, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 4);  
sphere(70);  
noStroke();  
directionalLight(250, 230, 255, -1, 0, 0);

}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 26, 2021, 7:16am UTC](https://discourse.processing.org/t/p3d-running-extremely-slow/30302/2 "2021-05-26T07:16:22Z")

</div>

> [@123](#):
>
> sphereDetail(mouseX / 1);

much too high

should maximum be around 23 imho

you can say

```auto
float mySphereDetail = map (mouseX, 0,width, 1, 30); 
sphereDetail(mySphereDetail);

```

**Translate**

you have several blocks with translate()

you can isolate them with

```auto
pushMatrix(); 
translate();
...
...

popMatrix();

```

otherwise the translate add up and your stuff ends up much more far to the right than you expect

Chrisir

---

<div class="post-metadata">

**Author:** ![123](https://avatars.discourse-cdn.com/v4/letter/1/7feea3/32.png) [@123](https://discourse.processing.org/u/123)\
**Post date:** [May 26, 2021, 10:10am UTC](https://discourse.processing.org/t/p3d-running-extremely-slow/30302/3 "2021-05-26T10:10:25Z")

</div>

Thanks! I’ll attempt that

I’ve imported sound into the sketch and suddenly it runs smoothly now. Here’s the new code.

import processing.sound.\*;  
SoundFile file;

//SEQUENCES OF MOVING CIRCLES  
//Name variable ‘xPosition’ with the value 0  
float xPosition = 0;  
float yPosition;  
float aPosition = 0;  
float bPosition;  
float onePosition = 0;  
float twoPosition;  
float threePosition = 0;  
float fourPosition;  
float fivePosition = 0;  
float sixPosition;  
float sevenPosition = 0;  
float eightPosition;  
float ninePosition = 0;  
float tenPosition;  
float elevenPosition = 0;  
float twelvePosition;  
float thirteenPosition = 0;  
float fourteenPosition;  
float fifteenPosition = 0;  
float sixteenPosition;  
float seventeenPosition = 0;  
float eighteenPosition;  
float nineteenPosition = 0;  
float tweentyPosition;  
float tweentyonePosition = 0;  
float tweentytwoPosition;

//ANIMATED GIFS  
PImage[] gifone = new PImage[67];  
int gifoneX = -1500, gifoneY = -100;

//SOUND, name variable ‘L’ for duration  
float L;

//MORPH  
//set up 2 arrays to store the vertices of 2 shapes (circle+square)  
ArrayList circle = new ArrayList();  
ArrayList square = new ArrayList();

//set up arraylist for a 3rd set of vertices  
ArrayList morph = new ArrayList();

//the boolean variable will control if we are morphing to a circle or square  
boolean state = false;

//set up lef and right eyes  
PVector leftEye;  
PVector rightEye;

void setup () {  
//set up location for eyes: fraeleft+right eye vector at (45,45) (55,40)  
leftEye = new PVector(width\*.45, height\*.40);  
rightEye = new PVector(width\*.55, height\*.40);

//set up sound  
file = new SoundFile (this, “sound.mp3”);  
file.play(); //play video  
file.amp(1); //set ampitude at 1  
L=file.duration(); //show video duration below  
println (L); // write to the console area  
//other general setup  
fullScreen (P3D);  
frameRate(20);  
smooth();  
noStroke ();

//set up xy coordinator for sequences of moving circles  
bPosition = 100;  
yPosition = 150;  
twoPosition = 200;  
fourPosition = 250;  
sixPosition = 300;  
eightPosition = 350;  
tenPosition = 400;  
twelvePosition = 450;  
fourteenPosition = 500;  
eighteenPosition = 550;  
tweentyPosition = 600;  
tweentytwoPosition = 650;

//load images to create gif  
gifone[0] = loadImage( “0.gif” );  
gifone[1] = loadImage( “1.gif” );  
gifone[2] = loadImage( “2.gif” );  
gifone[3] = loadImage( “3.gif” );  
gifone[4] = loadImage( “4.gif” );  
gifone[5] = loadImage( “5.gif” );  
gifone[6] = loadImage( “6.gif” );  
gifone[7] = loadImage( “7.gif” );  
gifone[8] = loadImage( “8.gif” );  
gifone[9] = loadImage( “9.gif” );  
gifone[10] = loadImage( “10.gif” );  
gifone[11] = loadImage( “11.gif” );  
gifone[12] = loadImage( “12.gif” );  
gifone[13] = loadImage( “13.gif” );  
gifone[14] = loadImage( “14.gif” );  
gifone[15] = loadImage( “15.gif” );  
gifone[16] = loadImage( “16.gif” );  
gifone[17] = loadImage( “17.gif” );  
gifone[18] = loadImage( “18.gif” );  
gifone[19] = loadImage( “19.gif” );  
gifone[20] = loadImage( “20.gif” );  
gifone[21] = loadImage( “21.gif” );  
gifone[22] = loadImage( “22.gif” );  
gifone[23] = loadImage( “23.gif” );  
gifone[24] = loadImage( “24.gif” );  
gifone[25] = loadImage( “25.gif” );  
gifone[26] = loadImage( “26.gif” );  
gifone[27] = loadImage( “27.gif” );  
gifone[28] = loadImage( “28.gif” );  
gifone[29] = loadImage( “29.gif” );  
gifone[30] = loadImage( “30.gif” );  
gifone[31] = loadImage( “31.gif” );  
gifone[32] = loadImage( “32.gif” );  
gifone[33] = loadImage( “33.gif” );  
gifone[34] = loadImage( “34.gif” );  
gifone[35] = loadImage( “35.gif” );  
gifone[36] = loadImage( “36.gif” );  
gifone[37] = loadImage( “37.gif” );  
gifone[38] = loadImage( “38.gif” );  
gifone[39] = loadImage( “39.gif” );  
gifone[40] = loadImage( “40.gif” );  
gifone[41] = loadImage( “41.gif” );  
gifone[42] = loadImage( “42.gif” );  
gifone[43] = loadImage( “43.gif” );  
gifone[44] = loadImage( “44.gif” );  
gifone[45] = loadImage( “45.gif” );  
gifone[46] = loadImage( “46.gif” );  
gifone[47] = loadImage( “47.gif” );  
gifone[48] = loadImage( “48.gif” );  
gifone[49] = loadImage( “49.gif” );  
gifone[50] = loadImage( “50.gif” );  
gifone[51] = loadImage( “51.gif” );  
gifone[52] = loadImage( “52.gif” );  
gifone[53] = loadImage( “53.gif” );  
gifone[54] = loadImage( “54.gif” );  
gifone[55] = loadImage( “55.gif” );  
gifone[56] = loadImage( “56.gif” );  
gifone[57] = loadImage( “57.gif” );  
gifone[58] = loadImage( “58.gif” );  
gifone[59] = loadImage( “59.gif” );  
gifone[60] = loadImage( “60.gif” );  
gifone[61] = loadImage( “61.gif” );  
gifone[62] = loadImage( “62.gif” );  
gifone[63] = loadImage( “63.gif” );  
gifone[64] = loadImage( “64.gif” );  
gifone[65] = loadImage( “65.gif” );  
gifone[66] = loadImage( “66.gif” );

//MORPH  
//// create a circle using vectors pointing from center  
for (int angle = 0; angle \< 360; angle += 9) {  
PVector v = PVector.fromAngle(radians(angle-135));  
v.mult(500); //adjust the size of the morphing shape  
circle.add(v); //add circle  
morph.add(new PVector()); //add morph  
}

// A square is a bunch of vertices along straight lines  
// Top of square  
for (int x = -50; x \< 50; x += 10) {  
square.add(new PVector(x, -50));  
}  
// Right side  
for (int y = -50; y \< 50; y += 10) {  
square.add(new PVector(50, y));  
}  
// Bottom  
for (int x = 50; x \> -50; x -= 10) {  
square.add(new PVector(x, 50));  
}  
// Left side  
for (int y = 50; y \> -50; y -= 10) {  
square.add(new PVector(-50, y));  
}  
}

void draw () {  
background(255); //set white background  
//eyes, move the eyes using cursor  
PVector mouseVector = new PVector(mouseX, mouseY);  
//set the magnitude of this vector to the value of 10 for both eyes  
PVector leftPupil = leftEye.copy().add(mouseVector.copy().sub(leftEye).setMag(10));  
PVector rightPupil = rightEye.copy().add(mouseVector.copy().sub(rightEye).setMag(10));

fill(255);  
stroke(3);  
//adjust left and right eyes  
ellipse(leftEye.x, leftEye.y, 100, 100); //outer shape size  
ellipse(rightEye.x, rightEye.y, 100, 100);

fill(0);  
ellipse(leftPupil.x, leftPupil.y, 20, 20); //pupil size  
ellipse(rightPupil.x, rightPupil.y, 20, 20);

//morph  
float totalDistance = 0;

// Look at each vertex  
for (int i = 0; i \< circle.size(); i++) { //value 1 is set to 0, i is less than circle size, i = i+1  
PVector v1;  
// lerping to the circle and square  
if (state) {  
v1 = circle.get(i);  
} else {  
v1 = square.get(i);  
}  
// Get the vertex we will draw  
PVector v2 = morph.get(i);  
// Lerp to the target  
v2.lerp(v1, 0.1);  
// Check how far we are from target  
totalDistance += PVector.dist(v1, v2);  
}

// If all the vertices are close, switch shape  
if (totalDistance \< 0.1) {  
state = !state;  
}

// Draw relative to center  
translate(width/2, height/2);  
strokeWeight(2);  
// Draw a polygon that makes up all the vertices  
beginShape();  
noFill();  
stroke(2);  
for (PVector v : morph) {  
vertex(v.x, v.y);  
}  
endShape(CLOSE);

noStroke ();  
//ANIMATED GIF, 67 frames will be displayed for the gif  
image( gifone[frameCount%67], gifoneX, gifoneY);  
gifoneX = gifoneX + 4;  
if ( gifoneX \> width + 10) {  
gifoneX = -250; //I want slow down the gif to make the gif disappear at 60 sec  
}

//sequences of circles  
//ab  
fill (0, 20, 220);  
ellipse(aPosition, bPosition, 50, 50);  
aPosition +=1;  
//xy  
fill (0, 20, 220);  
ellipse(xPosition, yPosition, 50, 50);  
xPosition +=2;  
//12  
fill (0, 20, 220);  
ellipse(onePosition, twoPosition, 50, 50);  
onePosition +=3;  
//34  
fill (0, 20, 220);  
ellipse(threePosition, fourPosition, 50, 50);  
threePosition +=4;  
//56  
fill (0, 20, 220);  
ellipse(fivePosition, sixPosition, 50, 50);  
fivePosition +=5;  
//78  
fill (0, 20, 220);  
ellipse(sevenPosition, eightPosition, 50, 50);  
sevenPosition +=6;  
//910  
fill (0, 20, 220);  
ellipse(ninePosition, tenPosition, 50, 50);  
ninePosition +=7;  
//1112  
fill (0, 20, 220);  
ellipse(elevenPosition, twelvePosition, 50, 50);  
elevenPosition +=8;  
//1314  
fill (0, 20, 220);  
ellipse(thirteenPosition, fourteenPosition, 50, 50);  
thirteenPosition +=9;  
//1516  
fill (0, 20, 220);  
ellipse(fifteenPosition, sixteenPosition, 50, 50);  
fifteenPosition +=10;  
//1718  
fill (0, 20, 220);  
ellipse(seventeenPosition, eighteenPosition, 50, 50);  
seventeenPosition +=11;  
//1920  
fill (0, 20, 220);  
ellipse(nineteenPosition, tweentyPosition, 50, 50);  
nineteenPosition +=12;  
//2122  
fill (0, 20, 220);  
ellipse(tweentyonePosition, tweentytwoPosition, 50, 50);  
tweentyonePosition += 13;

//interactive spheres  
//sphere1  
translate(0, 950, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);  
sphereDetail(mouseX / 10); //for rendering the sphere  
sphere(30);  
noStroke();  
directionalLight(250, 230, 500, -1, 100, 0);

//sphere 2  
translate(200, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);

sphere(20);  
noStroke();  
directionalLight(250, 230, 500, -1, 100, 0);

//sphere 3  
translate(300, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);

sphere(30);  
noStroke();  
directionalLight(250, 230, 500, -1, 100, 0);

//sphere 4  
translate(400, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);

sphere(40);  
noStroke();  
directionalLight(250, 230, 500, -1, 100, 0);

//sphere 5  
translate(500, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);

sphere(50);  
noStroke();  
directionalLight(250, 230, 500, -1, 100, 0);

//sphere 6  
translate(600, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);

sphere(60);  
noStroke();  
directionalLight(250, 230, 500, -1, 100, 0);

//sphere 7  
translate(700, height/2, 0);  
rotateX(mouseY \* 0.05);  
rotateY(mouseX \* 0.05);  
fill(mouseX \* 2, 0, 160);

sphere(70);  
noStroke();  
directionalLight(250, 230, 500, -1, 100, 0);  
}

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 26, 2021, 3:58pm UTC](https://discourse.processing.org/t/p3d-running-extremely-slow/30302/4 "2021-05-26T15:58:40Z")

</div>

Hello,

Please format your code as per:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

There are plenty of examples of what it should look like in the forum.

`:)`
