I‘ve been having a bit of trouble with this for a while and finally found a solution (more or less).
What i‘m trying to achieve is basically something like a Grid-based Wave Simulation (only that waves would propagate in a curved way, while i try to achieve straight motion, like a particle/wall of particles [instead of ‚)‘ it should be ‚l‘… that‘s the best visual representation i can offer right now ]).
Now, the problem lies within a „Formula“ i came up with for this… it works in Desmos, and it should at the very least not behave the way it does for me…
Because, the way it moves a particle is fine when the vector‘s floats are either -1, 1 or 0, but not any other number (float or int)… in that case, the particle moves correctly for a while, until it splits into horizonal and vertical motion in the same direction, so if it started (0.8,0.2) it ends up in the middle right and bottom, with the brighter part being right (in a 4:1 (0.8:0.2) ratio, so at least it somehow kept this…).
What it is supposed to do, is to continue moving and reaching or passing (if started at 50, 50) the position (90,60) at some point, which is impossible if it splits up…
And i just can‘t find the reason for it…
If anyone could point me in the right direction, it‘d be very much appreciated
As a reference, here‘s the part of the code together with the parts that will display the grid :
Summary
Take Note, that this is written in the iCompiler (iOS) so things like casting, nullpointers in arrays and others are handeled more or less automatically (access on indexoutofbounds just creates new indexes) so there might be things that cause errors… but there shouldn‘t i hope
//the method that updates the grid
void update() {
PVector[][] dir2 = new PVector[dir.length][dir[0].length];
for (int x = 0; x < dir2.length; x++) {
for (int y = 0; y < dir2[x].length; y++) {
dir2[x][y] = new PVector(0,0);
}
}
for (int x = 1; x < dir.length-1; x++) {
for (int y = 1; y < dir[x].length-1; y++) {
float a = dir[x][y].x;
float b = dir[x][y].y;
//my Formulae
dir2[x][y+1].x += a*(b>0?b:0);
dir2[x][y+1].y += (1-abs(a))*(b>0?b:0);
dir2[x][y-1].x += -a*(b<0?b:0);
dir2[x][y-1].y += (1-abs(a))*(b<0?b:0);
dir2[x+1][y].x += (1-abs(b))*(a>0?a:0);
dir2[x+1][y].y += b*(a>0?a:0);
dir2[x-1][y].x += (1-abs(b))*(a<0?a:0);
dir2[x-1][y].y += -b*(a<0?a:0);
}
}
for (int x = 1; x < dir.length-1; x++) {
for (int y = 1; y < dir[x].length-1; y++) {
dir[x][y].set(dir2[x][y].x, dir2[x][y].y,0);
}
}
}
The Grid (the update happens in an extended class, but that‘s just way too long, but doesn‘t do anything to this process, so i’ll just post the basic class)
class Grid {
PVector pos;
PVector[][] dir;
Grid(float px, float py, int w, int h) {
pos = new PVector(px, py);
dir = new PVector[w][h];
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
dir[x][y] = new PVector(0,0);
}
}
}
void update() {
}
void display() {
for (int x = 0; x < dir.length; x++) {
for (int y = 0; y < dir[x].length; y++) {
stroke(dir[x][y].mag() * 180);
point(pos.x+x, pos.y+y);
}
}
}
}
And for the rest just use this :
Grid g;
void setup() {
g = new Grid(50,50,100,100);
g.dir[50][50].set(0.8,0.2,0);
}
void draw() {
g.update();
g.display();
}