#movement script wasd

23 messages · Page 1 of 1 (latest)

cyan ravine
#

how do you guys suggest i implement WASD movement in my code? i want to add more keys in the future, but it seems really annoying having to make a boolean for each key, does anyone have a better solution? (i need to know when a key is being held down and not being held down)

final path
#

there are many solutions to this problem. You could also create an array that can contain all the pressed keys. This would scale to as many keys as you want, but I am not sure I find this more readable.

#

the boolean variables have a simplicity that is easy to write and understand in my opinion.

lusty willow
#

An object with all key booleans?

fringe zephyr
#

KeyIsDown(keycode)

final path
#

KeyIsDown() as you describe it does not seem to be a part of Processing

#

p5 python and p5.js have it, though.

final path
#

You keep bringing up P5.JS / JavaScript solutions, but this is a Processing / Java question.

outer geode
#

if (keyPressed && key == 'W') {} etc should work

#

If you want more than one direction at a time, nest the key checks inside the if (keyPressed) {} statement

#

Actually, it might work fine doing them separately, but it's more efficient to check once

#

Or better yet, create a method for handling keys and call it during draw() when keyPressed is true

cyan ravine
#

this is what i ended up with

float clamp(float val, float min, float max) {
  if (val<min) return min;
  if (val>max) return max;
  return val;
}

class Player {

  PVector pos;
  PVector dir = new PVector(0, 0);
  color c;
  float r;
  float speed;
  boolean up, down, left, right, sneak;

  Player(PVector _pos, float _r, color _c, float s) {
    pos=_pos;
    r=_r;
    c=_c;
    speed=s;
  }

  void show() {
    noStroke();
    fill(c);
    circle(pos.x, pos.y, r*2);
  }

  void update() {

    dir.x = left ? -1 : 0;
    dir.x = right ? 1 : dir.x;
    dir.y = down ? 1 : 0;
    dir.y = up ? -1 : dir.y;


    dir.normalize();

    //pos = pos.add(dir);
    pos = pos.add(dir.mult(!sneak ? speed : speed/2));

    //pos.x = (width+pos.x)%width;
    //pos.y = (height+pos.y)%height;

    pos.x = clamp(pos.x, 0+r, width-r);
    pos.y = clamp(pos.y, 0+r, height-r);
  }

  void keyPressed() {

    //println("pressed");

    if (keyCode == SHIFT) sneak=true;

    switch (key) {
    case 'w' :
    case 'W' :
      up=true;
      break;
    case 's' :
    case 'S' :
      down=true;
      break;
    case 'a' :
    case 'A' :
      left=true;
      break;
    case 'd':
    case 'D':
      right=true;
      break;
    }
  }

  void keyReleased() {

    if (keyCode == SHIFT) sneak=false;

    switch (key) {
    case 'w' :
    case 'W' :
      up=false;
      break;
    case 's' :
    case 'S' :
      down=false;
      break;
    case 'a' :
    case 'A' :
      left=false;
      break;
    case 'd':
    case 'D':
      right=false;
      break;
    }
  }
}

is this bad coding practices? if so how do you guys suggest doing it better

outer geode
# cyan ravine this is what i ended up with ```java float clamp(float val, float min, float ma...

Processing already has a "clamp" function: constrain()

Another way to handle case-insensitive keys would be to convert the character to uppercase using key = Character.toUpperCase(key); and then you would only need to worry about uppercase letters, reducing the number of cases here. It's not that big of a deal, but it might make things cleaner or take less space.

Looks pretty good tho

cyan ravine
#

thanks constrain will come in handy :D

final path
# outer geode `if (keyPressed && key == 'W') {}` etc should work

Sadly, it is not ideal. key always hold the key that was most recently pressed or released. So you can break this if statement pretty easily:

  • Press ANY key you want and hold it
  • Press and release the W key

-> You are no longer holding W but that statement will still evaluate to true because there is still a key pressed (that first "any" key), and W was the key most recently released.

final path
fringe zephyr
#

Me neither

outer geode
#

Silly me

#

Well that would be the issue if they want more than 2 directions at once. Not to mention the problem you brought up too

outer geode
cyan ravine