Need help getting object to leave trail

I have a sketch where a Pacman character moves around the screen trying to eat cherries. I want Pacman to leave a trail of yellow dots as he moves and to then remove the dots when he comes back into contact with them. The problem at the moment though is that the dots are transmitted on Pacmans location and so he removes them before they show up on screen. I therefore need a way of transmitting the dots just behind Pacman instead. Any help would be much appreciated.

    Pacman pacman = new Pacman(75); 

   void setup() {
  size(500, 500); //set the size of the page to 500pixels by 500pixels
  ellipseMode(CENTER); //set ellipse mode to center
  }

  void draw() {

  background(0); //set background to black
  pacman.update(); //use the function update() as written in class Pacman                               
  pacman.transmit(); //use the function transmit() as written in class Pacman 
  pacman.drawTrail(); //use the function drawTrail() as written in class Pacman
  pacman.collideTrail(); //use the function collideTrail() as written in class Pacman
  pacman.checkEdges();
  pacman.draw(); //use the function draw() as written in class Pacman
  }

  class Pacman {

  PVector location; 
  PVector velocity;                                                     
  PVector acceleration;                                                    
  PVector target; 
  float topspeed; 
  float tolerance = 15;      
  float direction = 0;

  float diameter; //declare diameter as float
  float MouthAngle = 0; //set MouthAngle to 0 (starting position closed)
  float OpeningAngle = PI * 2 / 180; 
  boolean MouthMovement = true; //set variable for MouthMovement
  ArrayList trail; // declare the use of trail as an Arraylist
  boolean MousePressed = false;


  Pacman(float dia) {

    diameter = dia;  
    trail = new ArrayList(); // assign trail as a function to create a new Arraylist
    location = new PVector(250,250);            
    velocity = new PVector(0, 0); 
    target = new PVector(width/2, height/2);                              
    topspeed = 3; //top speed is assigned a value of 3
  }

  void update() {


    PVector dir = PVector.sub(target, location);
    float distance = dist(location.x, location.y, target.x, target.y); 



    if (topspeed > tolerance) {                                           
      topspeed *= 0.9;
    } else {
      topspeed = distance*0.05;
    }

    dir.normalize(); //normalize
    direction = dir.mag(); 
    acceleration = dir; // acceleration = direction                                               
    velocity.add(acceleration); 
    velocity.limit(topspeed);   //limit velocity to topspeed


    if (distance<tolerance) { 
      target.x = random(width -100, width +100); 
      target.y = random(height -100, height +100);
    } else { //if the target hasn't been reached 
      location.add(velocity); //continue to move as normal

      if (mousePressed) {
        target.x = mouseX;
        target.y = mouseY;
      }
    }
  }

  void draw() {

    float dX = location.x - target.x; 
    float dY = location.y - target.y; 


    float angle = radians((atan2(dY, dX) * 180 / PI));


    if (dist(target.x, target.y, location.x, location.y) > 0) {
      cherries();

      pushMatrix();
      translate(location.x, location.y);
      rotate(angle);
      if (MouthMovement == true) MouthAngle += OpeningAngle; 
      else MouthAngle -= OpeningAngle; 
      if (MouthAngle > TWO_PI / 8 || MouthAngle < 0) MouthMovement = ! MouthMovement;
      stroke(0);
      fill(255, 255, 0); // set fill to yellow
      arc(0, 0, diameter, diameter, HALF_PI*-2 + MouthAngle, HALF_PI*2 -MouthAngle); 
      fill (0); //set fill to black
      ellipse(0, 0 - diameter / 4, diameter/8, diameter/8);
      popMatrix();
    }
  }

  void checkEdges() {
    if (location.x  > width -diameter/2 || location.x  <0 +diameter/2 ) { 
      velocity.x *= -2; //its velocity is multiplied by -0.1
      target.x = random(width); 
      target.y = random(height);
    } 
    if (location.y +diameter/2> height || location.y  <0 +diameter/2 ) { 
      velocity.y *= -2; //its velocity is multiplied by -0.1
      target.x = random(width); 
      target.y = random(height);
    }
  }

  void transmit() {


    for (int i=0; i<trail.size (); i++) { 
      if (trail.size() != 0) { 
        crumbs t = (crumbs) trail.get(i); 
        t.draw();
      }
    }
  }
  void drawTrail() {


    trail.add(new crumbs(new PVector(location.x, location.y), 10));
  }

  void collideTrail() {

    PVector crumbPos; 
    for (int i=0; i<trail.size (); i++) {
      crumbs drop = (crumbs) trail.get(i);
      crumbPos = drop.loc;
      float d = location.dist(crumbPos); 
      if (d < diameter/2) { //if Pacman is touching the crumbs                                
        trail.remove(i);
        // target.x = random(width); 
        //  target.y = random(height); 
        //velocity.x *= -2;
        //velocity.y *= -2;
      }
    }
  }


  void cherries() {
    stroke(0, 255, 0); //set stroke to green
    line (target.x -5, target.y+1, target.x-2.5, target.y-25); //draw line
    line (target.x +5, target.y-1, target.x-2.5, target.y-25); //draw line

    stroke(0);
    fill(255, 0, 0); //set fill to red
    ellipse(target.x-5, target.y-1, 15, 15); //draw ellipse 
    ellipse(target.x+5, target.y+1, 15, 15); //draw ellipse
   }
  }

   class crumbs { 

  PVector loc; //declare loc as a PVector 
  float diameter; //declare diameter as a float 



  crumbs(PVector loc_, float dia) {
    loc = loc_; 
    diameter=dia;
  }

  void draw () { 
    fill (255, 255, 0); 
    noStroke(); 

    ellipse(loc.x, loc.y, diameter, diameter);
    }
  }

Answers

  • please don't post duplicates

  • edited November 2016

    Sorry I just thought I'd repost it because I messed up formatting the last one

  • Answer ✓

    You already know the direction that your pacman is moving, so you could add the opposite to the location when you spawn your crumbs.

    Here is a quick example:

      void drawTrail() {
        // create vector that points to the back of pacman
        PVector behind = new PVector(-velocity.x, -velocity.y);
        // set length of vector, so it doesn't collide with pacman
        behind.setMag(diameter);
    
        trail.add(new crumbs(new PVector(location.x+behind.x, location.y+behind.y), 10));
      }
    
  • Awesome! That has done exactly what I wanted it to. Thank you very much!

Sign In or Register to comment.