Loading...
Logo
Processing Forum
So I'm trying to make a game in Processing, and i've got this source here:
Copy code
  1. PlayerRect mov = new PlayerRect(new PVector(40, 94), 5, 5, 0, 0, 255);
    final int windowX = 800;
    final int windowY = 600;
    void setup() { size(windowX,windowY); }
    void draw() {
      background(150);
      mov.update();
      mov.render();
      mov.moveself();
    }
  2. class Rectangle {
      PVector pos;

      int h;
      int w;

      int r;
      int g;
      int b;

      Rectangle (PVector inpos, int inw, int inh, int inr, int ing, int inb) { //define with a vector
        pos = inpos;
        h = inh;
        w = inw;
        r = inr;
        g = ing;
        b = inb;
      }

      void render () {
        fill(r, g, b);
        noStroke();
        rect(pos.x, pos.y, w, h);
      }
      float getCX() { //get center-x
        return pos.x-(w/2);
      }
      float getCY() { //get center-y
        return pos.y-(h/2);
      }
    }

    class MotileRect extends Rectangle {
      PVector speed; //in pixels per 1/10th second.
      Trigger timer;
      MotileRect(PVector inpos, int inw, int inh, int inr, int ing, int inb, PVector inspd) {
        super(inpos, inw, inh, inr, ing, inb);
        speed = inspd;
        timer = new Trigger(10);
      }
      void update() {
        while (timer.fires ()) {
          pos.add(speed);
        }
      }
    }

    class PlayerRect extends MotileRect {
      PlayerRect(PVector inpos, int inw, int inh, int inr, int ing, int inb) {
        super(inpos, inw, inh, inr, ing, inb, new PVector(0,0)); //start out speedless
      }
      void moveself() {
        if (checkKey("W")) {
          speed.y -= .1;
        }
        if (checkKey("S")) {
          speed.y += .1;
        }
        if (checkKey("A")) {
          speed.x -= .1;
        }
        if (checkKey("D")) {
          speed.x += .1;
        }
      }
    }
       

    boolean collDetect(Rectangle rect1, Rectangle rect2) {
      if (rect1.pos.x+rect1.w < rect2.pos.x) {
        return false;
      }
      if (rect1.pos.x > rect2.pos.x+rect2.w) {
        return false;
      }
      if (rect1.pos.y+rect1.h < rect2.pos.y) {
        return false;
      }
      if (rect1.pos.y > rect2.pos.y+rect2.h) {
        return false;
      }
      return true;
    }
  3. class Trigger { //by kritzikratzi
      long start = millis();
      int rate;

      public Trigger( int rate ) {
        this( rate, false );
      }
     
      public Trigger( int rate, boolean immediately) {
        setRate( rate );
        if ( immediately ) start -= rate;
      }

      public void setRate( int rate ) {
        this.rate = rate;
      }
      public int getRate() {
        return rate;
      }
     
      public boolean fires() {
        if ( millis() - start >= rate ) {
          start += rate;
          return true;
        }
        else {
          return false;
        }
      }
     
      public void reset() {
        start = millis();
      }
    }
  4. boolean[] keys = new boolean[526];
    boolean checkKey(String k) {
      for (int i = 0; i < keys.length; i++) {
        if (KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())) {
          return keys[i];
        }
      }
      return false;
    }
    void keyPressed()
    {
      keys[keyCode] = true;
      //println(KeyEvent.getKeyText(keyCode));
    }

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

It's a bit choppy when i move that rect, even though it have a high-end graphics card. Why is this?

Replies(5)

You can monitor your frame rate by adding these lines to the end of your draw routine:

  if (frameCount % 60 == 0) {
    println(frameRate);
  }

I'm getting approx 60 fps when I test your code, which is the default frame rate.  So on my machine, the code is plenty fast.  I suspect your problem is with the movement style, rather than with a slow frame rate, however if you wish to increase your framerate, you can adjust it at the end of your setup routine:

frameRate(120); // double the frame rate

For games, I actually prefer to use a slower frame rate (30), and get the physics right for that slower frame rate.  This puts less stress on the CPU.  Also, if you specify a faster frame rate, you should still try measuring it, because you aren't guaranteed to get that faster framerate.

You can control the movement by changing various lines.

There are a number of lines that look like this - 

      speed.y -= .1;

these are adding small amounts to the velocity.  You can experiment with changing these amounts.  Try .2, for example.  If you use larger amounts, you may find it helpful to also do some velocity damping so that the square is easier to control.  This will cause the square to slow down when you stop pressing a key.

   speed.x *= .98;
   speed.y *= .98;


good suggestions. but, the game is called "frictionless", so I want no velocity damping.

What about "double buffering"? I've heard that turns down flickering/unwanted blur.
Not sure about how to force double buffering in Processing (but maybe it already is in certain renderers ?) - its more likely an issue you may be able to option on your graphics card software (if you're on Windows) or developer tools (if your on Mac). It would surely help, as double buffering essentially has a front and a rear buffer for your graphics updates, allowing it to store a buffer when your screen is ready to update.

That being said, that issue really points to a frame rate issue in your sketch... so maybe the issue is just to limit it to a slower fps ? Depends on your card, what the sketch can run at, and your final display refresh rate.

There's a good overview of these concepts on the Touch Designer Wiki >  http://www.derivative.ca/wiki/index.php?title=Vertical_Sync_and_Horizontal_Tearing
General advice: don't do the same error as I did when I started Processing... Don't write size(windowX,windowY) but put the actual numbers there (they are used when exporting the applet) and just use width and height variables.

" I want no velocity damping"
If you effectively double the frame rate and halve the speed, the rectangle should move more smoothly and at the same effective speed.
Hi, 

This sounds like something I was struggling with when I was making my game. Getting a constant linear movement was a mission. But basically, when the move and update functions are both calculated at the same time you get slightly, laggy, movement as occasionally the movement function takes fractionally longer to calculate or everything gets paused for a fraction of a second by the garbage collector and the result is what appears to be a jerk.

So, even with something as simple as moving an ellipse across the screen there appears to be some lag, but the after a lot of experimenting I found that the best way is to have a second thread. Have that separate thread doing your movement, and then draw() will just render your objects irrelevant of their position. This creates near-perfect, smooth movement.

The alternative (if you can implement it well is using delta time) so that movement is time based, rather than frame-rate dependent. This also means you can have everything running at the same speed no matter what the hardware. Matt Ditton's polymonkey.time library, might also help. It was pretty useful for me, but doesn't seem to quite work right with Android, which was where I needed. I really wish that Processing had a time function like Unity!  

Anyway this turned into a bit of a rant, I hope this helps in my some way, i could be waaaay off the mark cos I can't actually test your code from here, but from what I'm reading, a thread might sort things out for you. 

Merry Christmas all.

Dave