I didn’t really explain it well, but even though i said magnet, they don’t have north or south poles. They just repel at a distance/ attract if far enough away. And the further they are the less the force is. In math terms it would be something like Repulsion = sqrt(distance) and attraction = 10/distance. So they would be equal at a certain point, in this case at 2.16~.
And this part i totally got to work, though the representation is still lacking, but it works. The problem is, that the 3rd object didn’t notice that it works… I’ll just add the latest code (i messed with it earlier and didn’t get it back to have the points really stand out… i think i did it with sqrt, but it did just say NaN after i tried to put it in again, and i just didn’t find a reason…)
Note that it’s not my most beatiful work, but after the 8th time redoing… well…
final PVector GRID_DISTANCE = new PVector(10, 10);
final PVector GRID_SIZE = new PVector(100, 50);
final PVector GRID_POSITION = new PVector(5, 5);
float[][] values = new float[int(GRID_SIZE.x)][int(GRID_SIZE.y)];
PVector[] points = {new PVector(40, 20), new PVector(30, 30), new PVector(50, 30), new PVector(10, 40)};
float valueMultiplier = 1;
String mode = "";
void setup() {
size(600, 400, P3D);
createGrid();
}
void draw() {
background(0);
fill(0);
text(valueMultiplier, GRID_SIZE.x*GRID_DISTANCE.x/2, GRID_SIZE.y*GRID_DISTANCE.y-220, 400);
fill(255);
continuousKeyEvent();
//rotateX(radians(45));
//translate(0, -200, -200);
camera(GRID_SIZE.x*GRID_DISTANCE.x/2, GRID_SIZE.y*GRID_DISTANCE.y, 600, GRID_SIZE.x*GRID_DISTANCE.x/2, GRID_SIZE.y*GRID_DISTANCE.y/2, 0, 0, 1, 0);
points[0].set(map(mouseX, 0, width, 0, GRID_SIZE.x), map(mouseY, 0, height, 0, GRID_SIZE.y));
createGrid();
updateMesh();
testMovement();
drawPoints();
println(getAverageValue());
println(values[(int)points[0].x][(int)points[0].y]);
}
float getAverageValue() {
float average = 0;
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
average += values[i][j];
}
}
average /= GRID_SIZE.x*GRID_SIZE.y;
return average;
}
void drawPoints() {
for (int i = 0; i < points.length; i++) {
translate(points[i].x*GRID_DISTANCE.x + GRID_POSITION.x, points[i].y*GRID_DISTANCE.y + GRID_POSITION.y, values[(int)points[i].x][(int)points[i].y]);
stroke(255, 0, 0);
sphere(5);
translate(-points[i].x*GRID_DISTANCE.x + GRID_POSITION.x, -points[i].y*GRID_DISTANCE.y + GRID_POSITION.y, -values[(int)points[i].x][(int)points[i].y]);
}
}
void testMovement() {
points[3].set(points[3].x + getMin(0), points[3].y + getMin(1));
points[3].set(constrain(points[3].x, 5, GRID_SIZE.x-5), constrain(points[3].y, 5, GRID_SIZE.y-5));
}
int getMin(int a) {
float min = values[(int)points[3].x][(int)points[3].y-1] < values[(int)points[3].x][(int)points[3].y] ? values[(int)points[3].x][(int)points[3].y-1] : values[(int)points[3].x][(int)points[3].y];
int x = 0;
int y = 1;
if (values[(int)points[3].x][(int)points[3].y+1] < min && values[(int)points[3].x][(int)points[3].y+1] < 50) {
min = values[(int)points[3].x][(int)points[3].y+1];
y = -1;
}
if (values[(int)points[3].x-1][(int)points[3].y] < min && values[(int)points[3].x-1][(int)points[3].y] < 50) {
min = values[(int)points[3].x-1][(int)points[3].y];
x = 1;
}
if (values[(int)points[3].x+1][(int)points[3].y] < min && values[(int)points[3].x+1][(int)points[3].y] < 50) {
min = values[(int)points[3].x+1][(int)points[3].y];
x = -1;
}
return a == 0 ? x : a == 1 ? y : 0;
}
void createGrid() {
stroke(0, 0, 255);
noFill();
for (int x = 0; x<GRID_SIZE.x; x++) {
beginShape();
for (int y = 0; y< GRID_SIZE.y; y++) {
//stroke(map(values[x][y], getAverageValues()-getAverageValues()/10, getAverageValues()+ getAverageValues()/10, 0,100)*2);
vertex( (x)*GRID_DISTANCE.x + GRID_POSITION.x, (y)*GRID_DISTANCE.y+GRID_POSITION.y, values[x][y]);
}
endShape();
}
for (int y = 0; y<GRID_SIZE.y; y++) {
beginShape();
for (int x = 0; x<GRID_SIZE.x; x++) {
vertex( (x)*GRID_DISTANCE.x+GRID_POSITION.x, (y)*GRID_DISTANCE.y+GRID_POSITION.y, values[x][y]);
}
endShape();
}
}
void updateMesh() {
for (int x=0; x < values.length; x++) {
for (int y=0; y < values[x].length; y++) {
values[x][y] = 0;
for (int i = 0; i < points.length; i++) {
values[x][y] -= dist(points[i].x, points[i].y, x, y);
}
values[x][y] = map(1/values[x][y], 0, 1, 0, -200);
values[x][y] *= valueMultiplier;
}
}
}
void continuousKeyEvent() {
if (keyPressed) {
if (!(key ==CODED)) {
switch (key) {
case 'm' :
mode = "Value Multiplier";
break;
case ENTER :
println("b");
break;
}
} else {
switch (keyCode) {
case UP :
if (mode.equals("Value Multiplier")) {
valueMultiplier += 0.1;
}
break;
case DOWN :
if (mode.equals("Value Multiplier")) {
valueMultiplier -= 0.1;
}
break;
}
}
}
}