I’m learning about metods of moving objects in proccesing and I tried to make object to act like a tank. The problem is, in order to do so, i want it to rotate around one caterpillar while the other is working and vice versa. I know that the key recognition isn’t made in the best possible way. I was trying to change the origin by translate function but it moves my tank left or right and the center point is in the same position. I can’t find answer anywhere so here i am. I hope that my intention is clear enought, here is my code:
PVector pos = new PVector(0,0);
float rotateAngle = 180;
int[] colorOfCaterpillarOFF = {188, 77, 82};
int[] colorOfCaterpillarON = {226, 118, 122};
int[] colorOfCaterpillar_L = {188, 77, 82};
int[] colorOfCaterpillar_R = {188, 77, 82};
void setup(){
size(800,800);
rectMode(CENTER);
}
void draw(){
background(255,255,255);
translate(width/2,height/2);
stroke(125, 132, 142);
line(0,-400,0, 400);
line(-400,0,400,0);
noStroke(); // cross lines in background
translate(pos.x,pos.y);
keyRecognition();
rotate(radians(rotateAngle));
drawTank();
}
void keyRecognition(){
if(keyPressed && key=='d'){
rotateAngle++;
colorOfCaterpillar_R = colorOfCaterpillarON; // turning right
translate(-34,0);
} else if(keyPressed && key=='a'){
rotateAngle--;
colorOfCaterpillar_L = colorOfCaterpillarON; // turning left
translate(34,0);
}else if(keyPressed && key=='w'){
pos.add(PVector.fromAngle(radians(rotateAngle+90)));
colorOfCaterpillar_R = colorOfCaterpillarON;
colorOfCaterpillar_L = colorOfCaterpillarON; //going straight
}else if(keyPressed && key=='s'){
pos.add(PVector.fromAngle(radians(rotateAngle+90)).mult(-1));
colorOfCaterpillar_R = colorOfCaterpillarON;
colorOfCaterpillar_L = colorOfCaterpillarON; // going backwards
} else {
colorOfCaterpillar_R = colorOfCaterpillarOFF;
colorOfCaterpillar_L = colorOfCaterpillarOFF; // standing still
}
}
void drawTank(){
fill(27, 153, 73); // color of tanks body
rect(0,0,50,80, 5,5,5,5); // tanks body
fill(colorOfCaterpillar_L[0], colorOfCaterpillar_L[1], colorOfCaterpillar_L[2]);
rect(-34,0,15,75,5,5,5,5);// caterpillars of the tank // left
fill(colorOfCaterpillar_R[0], colorOfCaterpillar_R[1], colorOfCaterpillar_R[2]);
rect(34,0,15,75,5,5,5,5); // right caterpillar
fill(72, 122, 15);
rect(0,20,30,20,5,5,5,5);
}