# How to create a vector with 4 parameters?

**URL:** <https://discourse.processing.org/t/how-to-create-a-vector-with-4-parameters/26509>\
**Category:** Beginners\
**Created:** [December 25, 2020, 12:48pm UTC](https://discourse.processing.org/t/how-to-create-a-vector-with-4-parameters/26509 "2020-12-25T12:48:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 25, 2020, 12:48pm UTC](https://discourse.processing.org/t/how-to-create-a-vector-with-4-parameters/26509/1 "2020-12-25T12:48:19Z")

</div>

How do I create a vector with 4 varriables stored within? (position x & y, speed x & y)

so something like

```auto
//moving the ball
ball = new PVector( ball.x + ball.z, ball.y + ball.w, ball.z*friction, ball.w*friction);
circle(ball.x, ball.y, 5 , 5);

```

would be possible

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [December 25, 2020, 2:29pm UTC](https://discourse.processing.org/t/how-to-create-a-vector-with-4-parameters/26509/2 "2020-12-25T14:29:26Z")

</div>

As far as I am aware there is no built in class in Processing for that, but creating your own one should be no problem.

I don’t know how much experience you have with classes, but if you know the basics you could probably just create one containing two `PVectors` (one for the position and one for the speed) and then use that.😉

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 25, 2020, 4:06pm UTC](https://discourse.processing.org/t/how-to-create-a-vector-with-4-parameters/26509/3 "2020-12-25T16:06:34Z")

</div>

here is an example - see tutorials about objects:

see [https://www.processing.org/tutorials/objects/](https://www.processing.org/tutorials/objects/)

* * *

Example:

```auto
Ball ball = new Ball( 100, 100, 3, 5 );

void setup() {
  size(800, 800);
}

void draw() {
  background(0);
  ball.move(); 
  ball.display();
}

// ==============================================================

class Ball {

  float x, y; 
  float speedX, speedY; 

  //constr
  Ball(float x_, float y_, float speedX_, float speedY_) {
    x=x_;
    y=y_;

    speedX=speedX_;
    speedY=speedY_;
  }//constr

  void move() {
    x=x + speedX;
    y=y + speedY;
  }

  void display() {
    ellipse(x, y, 5, 5);
  }
  //
}//class
//

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 25, 2020, 5:13pm UTC](https://discourse.processing.org/t/how-to-create-a-vector-with-4-parameters/26509/4 "2020-12-25T17:13:32Z")

</div>

thanks for that.  
While I was experimenting I made a “bouncing darts” “game” (not really a game just a few darts flying around)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fa767c9713a38ca3ac2cf711bb5f323a147ff45a.png)

```auto
int w = 20, h= 20,scl=30;
P4vector grid[][] = new P4vector[w][h];

void setup() {
  size(600,600);
  for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
    grid[i][j] = new P4vector();
    float rDir = random(TWO_PI), speed = 10;
    grid[i][j].setStats( (i+0.5)*scl,(j+0.5)*scl,cos(rDir)*speed,sin(rDir)*speed);
  }
}

void draw() {
  background(0);
  stroke(255);
  for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
    grid[i][j].display();
  }
}
void keyPressed() {
  for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
    grid[i][j].move();
    grid[i][j].inBounds();
  }
}

class P4vector {
  float x,y,xs,ys;
  void setStats(float x_, float y_, float xs_, float ys_) {
    x = x_;
    y = y_;
    xs = xs_;
    ys = ys_;
  }
  void display() {
    ellipse(x,y,5,5);
    line(x,y,x + xs, y + ys);
  }
  void move() {
    x += xs;
    y += ys;
  }
  void inBounds() {
    if(x > width || x < 0) xs *= -1;
    if(y > height || y < 0) ys *= -1;
  }
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 25, 2020, 5:19pm UTC](https://discourse.processing.org/t/how-to-create-a-vector-with-4-parameters/26509/5 "2020-12-25T17:19:36Z")

</div>

```auto
int w = 20, h= 20,scl=30, speed = 10;
P4vector grid[][] = new P4vector[w][h];

void setup() {
  size(600,600);
  for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
    grid[i][j] = new P4vector();
    float rDir = random(TWO_PI);
    grid[i][j].setStats( (i+0.5)*scl,(j+0.5)*scl,cos(rDir)*speed,sin(rDir)*speed);
  }
}

void draw() {
  background(0);
  stroke(255);
  for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
    grid[i][j].display();
  }
}
void keyPressed() {
  for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
    grid[i][j].move();
    grid[i][j].inBounds();
  }
}
void mousePressed() {
  changeDir();
}
void changeDir() {
  for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
    float x = grid[i][j].x, y = grid[i][j].y;
    float dir = atan2(y-mouseY,x-mouseX);
    grid[i][j].xs = cos(dir)*10;
    grid[i][j].ys = sin(dir)*10;
  }
}

class P4vector {
  float x,y,xs,ys;
  void setStats(float x_, float y_, float xs_, float ys_) {
    x = x_;
    y = y_;
    xs = xs_;
    ys = ys_;
  }
  void display() {
    ellipse(x,y,5,5);
    line(x,y,x + xs, y + ys);
  }
  void move() {
    x += xs;
    y += ys;
  }
  void inBounds() {
    if(x > width || x < 0) xs *= -1;
    if(y > height || y < 0) ys *= -1;
  }
}

```
