Hello,
Im new to processing and coding in general. Im trying to code a “tank” move around (seen from above). I want to have a tank rotate left when pressing keyboard “A” and right when pressing “D”. And I want it to drive forwards and backwards when pressing “W” and “S”.
I have a hard time finding any code from somebody else who have done the same thing. Its really annoying because it feels like it would be a really simple thing but I admit I really need some help. I have tried multiple things using vectors, trigonometry and 2D transformation.
I should also probably mention that my intention is to have a “tank-like” robot send its driving distance to processing via serial. And plot that information in that top view so that I can kind of see a navigation map of the robot.
I would really appriciate some help.
Here is the code that I have now. (Dont mind the created lines, its just so that I can see where the translate function is atm)
float x,y;
float angle;
void setup() {
size(600, 600);
x = 0;
y = 0;
angle = 0;
noFill();
stroke(0);
strokeWeight(1);
}
void draw() {
background(255);
translate(width/2,height/2);
line(0,0,0,-20);
line(-10,0,10,0);
//pushMatrix();
//popMatrix();
translate(x,y);
rotate(radians(angle));
line(0,0,0,-20);
line(-10,0,10,0);
translate(x,y);
line(0,0,0,-20);
line(-10,0,10,0);
point(0,0);
rectMode(CENTER);
rect(0,0, 20, 40);
update();
}
void update() {
if(keyPressed) {
print(angle + " ");
println(key);
if(key == 'w' || key =='W') {
y = y - 1;
}
if(key == 'a' || key =='A') {
angle = angle - 1;
}
}
}