Diagonal Movement Library?

edited June 2018 in How To...

Hey, I already know people have made solutions to this problem but I was wondering if anyone knew a Library for diagonal movement, Ideally can detect multiple keys pressed at once but I'll take anything you got ;)

Another quick question, I know that Processing/Java uses key/keyCode which is a primitive variable and why only one value can be detected at once but why do we stick with that? Is there a reason they haven't changed it because native diagonal movement/multiple keyPress detection has been a fair majority community's wishlist for quite a while now. Thanks for the help! :)

Tagged:

Answers

  • edited May 2018

    One way I've done this in the past is to keep track of all the keys pressed in an array. Currently down keys can be checked for being pressed with keys[' '] and fresh key presses can be detected with keyHit(' '). I do some math in setKey that makes upper and lower case interchangeable as well.

    boolean [] keys = new boolean[256];
    boolean [] lastKeys = new boolean[256];
    
    PVector loc, vel;
    
    float speedLimit = 6, drag = 0.98, acceleration = 0.2, jumpDistance = 40;
    
    void setup() {
      size(600, 600);
      loc = new PVector(width/2, height/2);
      vel = new PVector(0, 0);
      noStroke();
      rectMode(CENTER);
      strokeWeight(5);
    }
    
    void draw() {
      clear();
    
      if (keys['a']) vel.x-=acceleration;
      if (keys['d']) vel.x+=acceleration;
      if (keys['w']) vel.y-=acceleration;
      if (keys['s']) vel.y+=acceleration;
    
      loc.add(vel.copy());
      vel.limit(speedLimit);
      vel.mult(drag);
    
      if (keyHit(' ')) {
        PVector oldLoc = loc.copy();
        loc.add(PVector.fromAngle(vel.heading()).mult(jumpDistance));
        stroke(255);
        line(oldLoc.x, oldLoc.y, loc.x, loc.y);
        noStroke();
      }
    
      loc.x = constrain(loc.x, 0, width);
      loc.y = constrain(loc.y, 0, height);
    
      pushMatrix();
      translate(loc.x, loc.y);
      rotate(vel.heading());
      rect(0, 0, 10, 5);
    
      popMatrix();
      lastKeys = keys.clone();
    }
    
    boolean keyHit(int c) {
      return (keys[c] && !lastKeys[c]);
    }
    
    char keyHit= ' ';
    void setKey(boolean state) {
      int rawKey = key;
      if (rawKey < 256) {
        if ((rawKey>64)&&(rawKey<91)) rawKey+=32;
        if ((state) && (!lastKeys[rawKey])) {
          keyHit = (char) (rawKey);
        }
        keys[rawKey] = state;
      }
    }
    
    void keyPressed() { 
      setKey(true);
    }
    
    void keyReleased() { 
      setKey(false);
    }
    
  • Answer ✓

    I don't think it's complex enough to be worthy a library. But maybe there's one.

    Here is my simplified version of the code above:

    boolean [] keys = new boolean[256];
    boolean [] lastKeys = new boolean[256];
    
    PVector loc;
    
    char keyHit= ' ';
    
    void setup() {
      size(600, 600);
    
      loc = new PVector(width/2, height/2);
      noStroke();
      rectMode(CENTER);
      strokeWeight(5);
    }
    
    void draw() {
      clear();
    
      if (keys['a']) loc.x-=1;
      if (keys['d']) loc.x+=1;
      if (keys['w']) loc.y-=1;
      if (keys['s']) loc.y+=1;
    
      loc.x = constrain(loc.x, 0, width);
      loc.y = constrain(loc.y, 0, height);
    
      rect(loc.x, loc.y, 10, 10);
    
      lastKeys = keys.clone();
    }
    
    //----------------------------------------
    
    void keyPressed() { 
      setKey(true);
    }
    
    void keyReleased() { 
      setKey(false);
    }
    
    void setKey(boolean state) {
      int rawKey = key;
      if (rawKey < 256) {
        if ((rawKey>64)&&(rawKey<91))
          rawKey+=32;
        if ((state) && (!lastKeys[rawKey])) {
          keyHit = (char) (rawKey);
        }
        keys[rawKey] = state;
      }
    }
    //
    
  • Another option: you can also implement a list of active keys (as in the solutions above) using a HashMap rather than an array. It may not make much difference for basic setups, but it has one advantage that it isn't constrained to 256 values -- it tracks any keycode no matter what it is and puts it in the map.

    https://forum.processing.org/two/discussion/comment/122623/#Comment_122623

Sign In or Register to comment.