What does add() do?

So I have a code:

class Projectile extends PVector
{
  PVector velocity;
 
  Projectile(PVector loc, PVector vel)
  {
    super(loc.x, loc.y);
    velocity = vel;
  }
 
  void Display()
  {   
    add(velocity); // what does this do?
    
    fill(0, 0, 255);
    ellipse(x, y, 3, 3);
  }
}

I found this on a example I saw. It creates a bullet, and shoots it towards the mouse. The example didn’t explain the code, so I had to figure out what I does. But I’m stuck on this part. When it says add(velocity), it confuses me. What are you adding velocity too?

Since this is inside the Class Projectile which extends from PVector, the line in questions is invoking the add() associated to PVector. This is a core concept in object oriented programming where your Projectile class is inheriting fields, functions defined in PVector.

In other words, you can think of that line as this.add(velocity); so it is adding velocity to the member variables x,y,z, associated to your instantiated Projectile object, variables inherited from the PVector class.

Kf

2 Likes

Okay. Thank you. One more question too. Is it also the same idea with ellipse(x, y, 3, 3)? So the x and y are from PVector?

That is correct. You can see this in the docs and it the actual code:

Kf

1 Like