Was just a quick and dirty approach to show the idea.
But of course this could be applied to a sketch that uses classes.
I did some minor changes to your code, the main idea is, to use a repellor (defined by mouseX in this case and marked as small green rectangle).
Before displaying the points, you update their y-postion depending on their distance to the repellor.
- import peasy.*;
import toxi.geom.*;
PeasyCam cam;
int cols = 100;
int rows = 100;
float gridSize = 10;
float force = 1000;
Vec3D repellor= new Vec3D(0, -20, 0);
CA grid [][] = new CA[cols][rows];
void setup () {
size (800, 800, P3D);
cam = new PeasyCam(this, 400);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
Vec3D ptLoc = new Vec3D(i*gridSize, j*gridSize, 0);
grid[i][j] = new CA(ptLoc, i, j);
}
}
}
void draw () {
background(0);
stroke (255);
fill(255, 20);
// calculate and show the location from where the force acts
repellor.x = map(mouseX, 0, width, 0, cols*gridSize);
stroke(0, 250, 0);
strokeWeight(10);
point(repellor.x, repellor.y, repellor.z);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// update positions of points
grid[i][j].update(repellor);
grid[i][j].run();
}
}
}
class CA {
Vec3D loc;
float currentY;
int x;
int y;
CA(Vec3D _loc, int _x, int _y) {
loc = _loc;
currentY = loc.y;
x = _x;
y = _y;
}
void run() {
display();
}
void update(Vec3D repelV) {
// update y-coordinate, depending on distance to repellor
float distance = loc.distanceTo(repelV);
currentY = loc.y + (force)/sqrt(distance);
}
void display() {
stroke(255);
strokeWeight(2);
point (loc.x, currentY, loc.z);
if (x == 99 & y == 0) {
stroke(255, 0, 0);
strokeWeight(10);
point (loc.x, currentY, loc.z);
}
}
}