We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want to simulate a river stream eventually. That means I want to have a board with a vector in each field. For now I'm trying to make the "red dots" go towards random vectors. Well, I know how to make the dots follow the mouse but not the vectors. I'd also like to visualize the vectors as black circles (for now, so i can check their position visually). I also used to have a vector class, where i already visualized the vectors as i wished to. Though it was an Array List and absolute -> not ideal for my goal. So i cut it out for now.
Thank you in advance :)
Here is my code:
ArrayList mikroplastikParticles; // set of Mikroplastik Particles
int numOfMikroplastikPerField = 1;
int boardSize = 7; // board to be filled with vectors
int board [][] = new int [boardSize][boardSize];
PVector vectorSet [][] = new PVector [boardSize][boardSize];
int cellWidth; // Size of the fields
final int mikroplastikSize = 10;
final int VectorSize = 10;
int randomVectorPosition;
float mikroplastikSpeed = 1.;
void setup() {
// basic Setup
size(700, 700);
smooth();
rectMode(CORNER);
ellipseMode(CENTER);
cellWidth = width/boardSize; // width of the cell dependens on the size of the board
mikroplastikParticles = new ArrayList();
// visualization of the board (understanding purpose only)
fill(255, 255, 255);
stroke(0);
for (int r = 0; r<boardSize; r++) {
for (int c = 0; c<boardSize; c++) {
rect(r * cellWidth, c * cellWidth, cellWidth, cellWidth);
}
}
// now we start to initialize the particles
for (int r = 0; r<boardSize; r++) // we want to fill every single field
for (int c = 0; c<boardSize; c++)
for (int anz = 0; anz < numOfMikroplastikPerField; anz++) // we can choose how many particles per field we want with "numOfMikroplastikPerField"
mikroplastikParticles.add(new Mikroplastik(r*cellWidth+random(cellWidth), c*cellWidth+ random(cellWidth))); // mikroplastik per field gets added
// setting up the vectors
for (int x = 0; x<boardSize; x++) {
for (int y = 0; y<boardSize; y++) {
randomVectorPosition = (int)random(2);
if (randomVectorPosition == 0)
vectorSet[x][y] = new PVector(0, random(cellWidth));
else
vectorSet[x][y] = new PVector(random(cellWidth), 0);
}
}
}
void draw() {
background(255);
for (int i=0; i < boardSize*boardSize*numOfMikroplastikPerField; i++) {
Mikroplastik myMikroplastik = (Mikroplastik)mikroplastikParticles.get(i); // getting all vectors from the array
myMikroplastik.display(); // assigning a "to do" for the vectors
}
}
class Mikroplastik { // the actual mikroplastik class
PVector mikroplastik = new PVector(0, 0); // the mikroplastik is a vector object
PVector sum = new PVector(0, 0);
Mikroplastik(float tempX, float tempY) {
mikroplastik.set(tempX, tempY);
}
void display()
{ // how the mikroplastik looks like
fill(#FF0000);
noStroke();
ellipse(mikroplastik.x, mikroplastik.y, mikroplastikSize, mikroplastikSize);
// PVector mouse = Vector.vector(tempX, tempY);
PVector mouse = new PVector(mouseX, mouseY);
mouse.sub(mikroplastik);
mouse.mult(0.05);
sum.add(mouse);
sum.normalize();
sum.mult(mikroplastikSpeed);
mikroplastik.add(sum);
}
}
Answers
Select the code you have posted. Open a new window in Processing. Paste it. Run it. Whoops! Can you fix it? We don't want to.
Try posting a version of your code that actually runs.
OK fixed it. Sorry, I missed some important parts of the code when copying it :D
A vector is just a direction. There is no way to make a dot "go towards" a vector, because a vector doesn't have a position - it's just a direction. Do you mean you want the dots to move in the same direction as a vector if they're in a certain area? Something like this?
Yes that's pretty much what i meant :) . Since it was all self taught I had a little problem grasping the use of vectors in Processing. From school I remember them to be the shortest distance of to points - so I figured I could just create two points and then make them move towards each other (like i can make an objects follow the mouse) :D Well anyway. I can certainly work with what you presented. Thanks for you time ;) and this is what i made out of it (it's supposed to simulate the distribution of particles in water)