Let the image object move in different directions

Let’s say your object is at x,y.

You move it by


x+=xadd; 
y+=yadd;

xadd and yadd can be positive or negative.

Now, use the function mousePressed() and compare the mouse pos mouseX, mouseY
with x,y.
When

  • mouseX < x say xadd = -1;
  • mouseX > x say xadd = 1;
  • mouseY < y say yadd = -1;
  • mouseY > y say yadd = 1;

So you can get a vertical, diagonal and horizontal movement.

For movement in any direction see atan2 - Understanding the Atan2, Please help - #6 by CodeMasterX

1 Like