 # How to create a vector with 4 parameters?

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

so something like

``````//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

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. here is an example - see tutorials about objects:

Example:

``````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
//

``````

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

``````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;
}
}
``````
``````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;
}
}
``````
1 Like