'shivering' of object when touching surface

edited January 2016 in Programming Questions

So I made a fairly simple maze game that prevents a dot from touching the 'walls' by checking if it within coordinates and then moving it towards the nearest edge. I have to move it away fairly quickly to counteract the speed that it is hitting the wall. This causes it to jump back and forth which doesn't look very nice. Any suggestions on fixing this?

code:

//scene
int[] posX;
int[] posY;
int[] posW;
int[] posH;
int offX;
int offY;

//dot
int x = 320;
int y = 180;
int contX = 1;
int contY = 1;

//stats
int[] var;

void setup() {

  size(640, 360);
  fill(117, 117, 163);
  noStroke();
  orientation(LANDSCAPE);

  String[] tempX = loadStrings("world/posX.txt");
  posX = int(split(tempX[0],','));
  String[] tempY = loadStrings("world/posY.txt");
  posY = int(split(tempY[0],','));
  String[] tempH = loadStrings("world/posH.txt");
  posH = int(split(tempH[0],','));
  String[] tempW = loadStrings("world/posW.txt");
  posW = int(split(tempW[0],','));
  String[] tempV = loadStrings("world/var.txt");
  var = int(split(tempV[0],','));
}

void draw() {

  background(0, 0, 0);

  scene();

  check();

  movement();



}

void scene(){

  ellipse(x, y, 10, 10);

  for (int i = 0; i < var[0]; i++) {
    rect(posX[i], posY[i], posW[i], posH[i]);
  }
}

void check() {
  for (int i = 0; i < var[0]; i++) {

    int top = posY[i];
    int bottom = posY[i] + posH[i];
    int left = posX[i];
    int right = posX[i] + posW[i];

    if(x > posX[i] && x < posX[i] + posW[i] && y > posY[i] && y < posY[i] + posH[i]) {

      int mT = y - top;
      int mB = bottom - y;
      int mL = x - left;
      int mR = right - x;

      if (mT < mB && mT < mL && mT < mR) {
        contY = 0;
        offY = offY + 4;
      }
      if (mB < mL && mB < mR && mB < mT) {
        contY = 0;
        offY = offY - 4;
      }
      if (mL < mR && mL < mT && mL < mB) {
        contX = 0;
        offX = offX + 4;
      }
      if (mR < mB && mR < mL && mR < mT) {
        contX = 0;
        offX = offX - 4;
      }
      else {
        offX = offX + 2;
        offY = offY - 2;
      }
    }
    else {
      contX = 1;
      contY = 1;
    }
  }
}

void movement() {
  if(mousePressed == true) {

    if (x > mouseX) {
      offX = offX + var[1]*contX;
    }
    if (x < mouseX) {
      offX = offX - var[1]*contX;
    }
    if (y > mouseY) {
      offY = offY + var[1]*contY;
    }
    if (y < mouseY) {
      offY = offY - var[1]*contY;
    }
  }

  for (int i = 0; i < var[0]; i++) {
        posX[i] = posX[i] + offX;
        posY[i] = posY[i] + offY;
  }
    offX = 0;
    offY = 0;

}

Answers

  • Well first to add code, you can do the usual copy paste, then you highlight it and press the little C that you see in the text box right on the top. Now this problem could be caused by the object jumping back, but not far enough out of your if(reachedCertainPoint){ jumpBack } so it continues doing that for a little bit. That's my best guess on why since I can't really guess what code you have.

  • added code!

  • But it's useless without the files...

  • The files simply contain some coordinates for the rectangles.

    They are in the format "number, number, number, ..."

  • Without the files we can't run your program and try out possible solutions. You could archive the sketch (on the Tools menu) and put the zip file somewhere we can download it.

  • In check() it doesn't look like you reset the position within bounds, so it's possible that the dot is still out of bounds on the next frame... Though the other possibility is that the adjustments you make to offX and offY aren't enough to reverse the direction: you'd usually multiply by -1 to effect a bounce.

    Though as already said that's mere supposition on my part since I can't test your code :(|)

  • So here is a link to the whole folder. It also includes a simple editor but you dont have to worry about that.

    https://www.dropbox.com/s/njh48a5oitg7a4l/move.zip?dl=0

Sign In or Register to comment.