How do I make mouse controlled paddle stop glitching

I’m am trying to make a pong game where one player controls with the mouse and the other controls with the keyboard. I got my mouse to move the paddle, but it keeps glitching to different places on the screen. Any ideas?

//pong tab

sunBall sunball;



cloudPaddle left;
cloudPaddle right;

int leftscore = 0;
int rightscore = 0;

void setup() {
  size(1500, 1500);

  sunball = new sunBall();
  left = new cloudPaddle(true);
  right = new cloudPaddle(false);
}

void draw() {
  background(111,180,255);

  //puck.checkPaddle(left);
  sunball.checkcloudPaddleRight(right);
  sunball.checkcloudPaddleLeft(left);

  left.show();
  right.show();
  left.update();
  right.update();

  sunball.update();
  sunball.edges();
  sunball.show();

  fill(255);
  textSize(32);
  text(leftscore, 32, 40);
  text(rightscore, width-64, 40);
}

void keyReleased() {
  left.move(0);
  right.move(0);
}

void keyPressed() {
  if (key == 'a') {
    left.move(-15);
  } else if (key == 'd') {
    left.move(15);
  }

  
}
void mouseReleased(){


}

void mouseMoved(){

    right.move(mouseY);
}
//paddles tab

class cloudPaddle {
  float x;
  float y = height/2;
  float w = 30;
  float h = 150;

  float ychange = 0;

  cloudPaddle(boolean left) {
    if (left) {
      x = w;
    } else {
      x = width - w;
    }
  }

  void update() {
    y += ychange;
    y = constrain(y, h/2, height-h/2);
  }


  void move(float steps) {
    ychange = steps;
    y = mouseY;
    
    
  }

  void show() {
    fill(255);
    rectMode(CENTER);
    rect(x, y, w, h);
  }
}
//ball tab

class sunBall {
  float x = width/2;
  float y = height/2;
  float xspeed;
  float yspeed;
  float r = 17;

  sunBall() {
    reset();
  }

  void checkcloudPaddleLeft(cloudPaddle p) {
    if (y - r < p.y + p.h/2 && y + r > p.y - p.h/2 && x - r < p.x + p.w/2) {
      if (x > p.x) {
        float diff = y - (p.y - p.h/2);
        float rad = radians(45);
        float angle = map(diff, 0, p.h, -rad, rad);
        xspeed = 5 * cos(angle);
        yspeed = 5 * sin(angle);
        x = p.x + p.w/2 + r;
        //xspeed *= -1;
      }
    }
  }
  void checkcloudPaddleRight(cloudPaddle p) {
    if (y - r < p.y + p.h/2 && y + r > p.y - p.h/2 && x + r > p.x - p.w/2) {
      if (x < p.x) {
        //xspeed *= -1;
        float diff = y - (p.y - p.h/2);
        float angle = map(diff, 0, p.h, radians(225), radians(135));
        xspeed = 5 * cos(angle);
        yspeed = 5 * sin(angle);
        x = p.x - p.w/2 - r;
      }
    }
  }




  void update() {
    x = x + xspeed;
    y = y + yspeed;
  }

  void reset() {
    x = width/2;
    y = height/2;
    float angle = random(-PI/4, PI/4);
    //angle = 0;
    xspeed = 5 * cos(angle);
    yspeed = 5 * sin(angle);

    if (random(1) < 0.5) {
      xspeed *= -1;
    }
  }

  void edges() {
    if (y < 0 || y > height) {
      yspeed *= -1;
    }

    if (x - r > width) {

      leftscore++;
      reset();
    }

    if (x + r < 0) {

      rightscore++;
      reset();
    }
  }


  void show() {
    fill(255,221,60);
    ellipse(x, y, r*2, r*2);
  }
}
1 Like

Your right panel is glitching out because you’re calling right.move(mouseY) every time the mouse is moved. So far so good. But then every frame you’re also calling right.update() which adds ychange to right.y resulting in sporadic behavior.
Comment out right.update() or y += ychange and that will fix the right panel.

Your left panel is glitching out because your cloudPaddle.move() method is taking in a direct input from the mouse, which you don’t want for the left panel. You could replace y = mouseY with y += steps, but then you’ll have choppy movement. I haven’t taken the time to learn key inputs in processing yet so I can’t help you there but you should have no problem finding resources.
Also, this will break your right paddle, so you’ll need to play around to find a solution :slight_smile:

1 Like

Thanks for the recommendations!

I have not run your code but I suspect that the problem is that you are not validating the y position in move(…) try changing this method to

void move(float steps) {
    ychange = steps;
    y = constrain(mouseY, h/2, height - h/2);
}

Also leave the constraint in the update method.

Unfortunately my issue persists, but thanks for the idea!