# How to make an object go past another with PVectors?

**URL:** https://discourse.processing.org/t/how-to-make-an-object-go-past-another-with-pvectors/605
**Category:** Coding Questions
**Created:** [June 1, 2018, 7:08pm UTC](https://discourse.processing.org/t/how-to-make-an-object-go-past-another-with-pvectors/605 "2018-06-01T19:08:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![orin635](https://avatars.discourse-cdn.com/v4/letter/o/82dd89/32.png) [@orin635](https://discourse.processing.org/u/orin635)
#### Post date: [June 1, 2018, 7:08pm UTC](https://discourse.processing.org/t/how-to-make-an-object-go-past-another-with-pvectors/605/1 "2018-06-01T19:08:01Z")

</div>

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

* * *

```auto
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;
}

```

---

<div class="post-metadata">

### Author: ![orin635](https://avatars.discourse-cdn.com/v4/letter/o/82dd89/32.png) [@orin635](https://discourse.processing.org/u/orin635)
#### Post date: [June 1, 2018, 7:31pm UTC](https://discourse.processing.org/t/how-to-make-an-object-go-past-another-with-pvectors/605/2 "2018-06-01T19:31:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [June 1, 2018, 7:41pm UTC](https://discourse.processing.org/t/how-to-make-an-object-go-past-another-with-pvectors/605/3 "2018-06-01T19:41:19Z")

</div>

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

Have a nice day!

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [June 2, 2018, 3:43am UTC](https://discourse.processing.org/t/how-to-make-an-object-go-past-another-with-pvectors/605/4 "2018-06-02T03:43:45Z")

</div>

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

```auto
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);
}

```
