Hey thanks, im reading the objects part, by other way u could mix the grid and the objects but, how i store the potential nodes in that grid?
please look at and tell me what you thing, i dont know if i´m working at wring way.
// sistema de atraccion
//int num = 1; //how many particles we'll have in the system. More particles = slower sketch.
//Particle[] particle = new Particle[num]; //Initialise array of particles of length "num"
//
Cell[][] _cellArray;
int _cellSize = 20;
int _numX, _numY;
//las cosntantes de los campor de repuslion y atraccion
int margenD1; //constantes de las ecuaciones de los campos potenciales
int necesidadE1;
int campoDo;
int repulsionN;
// el objeto y el atractor
Thing t;
Attractor a;
boolean showVectors = true;
void setup() {
size(800, 600);
// del sistema de atraccion
smooth(); //turn on anti-aliasing
noStroke();
background(255);
//vector de lu
PVector ac = new PVector(0.0,0.0);
PVector ve = new PVector(0.0,1.0);
PVector lo = new PVector(50,50);
// Create new thing with some initial settings
t = new Thing(ac,ve,lo,10);
// Create an attractive body
a = new Attractor(new PVector(width/2,height/2),20,0.4);
_numX = floor(width/_cellSize);
_numY = floor(height/_cellSize);
matriz();
}
void matriz() {
_cellArray = new Cell[_numX][_numY];
for (int x = 0; x<_numX; x++) {
for (int y = 0; y<_numY; y++) {
Cell newCell = new Cell(x, y);
_cellArray[x][y] = newCell;
}
}
}
void draw() {
background(200);
for (int x = 0; x < _numX; x++) {
for (int y = 0; y < _numY; y++) {
}
}
translate(_cellSize/2, _cellSize/2);
for (int x = 0; x < _numX; x++) {
for (int y = 0; y < _numY; y++) {
_cellArray[x][y].drawMe();
}
}
a.rollover(mouseX,mouseY);
a.go();
// Calculate a force exerted by "attractor" on "thing"
PVector f = a.calcGravForce(t);
// Apply that force to the thing
t.applyForce(f);
// Update and render the positions of both objects
t.go();
}
void mousePressed() {
a.clicked(mouseX,mouseY);
}
void mouseReleased() {
a.stopDragging();
}
void keyPressed() {
showVectors = !showVectors;
}
// Renders a vector object 'v' as an arrow and a location 'loc'
void drawVector(PVector v, PVector loc, float scayl) {
if (v.mag() > 0.0) {
pushMatrix();
float arrowsize = 4;
// Translate to location to render vector
translate(loc.x,loc.y);
stroke(0);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading2D());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0,0,len,0);
line(len,0,len-arrowsize,+arrowsize/2);
line(len,0,len-arrowsize,-arrowsize/2);
popMatrix();
}
}
// Attraction
// Daniel Shiffman <
http://www.shiffman.net>
// A class for a draggable attractive body in our world
class Attractor {
float mass; // Mass, tied to size
float G; // Gravitational Constant
PVector loc; // Location
boolean dragging = false; // Is the object being dragged?
boolean rollover = false; // Is the mouse over the ellipse?
PVector drag; // holds the offset for when object is clicked on
Attractor(PVector l_,float m_, float g_) {
loc = l_.get();
mass = m_;
G = g_;
drag = new PVector(0.0,0.0);
}
void go() {
render();
drag();
}
PVector calcGravForce(Thing t) {
PVector dir = PVector.sub(loc,t.getLoc()); // Calculate direction of force
float d = dir.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
dir.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float force = (G * mass * t.getMass()) / (d * d); // Calculate gravitional force magnitude
dir.mult(force); // Get force vector --> magnitude * direction
return dir;
}
// Method to display
void render() {
ellipseMode(CENTER);
stroke(0);
if (dragging) fill (50);
else if (rollover) fill(100);
else fill(175,200);
ellipse(loc.x,loc.y,mass*2,mass*2);
}
// The methods below are for mouse interaction
void clicked(int mx, int my) {
float d = dist(mx,my,loc.x,loc.y);
if (d < mass) {
dragging = true;
drag.x = loc.x-mx;
drag.y = loc.y-my;
}
}
void rollover(int mx, int my) {
float d = dist(mx,my,loc.x,loc.y);
if (d < mass) {
rollover = true;
} else {
rollover = false;
}
}
void stopDragging() {
dragging = false;
}
void drag() {
if (dragging) {
loc.x = mouseX + drag.x;
loc.y = mouseY + drag.y;
}
}
}
// Attraction
// Daniel Shiffman <
http://www.shiffman.net>
// A class to describe a thing in our world, has vectors for location, velocity, and acceleration
// Also includes scalar values for mass, maximum velocity, and elasticity
class Thing {
PVector loc;
PVector vel;
PVector acc;
float mass;
float max_vel;
float bounce = 1.0; // How "elastic" is the object
Thing(PVector a, PVector v, PVector l, float m_) {
acc = a.get();
vel = v.get();
loc = l.get();
mass = m_;
max_vel = 20.0;
}
PVector getLoc() {
return loc;
}
PVector getVel() {
return vel;
}
float getMass() {
return mass;
}
void applyForce(PVector force) {
force.div(mass);
acc.add(force);
if (showVectors) {
drawVector(force,loc,1000);
}
}
// Main method to operate object
void go() {
update();
render();
}
// Method to update location
void update() {
vel.add(acc);
vel.limit(max_vel);
loc.add(vel);
// Multiplying by 0 sets the all the components to 0
acc.mult(0);
}
// Method to display
void render() {
ellipseMode(CENTER);
stroke(0);
fill(175,100);
ellipse(loc.x,loc.y,mass*2,mass*2);
if (showVectors) {
drawVector(vel,loc,20);
}
}
}
//================================= the grid
class Cell {
float x, y;
Cell(float ex, float why) {
x = ex * _cellSize;
y = why * _cellSize;
}
void drawMe() {
stroke(0);
fill(255);
fill(255);
rect(x, y, _cellSize, _cellSize);
}
}