Make the mouse act with a joystick

Asked

Viewed 66 times

0

My code should make the mouse act with a joystick to drive a ball on the screen. It should work as follows: if the mouse is in the crosshairs, the ball does not move. If the mouse is to the right or to the left, the ball moves to the right or to the left. If the mouse is above or below the center, the ball moves up or down. The distance of the center mouse determines how fast the ball moves. I managed to implement the code to draw the ball and aim. But I can’t get the ball to move properly. How I use min and max functions in this case?

int ballPositionX = 250;
int ballPositionY = 250;
int ballSize = 20;
int slowerSpeed = 50;
int mouvement = 250;
int linePt1 = 100;
int linePt2 = 250; 
int linePt3 = 400;
int rightLimit, leftLimit, bottomLimit, topLimit;

int moveX, moveY;

void setup(){
  size(500,500);
}

void draw(){
  background(255);
  line(linePt1,linePt2,linePt3,linePt2);
  line(linePt2,linePt1,linePt2,linePt3);
  ellipse(ballPositionX, ballPositionY, ballSize, ballSize);
  moveX = (mouseX - 250);
  moveY = (mouseY - 250);
  ballPositionX = min(ballPositionX, moveX+ballPositionY);
  ballPositionY = max(ballPositionY, moveY+ballPositionX);
}
  • You will need a Mouseadapter (https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html)

  • No @prmottajr. I need to calculate ballPositionX and ballPositionY. I know it’s a simple thing, but I’m racking my brain here.

  • Add a [mcve], because as it comes to swing, you need to test the problem to get better results to suggest and your code is not executable.

  • The Processing language doesn’t use swing for what I know. It’s simpler than that, but I can’t solve.

  • @Carlosheuberger the solution includes min and max. Yes, the formula is wrong. And that’s what I’m asking for help.

  • @Carlosheuberger the hiccup also does not include IF. That’s why I’m breaking my head.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.