How to make an object go past another with PVectors?

Hi guys, last night I posted about making an object move in the direction of my mouse and I finally got that!

I’m so pleased but now I need to make the object continue past the marker I make.

I’m terrible at explaining things so I’m just gonna copy and paste my code and please just past it into processing to see what I have. as you can see when you click the black ball goes towards where you clicked then just goes backwards. I’d like to make it like so it just continues forward

code


PVector bullet, mouse;

int dragX=0;
int dragY=0;
float bullet_speed=10;
float tr=0;
float tr2=0;

void setup()
{
 size(1600,900);
 noStroke();
 bullet = new PVector(10,890);
 mouse = new PVector(10,890);
}

void draw()
{
  
  background(204);
  fill(0);
  //--------------------------------------------- OBJECTS
  ellipse(dragX, dragY,3,3);
 ellipse(bullet.x,bullet.y,20,20);
 //----------------------------------------------SHOOTING PART
  if(mousePressed==true)
  {
   tr+=2;
  }

  if(tr>2 && dragX>0)
  {
  PVector target = PVector.sub (mouse,bullet);
  mouse.set (dragX,dragY);
  target.normalize();
  target.mult(bullet_speed);
  bullet.add(target);
  }
  
  //--------------------------------------------going past where i place my marker
 if(dist(bullet.x,bullet.y,dragX,dragY)<11.5)
 {
  tr2+=2; 
  tr=0;
 }
 
 if(tr2>1)
  {
    bullet_speed=-10;
    PVector target = PVector.sub (mouse,bullet);
    target.normalize();
   target.mult(bullet_speed);
   bullet.add(target);
  }

  //----------------------------------------PRINT
  println(dist(bullet.x,bullet.y,dragX,dragY));
}


void mouseReleased() 
{
  dragX = mouseX;
  dragY = mouseY;
}

I think i got it i changed the mouse.set to (10,890) then made the bullet speed negetive

Please format your code with </> in the editor.

Have a nice day!

Please format your code by clicking the </> icon when you are creating or editing a new post.

I have looked at your code and you just need to remove the lines that invert the motion. Then, thinking about your logic, you can actually reduce your code in order to remove those tr and tr2 variables as they are not needed. Lastly, you should be using the function mouseReleased() instead of the property mousePressed as mousePressed is executed ad many times while the mouse is pressed while mousePressed()/mouseReleased() is executed only once. Code below.

Kf

PVector bullet, mouse;

int dragX=0;
int dragY=0;
float bullet_speed=10;
float tr=0;
float tr2=0;
PVector target;

void setup()
{
  size(1600, 900);
  noStroke();
  bullet = new PVector(10, 890);
  mouse = new PVector(10, 890);
}

void draw()
{

  background(204);
  fill(0);
  //--------------------------------------------- OBJECTS
  ellipse(dragX, dragY, 3, 3);
  ellipse(bullet.x, bullet.y, 20, 20);
  //----------------------------------------------SHOOTING PART
  //if (mousePressed==true)


  if (target!=null)  {
    bullet.add(target);
  }


  //--------------------------------------------going past where i place my marker
  if (dist(bullet.x, bullet.y, dragX, dragY)<11.5)
  {
    //tr2+=2;
    //tr=0;
  }

  //if (tr2>1)
  //{
  //  bullet_speed=-10;
  //  PVector target = PVector.sub (mouse, bullet);
  //  target.normalize();
  //  target.mult(bullet_speed);
  //  bullet.add(target);
  //}

  //----------------------------------------PRINT
  println(dist(bullet.x, bullet.y, dragX, dragY));
}

void mouseReleased()
{
  dragX = mouseX;
  dragY = mouseY;

  //tr+=2;
  //bullet_speed=10;
  mouse.set (dragX, dragY);
  target = PVector.sub (mouse, bullet);
  target.normalize();
  target.mult(bullet_speed);
}
1 Like