We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm having a problem with my sketch running slow also. I'm making a 3D game with a lot of different objects in the game like enemies, obstacles and things like that. The issue is, once I start adding more and more things to the game, it runs really, really slow. Is there a way I can make it run faster. Here's my code:
import peasy.*;
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
int button1 = 1;
int button2 = 1;
int button3 = 1;
int button4 = 1;
int linPot = 0;
int liteSensor, tempSensor, mic;
int jX, jY;
int aX, aY, aZ;
int jButton = 1;
boolean test = false; // new
PeasyCam cam;
int x=75,y=0,z=175;
int xMov = 0;
int yMov = 0;
int zMov = 0;
Tent tent1;
Tent tent2;
Tent tent3;
Tent tent4;
Tent tent5;
Tent tent6;
Tent tent7;
Tent tent8;
Tent tent9;
Tent tent10;
Tent tent11;
Tent tent12;
Tent tent13;
Tent tent14;
Tent tent15;
Tent tent16;
Tent tent17;
ArrayList<Tree> aForest;
int cY = 0, cZ = 0;
float ccy=0,ccz=0;
float xAngle = 0;
void setup(){
//fullScreen(P3D);
size(683,384,P3D);
frameRate(120);
if (test) {
frameRate(2); // slow everything down
} else {
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 300);
}
//cam.setSuppressRollRotationMode();
aForest = new ArrayList<Tree>(); /*
for (int z = -100; z < 300; z+=10){
for (int x = 175; x < 195; x+=10){
Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
aForest.add(tree);
}
}
for (int z = 55; z < 75; z+=10){
for (int x = -20; x < 130; x+=10){
Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
aForest.add(tree);
}
} //THIS AREA MAKES IT RUN SLOW
for (int z = -100; z < -50; z+=10){
for (int x = -20; x < 175; x+=10){
Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
aForest.add(tree);
}
}
for (int z = -100; z < 75; z+=10){
for (int x = -50; x < -20; x+=10){
Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
aForest.add(tree);
}
} */
tent1 = new Tent(#f4a460,130,0,100,0);
tent2 = new Tent(#f4a460,100,0,100,0);
tent3 = new Tent(#f4a460,70,0,100,0);
tent4 = new Tent(#f4a460,40,0,100,0);
tent5 = new Tent(#f4a460,10,0,100,0);
tent6 = new Tent(#f4a460,130,0,140,0);
tent7 = new Tent(#f4a460,100,0,140,0);
tent8 = new Tent(#f4a460,70,0,140,0);
tent9 = new Tent(#f4a460,40,0,140,0);
tent10 = new Tent(#f4a460,10,0,140,0);
tent11 = new Tent(#f4a460,150,0,200,1);
tent12 = new Tent(#f4a460,150,0,230,1);
tent13 = new Tent(#f4a460,90,0,240,0);
tent14 = new Tent(#f4a460,60,0,240,0);
tent15 = new Tent(#f4a460,120,0,300,0);
tent16 = new Tent(#f4a460,90,0,300,0);
tent17 = new Tent(#f4a460,60,0,300,0);
}
void draw(){
background(105);
println(frameRate);
cc();
//println(cam.getPosition());
//directionalLight(255,255,255,0,1,0);
translate(width/2,height+1,0);
fill(0,255,0); //(height/2)/tan(PI/6)
stroke(0); //translate(x,y,z);
box(width,-1,width); //box(width,height,depth)
fill(255,0,0);
tent1.display();
tent2.display();
tent3.display();
tent4.display();
tent5.display();
tent6.display();
tent7.display();
tent8.display();
tent9.display();
tent10.display();
tent11.display();
tent12.display();
tent13.display();
tent14.display();
tent15.display();
tent16.display();
tent17.display();
for(Tree singleTree: aForest){ //Check: <a href="https://processing.org/reference/for.html" target="_blank" rel="nofollow">https://processing.org/reference/for.html</a>
singleTree.display();
}
translate(x,y-2.5,z);
// rotateY(turn);
fill(0);
stroke(255,0,0);
sphere(5);
x += xMov;
y += yMov;
z += zMov;
println(x,",",y,",",z);
}
class Tent{
color c;
float tX;
float tY;
float tZ;
float d;
Tent(color tempC, float tempX, float tempY, float tempZ, float tempD){
d = tempD;
c = tempC;
tX = tempX;
tY = tempY;
tZ = tempZ;
}
void display(){
if(d == 0){
stroke(0);
fill(c);
beginShape();
vertex(tX-10,tY,tZ-10); //left rear
vertex(tX-10,tY,tZ+10); //left front
vertex(tX+10,tY,tZ+10); //right front
vertex(tX+10,tY,tZ-10); //right rear
endShape(CLOSE);
beginShape();
vertex(tX,tY-10,tZ-10); //top rear
vertex(tX,tY-10,tZ+10); //top front
vertex(tX-10,tY,tZ+10); //left front
vertex(tX-10,tY,tZ-10); //left rear
endShape(CLOSE);
beginShape();
vertex(tX,tY-10,tZ-10); //top rear
vertex(tX,tY-10,tZ+10); //top front
vertex(tX+10,tY,tZ+10); //right front
vertex(tX+10,tY,tZ-10); //right rear
endShape(CLOSE);
beginShape();
vertex(tX-10,tY,tZ-10); //left rear
vertex(tX+10,tY,tZ-10); //right rear
vertex(tX,tY-10,tZ-10); //top rear
endShape(CLOSE);
beginShape();
vertex(tX-10,tY,tZ+10); //left front
vertex(tX+10,tY,tZ+10); //right front
vertex(tX,tY-10,tZ+10); //top front
endShape(CLOSE);
}else if(d == 1){
stroke(0);
fill(c);
beginShape();
vertex(tX-10,tY,tZ-10); //left rear
vertex(tX-10,tY,tZ+10); //left front
vertex(tX+10,tY,tZ+10); //right front
vertex(tX+10,tY,tZ-10); //right rear
endShape(CLOSE);
beginShape();
vertex(tX-10,tY-10,tZ); //top left
vertex(tX+10,tY-10,tZ); //top right
vertex(tX+10,tY,tZ-10); //right rear
vertex(tX-10,tY,tZ-10); //left rear
endShape(CLOSE);
beginShape();
vertex(tX-10,tY-10,tZ); //top left
vertex(tX+10,tY-10,tZ); //top right
vertex(tX+10,tY,tZ+10); //right front
vertex(tX-10,tY,tZ+10); //left front
endShape(CLOSE);
beginShape();
vertex(tX+10,tY,tZ+10); //right front
vertex(tX+10,tY,tZ-10); //right rear
vertex(tX+10,tY-10,tZ); //top right
endShape(CLOSE);
beginShape();
vertex(tX-10,tY,tZ+10); //left front
vertex(tX-10,tY,tZ-10); //left rear
vertex(tX-10,tY-10,tZ); //top left
endShape(CLOSE);
}else if(d == 2){
stroke(0);
fill(c);
beginShape();
vertex(tX-10,tY,tZ); //right
vertex(tX,tY,tZ+10); //front
vertex(tX+10,tY,tZ); //left
vertex(tX,tY,tZ-10); //rear
endShape(CLOSE);
beginShape();
vertex(tX-10,tY,tZ); //right
vertex(tX,tY,tZ-10); //rear
vertex(tX-5,tY-10,tZ-5); //right rear top
endShape(CLOSE);
beginShape();
vertex(tX,tY,tZ+10); //front
vertex(tX+10,tY,tZ); //left
vertex(tX+5,tY-10,tZ+5); //left front top
endShape(CLOSE);
beginShape();
vertex(tX-10,tY,tZ); //right
vertex(tX,tY,tZ+10); //front
vertex(tX+5,tY-10,tZ+5); //left front top
vertex(tX-5,tY-10,tZ-5); //right rear top
endShape(CLOSE);
beginShape();
vertex(tX+10,tY,tZ); //left
vertex(tX,tY,tZ-10); //rear
vertex(tX-5,tY-10,tZ-5); //right rear top
vertex(tX+5,tY-10,tZ+5); //left front top
endShape(CLOSE);
}
}
}
class Tree {
float h;
float tS;
float lS;
color lC;
color tC;
float treeX,treeY,treeZ;
Tree (float trunkHeight, float trunkSize, float leafSize, color leafColor, color trunkColor, int treex, int treey, int treez){
h = trunkHeight;
tS = trunkSize;
lS = leafSize;
lC = leafColor;
tC = trunkColor;
treeX = treex;
treeY = treey;
treeZ = treez;
}
void display(){
fill(tC);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
fill(lC);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
}
}
void keyPressed() {
if ((key == 'w')) {
yMov = -1;
} else if ((key == 'a')) {
xMov = -1; //WASD CONTROLS
} else if ((key == 'd')) {
xMov = 1;
} else if ((key == 's')) {
yMov = 1;
} else if((key == 'e')){
zMov = -1;
}else if((key == 'c')){
zMov = 1;
}
}
void keyReleased() {
if ((key == 'w') || (key == 's')) {
yMov = 0; //WASD CONTROLS
} else if ((key == 'a') || (key == 'd')) {
xMov = 0;
}else if((key == 'e' ) || (key == 'c')){
zMov = 0;
}
}
void cc() {
String val = getData();
if (val != null) {
parseData(val);
}
if (jX >= 15) {
x-=1;
} else if (jX <= -15) {
x+=1;
}
if (jY >= 15) {
z += 1;
} else if (jY <= -15) {
z -= 1;
}
cY = 21;
cam = new PeasyCam(this, (width/2)+x, (height-50)+y, z+15, 50);
// println(cX,cY,cZ);
ccy = map(cY, -100, 100, -3, 3);
cam.setRotations(ccy, 0, 0);
// println(cX,cY,cZ);
}
void parseData(String val) {
String[] data = split(val, ",");
if (data == null || data.length != 14) {
} else {
button1 = int(data[0]);
button2 = int(data[1]);
button3 = int(data[2]);
button4 = int(data[3]);
linPot = int(data[4]);
liteSensor = int(data[5]);
tempSensor = int(data[6]);
mic = int(data[7]);
jButton = int(data[8]);
jX = int(data[9]);
jY = int(data[10]);
aX = int(data[11]);
aY = int(data[12]);
aZ = int(data[13]);
}
}
String getData() {
if (test) {
return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
} else if ( myPort.available() > 0) {
val = myPort.readStringUntil('\n');
return val;
} else {
return null;
}
}
As you can see, there is a lot of code here and i just started, if I note out the area shown, it runs fine, as is, it runs faster, but then its missing a large chunk of the game. There is still more that needs to be added so if there is a way I can get it to run faster, that would be greatly appreciated.
Answers
That's a ton of code to ask us to debug for you. The best thing you can do is profile your code: What takes the most time per frame? What takes the most time over the course of your game? You can't really talk about optimizations if you haven't profiled yet.
But speaking generally, computers don't have infinite power. You can only do a certain number of calculations per second. So you can only handle a certain number of items in your game. The key will be decreasing that number.
There are a ton of approaches to decreasing the number of items in your game. Try splitting your game up into regions and only dealing with items in the current region, for example.
use ctrl-t to get better indents
why do you display a huge box? line 113
you can perform tests in that you comment out the tents or trees to see what really makes it slow
use sphereDetail to make sphere faster
the main point I see is that you can define the shapes tree and tent in setup and just call them in the class (using translate)
don't use println
don't load anything outside setup()
processing / java can only do so much. for real games turn to a game engine such as unity or unreal
draw() calls cc(), 60 times a second
cc() does
a new camera 60 times a second? initialise it in setup.
read this:
https://forum.processing.org/two/discussion/8082/from-several-variables-to-arrays
i second this. you have one tent shape, so define it once. the trees are random so have one per instance, but create it in the constructor.
Well spotted
399 to 404 belong in setup