# Player keeps moving with the bullet

**URL:** <https://discourse.processing.org/t/player-keeps-moving-with-the-bullet/23320>\
**Category:** Coding Questions\
**Created:** [August 17, 2020, 11:17pm UTC](https://discourse.processing.org/t/player-keeps-moving-with-the-bullet/23320 "2020-08-17T23:17:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [August 17, 2020, 11:17pm UTC](https://discourse.processing.org/t/player-keeps-moving-with-the-bullet/23320/1 "2020-08-17T23:17:30Z")

</div>

I have this code, and there’s a player class and a bullet class. But when I shoot a bullet, it moves the player along with it…

```auto
class Bullet
{
  PVector pos;
  PVector vel;
  float rotation;
  
  Bullet(PVector pos, PVector tar, float rotation)
  {
    this.pos = pos;
    this.rotation = rotation;
    
    vel = new PVector(tar.x - pos.x, tar.y - pos.y);
    vel.normalize();
  }
  
  void Display()
  {
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(rotation);
    fill(230, 255, 0);
    noStroke();
    rect(-15, 5, 5, 10);
    popMatrix();
    
    pos.add(vel);
  }
}

class Player
{ 
  ArrayList<Bullet> bullets;
  
  PVector pos, vel;
  boolean up, down, left, right;
  
  Player()
  {
    bullets = new ArrayList<Bullet>();
    
    pos = new PVector(60, 60);
    vel = new PVector(0, 0);
    
    up = false;
    down = false;
    left = false;
    right = false;
  }
  
  void Display()
  {
    fill(230);
    stroke(180);
    strokeWeight(3);
    ellipse(pos.x, pos.y, 40, 40);
    
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(radians(degrees(atan((mouseY - pos.y) / (mouseX - pos.x))) + (mouseX > pos.x ? 270 : -270)));
    fill(120);
    noStroke();
    rectMode(CENTER);
    rect(-15, 5, 10, 35);
    popMatrix();
    
    for (int a = 0; a < bullets.size(); ++a)
      bullets.get(a).Display();
  }
  
  void Move()
  {
    if(up)
      vel.y -= 0.5;
    if(down)
      vel.y += 0.5;
    if(left)
      vel.x -= 0.5;
    if(right)
      vel.x += 0.5;

    vel.mult(0.9);
    pos.add(vel);
  }
  
  void Shoot()
  {
    bullets.add(new Bullet(pos, new PVector(mouseX, mouseY), radians(degrees(atan((mouseY - pos.y) / (mouseX - pos.x))) + (mouseX > pos.x ? 270 : -270))));
  }
}

```

So when I press my mouse, it calls the `player.Shoot()` functions, and when that happens, it move my player forward??

---

<div class="post-metadata">

**Author:** ![Hapiel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hapiel/32/10226_2.png) [@Hapiel](https://discourse.processing.org/u/Hapiel)\
**Post date:** [August 18, 2020, 11:55am UTC](https://discourse.processing.org/t/player-keeps-moving-with-the-bullet/23320/2 "2020-08-18T11:55:04Z")

</div>

I don’t understand exactly why this works (I spent half an hour tinkering around with it, confused), but the answer is `pos.copy()`…

```auto
void Shoot()
  {
    bullets.add(new Bullet(pos.copy(), new PVector(mouseX, mouseY), radians(degrees(atan((mouseY - pos.y) / (mouseX - pos.x))) + (mouseX > pos.x ? 270 : -270))));
  }

```

---

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [August 18, 2020, 7:14pm UTC](https://discourse.processing.org/t/player-keeps-moving-with-the-bullet/23320/3 "2020-08-18T19:14:30Z")

</div>

huh, that works, that’s weird. I thought java was pass by reference

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 13, 2020, 10:00pm UTC](https://discourse.processing.org/t/player-keeps-moving-with-the-bullet/23320/4 "2020-09-13T22:00:19Z")

</div>

> [@Cybernet](#):
>
> I thought java was pass by reference

Java is always pass by value. That value may be a pointer to an object. One extended discussion is here:

> <https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value>
