Moving on an angle
in
Programming Questions
•
10 months ago
I'm trying to make a character controlled by arrow keys, and I want it to move in any direction. when I hit left and right, i would like it to turn, and up and down should be forwards and backwards. I have something that kinda works, but it only rotates around the screen corner, and is technically only sliding on the x axis. Can anybody help?
- int x;
- int y;
- float ang;
- void setup() {
- size(500, 500);
- smooth();
- }
- void draw() {
- background(0);
- pushMatrix();
- rotate(ang);
- translate(x, y);
- rect(0, -5, 40, 10);
- popMatrix();
- if (keyPressed) {
- switch(keyCode) {
- case UP:
- x+=5;
- break;
- case DOWN:
- x-=5;
- break;
- case LEFT:
- ang -= 0.01;
- break;
- case RIGHT:
- ang += 0.01;
- break;
- }
- }
- }
1