# Delta operators, and magicaly changing PVectors

**URL:** <https://discourse.processing.org/t/delta-operators-and-magicaly-changing-pvectors/3189>\
**Category:** Coding Questions\
**Created:** [September 2, 2018, 8:28am UTC](https://discourse.processing.org/t/delta-operators-and-magicaly-changing-pvectors/3189 "2018-09-02T08:28:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BodaciousBrian](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@BodaciousBrian](https://discourse.processing.org/u/BodaciousBrian)\
**Post date:** [September 2, 2018, 8:28am UTC](https://discourse.processing.org/t/delta-operators-and-magicaly-changing-pvectors/3189/1 "2018-09-02T08:28:34Z")

</div>

_Solution at the bottom of post, but I can’t offer an explanation!_

I’m trying to make a delta operator to keep track of change in velocity, and I encountered something I have never seen in any language before. I store one pvector into another, manipulate the original, and BOTH experience the same manipulation? What is going on?

```auto
class Star {
  PVector pos = new PVector(width/2, height/2); //position
  PVector clk = new PVector(0, 0); //click
  PVector Vel = new PVector(0, 0); //velocity
  PVector oVel = new PVector(0, 0); //old velocity
  PVector dVel = new PVector(0, 0); //delta velocity
  PVector acc = new PVector(0, 0); //acceleration
  int size;
  
  Star (int s){
    size = s;
  }
  void click(float x, float y){
    clk.x = x;
    clk.y = y;
  }
  void update( ){
   
    acc.limit(2);
    acc.x = clk.x - pos.x;
    acc.y = clk.y - pos.y;
    acc.normalize();

   // Start of question area

    oVel = Vel; //Variable copied
    Vel.add(acc); //orignal changed
    Vel.mult(0.97); //and changed again
    println(oVel, Vel); //oVel, and Vel are always equal, oVel is magically changed

    dVel.sub(Vel, oVel); 
    
    pos.add(Vel);
    
    pushMatrix();
     //ill spare you this long atrocity.
    popMatrix();
    
  }
}

```

Well I was able to make this work by using the PVector.copy() method.

```auto
oVel = Vel.Copy();

//instead of :
oVel = Vel;

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 2, 2018, 8:31am UTC](https://discourse.processing.org/t/delta-operators-and-magicaly-changing-pvectors/3189/2 "2018-09-02T08:31:01Z")

</div>

Hi BodaciousBrian,

When you copy an object like this, you are not making a deep copy. In other words, you are not creating a new object with the same properties.

What you have is another variable that points toward the same object. So if you modify the object through one of those variable, then you modify it for both variable.

The `Copy()` method actually returns a new PVector with the same properties. That’s why in this case, you can modify one without modifying the other: they are 2 different objects.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 2, 2018, 12:59pm UTC](https://discourse.processing.org/t/delta-operators-and-magicaly-changing-pvectors/3189/3 "2018-09-02T12:59:06Z")

</div>

> **[Pointer aliasing](https://en.wikipedia.org/wiki/Pointer_aliasing)**
>
> In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names.
> For instance, if a function takes two pointers A and B which have the same value, then the name A aliases the name B. In this case we say the pointers A and B alias each other. Note that the concept of pointer aliasing is not very well-defined – two pointers A and B may or may not alias each other, depending on what operations are performed in the function using A and...

---

<div class="post-metadata">

**Author:** ![BodaciousBrian](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@BodaciousBrian](https://discourse.processing.org/u/BodaciousBrian)\
**Post date:** [September 2, 2018, 4:13pm UTC](https://discourse.processing.org/t/delta-operators-and-magicaly-changing-pvectors/3189/4 "2018-09-02T16:13:52Z")

</div>

I thank you both. I had never encountered any thing like that before.
