Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Cat theOneCat;
- void setup() {
- size(800, 600);
- theOneCat = new Cat();
- theOneCat.posX = width/2;
- theOneCat.posY = height/2;
- }
- void draw() {
- background(255);
- noStroke();
- fill(255, 0, 255);
- ellipse(mouseX, mouseY, 100, 100);
- theOneCat.chase(mouseX, mouseY);
- theOneCat.drawCat();
- }
- class Cat {
- float posX;
- float posY;
- float speed = 1.5;
- void drawCat() {
- noStroke();
- fill(0);
- ellipse(this.posX, this.posY, 100, 100);
- }
- void chase(int targetX, int targetY) {
- //println("Cat: I'm moving to " + targetX + " " + targetY);
- float diffX = targetX - this.posX;
- float diffY = targetY - this.posY;
- float angle = atan2(diffY, diffX);
- println("angle (rad) " + angle + " (deg) " + degrees(angle));
- float moveX = cos(angle) * this.speed;
- float moveY = sin(angle) * this.speed;
- this.posX += moveX;
- this.posY += moveY;
- // as alternative, you could use this too
- // it's simpler to write, but less simple to control the speed
- /*
- this.posX += diffX * 0.1;
- this.posY += diffY * 0.1;
- */
- }
- }
Add Comment
Please, Sign In to add comment