[SOLVED] Keyboard controlled object (move and rotate)

edited June 2017 in Questions about Code

Hi everyone, first post here. Just started learning processing and i'd like to ask you something about the code i'm writing. I've spent quite sometime figuring out how to move the rectangle across the screen using WASD and i think i've solved it pretty flawless, now i'm stuck trying to make it rotate left and right by using Q-E. The problem is that i'm actually drawing another rectangle when pressing E instead of modifying the parameter of the existing one, and i can't fathom how to do it. Also i don't know if it's possible to make it rotate left using rotate(radians(frameCount));

Here's the code i wrote:

object myObject;

boolean [] keys = new boolean[128];

void setup(){
  size(600, 600); 
  smooth();
  myObject = new object();

}

void draw(){
  background(0);
  myObject.run();  

}

void keyPressed(){
  keys[key] = true;
  }

void keyReleased(){
    keys[key] = false;
  }

and that's the object class with the problematic function

class object {

  float x = width/2;
  float y = height/2;
  int speed = 5;

  object() {
  }

  void run() {    //generic function container
    display();
    move();
  }


  void display() {
    x=constrain(x, 25, width-25);
    y=constrain(y, 25, height-25);
    rectMode(CENTER);
    rect(x, y, 50, 50);
  }


  void move() {
    if (keys['a']) //move left 
      x= x-speed;
    if (keys['d']) //move right
      x= x+speed;
    if (keys['w']) //move up
      y= y-speed;
    if (keys['s']) //move down
      y= y+speed;
    if (keys['e']) //rotate right (calls for another function)
      rotateR();
    if (keys['q']) //rotate left (unwritten code)!!!
      rotateL();
  }

  void rotateR() {   //PROBLEMATIC FUNCTION  HOW DO I FIX IT?
    pushMatrix();
    translate(x, y);
    rotate(radians(frameCount));
    rect(0,0,50,50);
    popMatrix();
  }

  void rotateL(){   // TODO

  }

}

any help would be appreciated. ty

Answers

  • Answer ✓

    First note: Change the name of your class. Object is very generic. Also there is a class in Java called Object and this class is a very important one. All instances from any classes inherits from this class directly or indirectly. When you create a class called "object", you are just looking for trouble :-B Easy fix, name it differently like ShipObject or MonsterObj. Btw, notice it is a common (and recommended) practice to write class's names using the TitleSchema, first letter always on uppercase. For variables and functions, first letter is always in lowercase.

    Related to your rotation question, I don't think frameCount should be use here at all. Instead, create a variable called rotation inside your class and modify this variable based on Q/E inputs. A brief layout below. It might need minor tweaking but it shows the concept.

    Kf

    class object {
    
      float x; // = width/2;   <---Define this inside your constructor::better practice
      float y; // = height/2;
      float rotation;
    
      .... OTHER CODE
    
    void rotateR() {  
        rotation+=0.02;   //Values in radians
      }
    
    void rotateL() {  
        rotation-=0.02;   //Values in radians
      }
    
    void display() {
        x=constrain(x, 25, width-25);
        y=constrain(y, 25, height-25);
        pushMatrix();
        translate(x, y);
        rotate(radians(frameCount));
        rectMode(CENTER);
        rect(0,0, 50, 50);   // CHANGED
        popMatrix();
      }
    
  • edited June 2017

    ty very much kfrajer, that's what i was looking for, i rewrote the code to use PVectors instead of a bunch of floats, by adding a rotation variable i don't even need rotateR() and rotateL() anymore. Also thanks for the "style" advices, better be precise from the start to avoid developing bad habits.

    here's the updated code for future references:

    Ship myShip;
    
    boolean [] keys = new boolean[128];
    
    void setup() {
      size(600, 600); 
      smooth();
      myShip = new Ship();
    }
    
    void draw() {
      background(0);
      myShip.run();
    }
    
    void keyPressed() {
      keys[key] = true;
    }
    
    void keyReleased() {
      keys[key] = false;
    }
    

    and the Ship class

    class Ship {  
      PVector pos, vel;
      float rotation;
    
    
      Ship() {
        pos = new PVector(width/2, height/2);
        vel = new PVector(5, 5);
        rotation = 0;
      }
    
      void run() {    //generic function container
        display();
        move();
      }
    
    
      void display() {
        pos.x=constrain(pos.x, 25, width-25);
        pos.y=constrain(pos.y, 25, height-25);
        pushMatrix();
        translate(pos.x, pos.y);
        rotate(radians(rotation));
        rectMode(CENTER);
        rect(0, 0, 50, 50);
        popMatrix();
      }
    
    
      void move() {
        if (keys['a']) //move left 
          pos.x -= vel.x;
        if (keys['d']) //move right
          pos.x += vel.x;
        if (keys['w']) //move up
          pos.y -= vel.y;
        if (keys['s']) //move down
          pos.y += vel.y;
        if (keys['e']) //rotate right 
          rotation ++;
        if (keys['q']) //rotate left 
          rotation --;
      }
    }
    

    now onto the next step, i want it to rotate left and right and move forward based on it's facing direction. Probably i'll need to write another help me post in a couple of days :D

Sign In or Register to comment.