Guest User

Untitled

a guest
Jun 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. Cat theOneCat;
  2.  
  3. void setup() {
  4. size(800, 600);
  5.  
  6. theOneCat = new Cat();
  7. theOneCat.posX = width/2;
  8. theOneCat.posY = height/2;
  9. }
  10.  
  11.  
  12. void draw() {
  13. background(255);
  14.  
  15. noStroke();
  16. fill(255, 0, 255);
  17. ellipse(mouseX, mouseY, 100, 100);
  18.  
  19. theOneCat.chase(mouseX, mouseY);
  20.  
  21. theOneCat.drawCat();
  22. }
  23.  
  24.  
  25. class Cat {
  26. float posX;
  27. float posY;
  28. float speed = 1.5;
  29.  
  30. void drawCat() {
  31. noStroke();
  32. fill(0);
  33. ellipse(this.posX, this.posY, 100, 100);
  34. }
  35.  
  36. void chase(int targetX, int targetY) {
  37. //println("Cat: I'm moving to " + targetX + " " + targetY);
  38.  
  39. float diffX = targetX - this.posX;
  40. float diffY = targetY - this.posY;
  41.  
  42. float angle = atan2(diffY, diffX);
  43.  
  44. println("angle (rad) " + angle + " (deg) " + degrees(angle));
  45.  
  46. float moveX = cos(angle) * this.speed;
  47. float moveY = sin(angle) * this.speed;
  48.  
  49. this.posX += moveX;
  50. this.posY += moveY;
  51.  
  52.  
  53. // as alternative, you could use this too
  54. // it's simpler to write, but less simple to control the speed
  55. /*
  56. this.posX += diffX * 0.1;
  57. this.posY += diffY * 0.1;
  58. */
  59.  
  60. }
  61. }
Add Comment
Please, Sign In to add comment