WASD Control + Rotating Object
in
Programming Questions
•
1 year ago
Hey guys,
I have a problem and after lots of hours testing and trying I have no idea what else I can do.
So I'm begging you to help me. I also have to say, I'm very newbie with processing,
we did a bit in school :S
So I want to do a 2D Shooter (in bird's eye view), and the character should be movable with "wasd".
This one is not the problem, but i also want the character rotating/look in direction of the cursor.
Both things I can do easily, but I don't get how I achieve to combine those two things.
So I'm begging you to help me. I also have to say, I'm very newbie with processing,
we did a bit in school :S
So I want to do a 2D Shooter (in bird's eye view), and the character should be movable with "wasd".
This one is not the problem, but i also want the character rotating/look in direction of the cursor.
Both things I can do easily, but I don't get how I achieve to combine those two things.
- //Moving an object
- final static int NORTH = 1;
- final static int EAST = 2;
- final static int SOUTH = 4;
- final static int WEST = 8;
- int result;
- float x,y;
- void setup() {
- size(512,400);
- frameRate(60);
- result = 0;
- x = width/2;
- y = height/2;
- }
- void draw() {
- background(0);
- switch(result) {
- case NORTH: y--; break;
- case EAST: x++; break;
- case SOUTH: y++; break;
- case WEST: x--; break;
- case NORTH|EAST: y--; x++; break;
- case NORTH|WEST: y--; x--; break;
- case SOUTH|EAST: y++; x++; break;
- case SOUTH|WEST: y++; x--; break;
- }
- fill(255);
- rect(x,y,50,50);
- }
- void keyPressed(){
- switch(key) {
- case('w'):case('W'):result |=NORTH;break;
- case('d'):case('D'):result |=EAST;break;
- case('s'):case('S'):result |=SOUTH;break;
- case('a'):case('A'):result |=WEST;break;
- }
- }
- void keyReleased(){
- switch(key) {
- case('w'):case('W'):result ^=NORTH;break;
- case('d'):case('D'):result ^=EAST;break;
- case('s'):case('S'):result ^=SOUTH;break;
- case('a'):case('A'):result ^=WEST;break;
- }
- }
- //Rotating an object
- float angle, oldAngle = 0;
- void setup(){
- size(1200,900);
- smooth();
- background (30,50,110);
- }
- void draw(){
- angle = atan2(mouseY-height/2,mouseX-width/2)+oldAngle ;
- translate(width/2,height/2);
- rotate(angle);
- rectMode(CENTER);
- background (0);
- rect (0,0,50,50);
- }
1