Help with rendering height map.....
in
Programming Questions
•
2 years ago
Hey Guys,
I was hoping someone could help me. I've got a sketch (posted below) which calls 2 grids that react to attractors to create an undulating surface. However, I want to render it so I can show heights, i.e. deviation from the base of each grid, and this should change as the grid changes.
I guess it might be a case of creating a mesh and then putting in some variables so the colour changes from green to yellow for example depending on the points deviation from a base vector. If some one could give me some clues on creating and applying meshes (or if theres a better way) it would be really helpful.Please feel free to muck around with the script!!
Cheers, Farley
(ps, sorry if the scripts a bit of a mess!!)
// IMPORT EXTERNAL LIBRARIES
import processing.pdf.*;
import processing.opengl.*; // openGL library
import peasy.*; // the camera library
import controlP5.*; // the interface library
/*------------------------------------------------------------------
*** GLOBAL VARIABLES ***
------------------------------------------------------------------*/
// camera
PeasyCam theCamera;
boolean record = false;
// inteface
ControlP5 theGUI;
ControlWindow theGUIWindow;
// ArrayLists of stuff
ArrayList springList1 = new ArrayList();
ArrayList springList2 = new ArrayList();
// particle variables
int numParticlesX = 28;
int numParticlesY = 70;
// particle list
Particle[][] particleList1 = new Particle[numParticlesX][numParticlesY];
Particle[][] particleList2 = new Particle[numParticlesX][numParticlesY];
// x and y increments
int xInc = 12;
int yInc = 12;
// environment
int bBoxX = numParticlesX * xInc;
int bBoxY = numParticlesY * yInc;
int bBoxZ = 400;
// spring variables
//PVector gravity = new PVector(0, 0, 0);
float SPRING_RESTLENGTH = 8.0; // the length a spring is at rest
float SPRING_DAMPING = 0.3; // equiv hydraulic damping of a piston
// ArrayLists of attractors
ArrayList attList1 = new ArrayList();
ArrayList attList2 = new ArrayList();
ArrayList attList3 = new ArrayList();
int numAttractors = 12;
int attMAXHEIGHT = 168;
// declare meshes
Mesh bottomMesh;
Mesh topMesh;
/*------------------------------------------------------------------
*** GLOBAL SETUP ***
------------------------------------------------------------------*/
void setup() {
//----------general
size(900, 600, OPENGL);
background(0);
smooth();
//frameRate(100);
//----------make the camera
theCamera = new PeasyCam(this, bBoxX*1.65);
theCamera.lookAt(bBoxX/2, bBoxY/2, bBoxZ/2);
//----------setup interface
theGUI = new ControlP5(this);
theGUIWindow = theGUI.addControlWindow("GUI_WINDOW", 100, 100, 380, 50);
theGUIWindow.hideCoordinates();
Controller slider_springLength = theGUI.addSlider( "SPRING_RESTLENGTH", 1.0, 20.0, SPRING_RESTLENGTH, 15, 0, 250, 15 );
slider_springLength.setWindow(theGUIWindow);
Controller slider_springDamping = theGUI.addSlider( "SPRING_DAMPING", 1.0, 5.0, SPRING_DAMPING, 15, 35, 250, 15 );
slider_springDamping.setWindow(theGUIWindow);
//----------make mesh
initMesh();
}
/*------------------------------------------------------------------
*** GLOBAL CONTINUOUS DRAW ***
------------------------------------------------------------------*/
void draw() {
//----------general
background(250);
if (record) {
beginRaw(PDF, "3D####.pdf");
}
//----------visualize our bounding box
//showEnv();
//----------draw meshes
topMesh.createMesh();
topMesh.createSprings();
bottomMesh.createMesh();
bottomMesh.createSprings();
//----------draw attractors
for (int i = 0; i < numAttractors; ++i) {
Attractor myAtt = (Attractor) attList1.get(i);
myAtt.run();
}
for (int i = 0; i < numAttractors; ++i) {
Attractor myAtt = (Attractor) attList2.get(i);
myAtt.run();
}
for (int i = 0; i < 4; ++i) {
Attractor myAtt = (Attractor) attList3.get(i);
myAtt.run();
}
if (record) {
endRaw();
record = false;
}
}
/*-------------------------------------------------------------
*** MAKE PARTICLES, SPRINGS AND ATTRACTORS ***
--------------------------------------------------------------*/
void initMesh() {
bottomMesh = new Mesh(bBoxZ, particleList1, springList1, attList1, attList3);
topMesh = new Mesh(bBoxZ+bBoxX, particleList2, springList2, attList2, attList3);
}
/*----------------------------------
*** DRAW THE BOUNDING BOX
-----------------------------------*/
void showEnv() {
stroke(180);
strokeWeight(0.8);
noFill();
pushMatrix();
translate(bBoxX/2, bBoxY/2, bBoxZ);
box(bBoxX, bBoxY, bBoxZ/2);
popMatrix();
}
void keyPressed() {
if (key == 'p' || key == 'P') {
record = true;
}
}
//------------------------------------------------------------------------------------------------------------
class Attractor {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
PVector aPos;
PVector aVel;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Attractor(PVector aPos_, PVector aVel_) {
// assign all the class variables
aPos = aPos_;
aVel = aVel_;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
void run() {
move();
bounce();
//display();
}
void move() {
aPos.add(aVel);
}
void bounce() {
if (aPos.x > bBoxX) {
aVel.x *= -1;
}
if (aPos.x < 0) {
aVel.x *= -1;
}
if (aPos.y > bBoxY-150) {
aVel.y *= -1;
}
if (aPos.y < 150) {
aVel.y *= -1;
}
}
void display() {
strokeWeight(3);
stroke(255, 0, 0);
point(aPos.x, aPos.y, aPos.z);
}
}
//------------------------------------------------------------------------------------------------------------
class Mesh {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
float meshHeight;
Particle[][] particleList;
ArrayList springList;
Spring s0, s1;
ArrayList tmpAttList1;
ArrayList tmpAttList2;
Attractor tmpAtt;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Mesh(float meshHeight_, Particle[][] particleList_, ArrayList springList_, ArrayList attList_, ArrayList attList2_) {
meshHeight = meshHeight_;
particleList = particleList_;
springList = springList_;
tmpAttList1 = attList_;
tmpAttList2 = attList2_;
//-----initialise particles
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
// make a vector
PVector tmpPos = new PVector(i*xInc, j*yInc, meshHeight);
// make a particle
Particle newP = new Particle(tmpPos, tmpAttList1, tmpAttList2);
// fix perimeter particles
if ( (i < numParticlesX && j==0) || (i < numParticlesX && j==numParticlesY-1) ) {
newP.fixMe();
}
// store the particle in our list
particleList[i][j] = newP;
}
}
//-----initialise springs
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
if ( i>0 ) {
s0 = new Spring( particleList[i][j], particleList[i-1][j] );
//springList.add(s0);
particleList[i][j].addSpring(s0);
particleList[i-1][j].addSpring(s0);
}
if ( j>0 ) {
s1 = new Spring( particleList[i][j], particleList[i][j-1] );
springList.add(s1);
particleList[i][j].addSpring(s1);
particleList[i][j-1].addSpring(s1);
}
}
}
// print how many springs we made
println("Number of springs " + springList.size() );
//-----initialise surface attractors
for (int i = 0; i < numAttractors; ++i) {
//if (i < numAttractors) {
PVector aPos = new PVector(random(0, bBoxX), random(150, bBoxY-150), meshHeight);
PVector aVel = new PVector(random(1, 1.5), random(1, 2), 0);
tmpAtt = new Attractor(aPos, aVel);
tmpAttList1.add(tmpAtt);
}
//-----initialise middle attractors
for (int i = 0; i < 2; ++i) {
//if (i < numAttractors) {
PVector aPos = new PVector(random(0, bBoxX), random(150, bBoxY-150), 568);
PVector aVel = new PVector(random(1, 1.5), random(1, 2), 0);
tmpAtt = new Attractor(aPos, aVel);
tmpAttList2.add(tmpAtt);
}
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
void createMesh() {
//----------update particle physics
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
particleList[i][j].solveForce();
}
}
//----------update particle positions
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
particleList[i][j].update();
}
}
}
void createSprings() {
for (int i=0; i<springList.size(); i++) {
// get a spring from the arraylist
Spring currS = (Spring) springList.get(i);
currS.showMe();
}
}
}
//------------------------------------------------------------------------------------------------------------
class Particle {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
int pID;
PVector pos;
PVector vel;
PVector acc;
float pSize;
boolean pFixed;
ArrayList conSprings = new ArrayList();
ArrayList tmpAttList1;
ArrayList tmpAttList2;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Particle(PVector STARTPOS, ArrayList attList1_, ArrayList attList2_) {
// assign all the class variables
pos = STARTPOS;
vel = new PVector();
acc = new PVector();
pSize = 10.0;
pID = 0;
pFixed = false;
tmpAttList1 = attList1_;
tmpAttList2 = attList2_;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------MOVE AND DRAW TO SCREEN
void update() {
if (pFixed==false) {
updatePos();
attraction();
attractionB();
}
//showMe();
}
//----------SOLVE THE SPRINGS
void solveForce() {
if (pFixed==false) {
Particle otherP;
PVector sumForces = new PVector();
//sumForces.add(gravity);
sumForces.add(acc);
// loop thru our connections
for (int i = 0; i<conSprings.size(); ++i) {
// get a spring
Spring s = (Spring) conSprings.get(i);
// get other end of spring
if ( s.p00 == this ) {
otherP = s.p01;
}
else {
otherP = s.p00;
}
// calculate the push/pull of spring
PVector vecBet = PVector.sub(otherP.pos, pos);
float currLen = vecBet.mag();
float desiredLen = currLen-SPRING_RESTLENGTH;
vecBet.normalize();
vecBet.mult(desiredLen/2);
// add this force to our sum of forces
sumForces.add(vecBet);
}
// apply a damping to the force
sumForces.mult(SPRING_DAMPING);
// add the resultant force to our vel
vel.add(sumForces);
}
}
void updatePos() {
vel.limit(2);
pos.add(0, 0, vel.z);
acc = new PVector(0, 0, 0);
/*pos.add(vel); // move the particle
vel.mult(0); */ // reset the vel for next time
}
//----------A typical class function!!
void showMe() {
// set appearance
noFill();
if (pFixed==true) {
// draw as 'FIXED' geometry
strokeWeight(1);
stroke(0, 0, 250, 100);
line(pos.x-pSize, pos.y, pos.z, pos.x+pSize, pos.y, pos.z);
line(pos.x, pos.y-pSize, pos.z, pos.x, pos.y+pSize, pos.z);
line(pos.x, pos.y, pos.z-pSize, pos.x, pos.y, pos.z+pSize);
}
else {
// draw as 'FREE' geometry
strokeWeight(0.5);
stroke(100);
line(pos.x-pSize/2, pos.y, pos.z, pos.x+pSize/2, pos.y, pos.z);
line(pos.x, pos.y-pSize/2, pos.z, pos.x, pos.y+pSize/2, pos.z);
line(pos.x, pos.y, pos.z-pSize/2, pos.x, pos.y, pos.z+pSize/2);
}
}
//----------Make the particle fixed / free
void fixMe() {
pFixed=true;
}
void freeMe() {
pFixed=false;
}
//----------Add a spring to my list of springs
void addSpring(Spring S) {
// store this spring
conSprings.add(S);
}
void attraction() {
PVector steer = new PVector();
int count = 0;
for (int i = 0; i < tmpAttList1.size(); i++) {
// get a PVector from the list
Attractor tmpAtt = (Attractor) tmpAttList1.get(i);
float distance = PVector.dist(tmpAtt.aPos, pos);
if (distance > 0 && distance < attMAXHEIGHT+1) {
PVector vecBetween = PVector.sub(tmpAtt.aPos, pos);
// get the length of the vector
vecBetween.normalize();
steer.add(vecBetween);
count++;
}
}
if (count > 0) {
steer.mult(1.0/count);
}
steer.mult(2);
acc.add(steer);
}
void attractionB() {
PVector steer = new PVector();
int count = 0;
for (int i = 0; i < tmpAttList2.size(); i++) {
// get a PVector from the list
Attractor tmpAtt = (Attractor) tmpAttList2.get(i);
float distance = PVector.dist(tmpAtt.aPos, pos);
if (distance > 0 && distance < attMAXHEIGHT+1) {
PVector vecBetween = PVector.sub(tmpAtt.aPos, pos);
// get the length of the vector
vecBetween.normalize();
steer.add(vecBetween);
count++;
}
}
if (count > 0) {
steer.mult(1.0/count);
}
steer.mult(2);
acc.add(steer);
}
}
//------------------------------------------------------------------------------------------------------------
class Spring {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
// declare all the class variables
Particle p00;
Particle p01;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Spring (Particle INPUT_00, Particle INPUT_01) {
// assign all the class variables
p00 = INPUT_00;
p01 = INPUT_01;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------Draw myself
void showMe() {
stroke(180);
strokeWeight(0.5);
noFill();
line(
p00.pos.x, p00.pos.y, p00.pos.z,
p01.pos.x, p01.pos.y, p01.pos.z
);
}
}
I was hoping someone could help me. I've got a sketch (posted below) which calls 2 grids that react to attractors to create an undulating surface. However, I want to render it so I can show heights, i.e. deviation from the base of each grid, and this should change as the grid changes.
I guess it might be a case of creating a mesh and then putting in some variables so the colour changes from green to yellow for example depending on the points deviation from a base vector. If some one could give me some clues on creating and applying meshes (or if theres a better way) it would be really helpful.Please feel free to muck around with the script!!
Cheers, Farley
(ps, sorry if the scripts a bit of a mess!!)
// IMPORT EXTERNAL LIBRARIES
import processing.pdf.*;
import processing.opengl.*; // openGL library
import peasy.*; // the camera library
import controlP5.*; // the interface library
/*------------------------------------------------------------------
*** GLOBAL VARIABLES ***
------------------------------------------------------------------*/
// camera
PeasyCam theCamera;
boolean record = false;
// inteface
ControlP5 theGUI;
ControlWindow theGUIWindow;
// ArrayLists of stuff
ArrayList springList1 = new ArrayList();
ArrayList springList2 = new ArrayList();
// particle variables
int numParticlesX = 28;
int numParticlesY = 70;
// particle list
Particle[][] particleList1 = new Particle[numParticlesX][numParticlesY];
Particle[][] particleList2 = new Particle[numParticlesX][numParticlesY];
// x and y increments
int xInc = 12;
int yInc = 12;
// environment
int bBoxX = numParticlesX * xInc;
int bBoxY = numParticlesY * yInc;
int bBoxZ = 400;
// spring variables
//PVector gravity = new PVector(0, 0, 0);
float SPRING_RESTLENGTH = 8.0; // the length a spring is at rest
float SPRING_DAMPING = 0.3; // equiv hydraulic damping of a piston
// ArrayLists of attractors
ArrayList attList1 = new ArrayList();
ArrayList attList2 = new ArrayList();
ArrayList attList3 = new ArrayList();
int numAttractors = 12;
int attMAXHEIGHT = 168;
// declare meshes
Mesh bottomMesh;
Mesh topMesh;
/*------------------------------------------------------------------
*** GLOBAL SETUP ***
------------------------------------------------------------------*/
void setup() {
//----------general
size(900, 600, OPENGL);
background(0);
smooth();
//frameRate(100);
//----------make the camera
theCamera = new PeasyCam(this, bBoxX*1.65);
theCamera.lookAt(bBoxX/2, bBoxY/2, bBoxZ/2);
//----------setup interface
theGUI = new ControlP5(this);
theGUIWindow = theGUI.addControlWindow("GUI_WINDOW", 100, 100, 380, 50);
theGUIWindow.hideCoordinates();
Controller slider_springLength = theGUI.addSlider( "SPRING_RESTLENGTH", 1.0, 20.0, SPRING_RESTLENGTH, 15, 0, 250, 15 );
slider_springLength.setWindow(theGUIWindow);
Controller slider_springDamping = theGUI.addSlider( "SPRING_DAMPING", 1.0, 5.0, SPRING_DAMPING, 15, 35, 250, 15 );
slider_springDamping.setWindow(theGUIWindow);
//----------make mesh
initMesh();
}
/*------------------------------------------------------------------
*** GLOBAL CONTINUOUS DRAW ***
------------------------------------------------------------------*/
void draw() {
//----------general
background(250);
if (record) {
beginRaw(PDF, "3D####.pdf");
}
//----------visualize our bounding box
//showEnv();
//----------draw meshes
topMesh.createMesh();
topMesh.createSprings();
bottomMesh.createMesh();
bottomMesh.createSprings();
//----------draw attractors
for (int i = 0; i < numAttractors; ++i) {
Attractor myAtt = (Attractor) attList1.get(i);
myAtt.run();
}
for (int i = 0; i < numAttractors; ++i) {
Attractor myAtt = (Attractor) attList2.get(i);
myAtt.run();
}
for (int i = 0; i < 4; ++i) {
Attractor myAtt = (Attractor) attList3.get(i);
myAtt.run();
}
if (record) {
endRaw();
record = false;
}
}
/*-------------------------------------------------------------
*** MAKE PARTICLES, SPRINGS AND ATTRACTORS ***
--------------------------------------------------------------*/
void initMesh() {
bottomMesh = new Mesh(bBoxZ, particleList1, springList1, attList1, attList3);
topMesh = new Mesh(bBoxZ+bBoxX, particleList2, springList2, attList2, attList3);
}
/*----------------------------------
*** DRAW THE BOUNDING BOX
-----------------------------------*/
void showEnv() {
stroke(180);
strokeWeight(0.8);
noFill();
pushMatrix();
translate(bBoxX/2, bBoxY/2, bBoxZ);
box(bBoxX, bBoxY, bBoxZ/2);
popMatrix();
}
void keyPressed() {
if (key == 'p' || key == 'P') {
record = true;
}
}
//------------------------------------------------------------------------------------------------------------
class Attractor {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
PVector aPos;
PVector aVel;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Attractor(PVector aPos_, PVector aVel_) {
// assign all the class variables
aPos = aPos_;
aVel = aVel_;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
void run() {
move();
bounce();
//display();
}
void move() {
aPos.add(aVel);
}
void bounce() {
if (aPos.x > bBoxX) {
aVel.x *= -1;
}
if (aPos.x < 0) {
aVel.x *= -1;
}
if (aPos.y > bBoxY-150) {
aVel.y *= -1;
}
if (aPos.y < 150) {
aVel.y *= -1;
}
}
void display() {
strokeWeight(3);
stroke(255, 0, 0);
point(aPos.x, aPos.y, aPos.z);
}
}
//------------------------------------------------------------------------------------------------------------
class Mesh {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
float meshHeight;
Particle[][] particleList;
ArrayList springList;
Spring s0, s1;
ArrayList tmpAttList1;
ArrayList tmpAttList2;
Attractor tmpAtt;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Mesh(float meshHeight_, Particle[][] particleList_, ArrayList springList_, ArrayList attList_, ArrayList attList2_) {
meshHeight = meshHeight_;
particleList = particleList_;
springList = springList_;
tmpAttList1 = attList_;
tmpAttList2 = attList2_;
//-----initialise particles
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
// make a vector
PVector tmpPos = new PVector(i*xInc, j*yInc, meshHeight);
// make a particle
Particle newP = new Particle(tmpPos, tmpAttList1, tmpAttList2);
// fix perimeter particles
if ( (i < numParticlesX && j==0) || (i < numParticlesX && j==numParticlesY-1) ) {
newP.fixMe();
}
// store the particle in our list
particleList[i][j] = newP;
}
}
//-----initialise springs
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
if ( i>0 ) {
s0 = new Spring( particleList[i][j], particleList[i-1][j] );
//springList.add(s0);
particleList[i][j].addSpring(s0);
particleList[i-1][j].addSpring(s0);
}
if ( j>0 ) {
s1 = new Spring( particleList[i][j], particleList[i][j-1] );
springList.add(s1);
particleList[i][j].addSpring(s1);
particleList[i][j-1].addSpring(s1);
}
}
}
// print how many springs we made
println("Number of springs " + springList.size() );
//-----initialise surface attractors
for (int i = 0; i < numAttractors; ++i) {
//if (i < numAttractors) {
PVector aPos = new PVector(random(0, bBoxX), random(150, bBoxY-150), meshHeight);
PVector aVel = new PVector(random(1, 1.5), random(1, 2), 0);
tmpAtt = new Attractor(aPos, aVel);
tmpAttList1.add(tmpAtt);
}
//-----initialise middle attractors
for (int i = 0; i < 2; ++i) {
//if (i < numAttractors) {
PVector aPos = new PVector(random(0, bBoxX), random(150, bBoxY-150), 568);
PVector aVel = new PVector(random(1, 1.5), random(1, 2), 0);
tmpAtt = new Attractor(aPos, aVel);
tmpAttList2.add(tmpAtt);
}
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
void createMesh() {
//----------update particle physics
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
particleList[i][j].solveForce();
}
}
//----------update particle positions
for (int i=0; i<numParticlesX; ++i) {
for (int j=0; j<numParticlesY; ++j) {
particleList[i][j].update();
}
}
}
void createSprings() {
for (int i=0; i<springList.size(); i++) {
// get a spring from the arraylist
Spring currS = (Spring) springList.get(i);
currS.showMe();
}
}
}
//------------------------------------------------------------------------------------------------------------
class Particle {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
int pID;
PVector pos;
PVector vel;
PVector acc;
float pSize;
boolean pFixed;
ArrayList conSprings = new ArrayList();
ArrayList tmpAttList1;
ArrayList tmpAttList2;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Particle(PVector STARTPOS, ArrayList attList1_, ArrayList attList2_) {
// assign all the class variables
pos = STARTPOS;
vel = new PVector();
acc = new PVector();
pSize = 10.0;
pID = 0;
pFixed = false;
tmpAttList1 = attList1_;
tmpAttList2 = attList2_;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------MOVE AND DRAW TO SCREEN
void update() {
if (pFixed==false) {
updatePos();
attraction();
attractionB();
}
//showMe();
}
//----------SOLVE THE SPRINGS
void solveForce() {
if (pFixed==false) {
Particle otherP;
PVector sumForces = new PVector();
//sumForces.add(gravity);
sumForces.add(acc);
// loop thru our connections
for (int i = 0; i<conSprings.size(); ++i) {
// get a spring
Spring s = (Spring) conSprings.get(i);
// get other end of spring
if ( s.p00 == this ) {
otherP = s.p01;
}
else {
otherP = s.p00;
}
// calculate the push/pull of spring
PVector vecBet = PVector.sub(otherP.pos, pos);
float currLen = vecBet.mag();
float desiredLen = currLen-SPRING_RESTLENGTH;
vecBet.normalize();
vecBet.mult(desiredLen/2);
// add this force to our sum of forces
sumForces.add(vecBet);
}
// apply a damping to the force
sumForces.mult(SPRING_DAMPING);
// add the resultant force to our vel
vel.add(sumForces);
}
}
void updatePos() {
vel.limit(2);
pos.add(0, 0, vel.z);
acc = new PVector(0, 0, 0);
/*pos.add(vel); // move the particle
vel.mult(0); */ // reset the vel for next time
}
//----------A typical class function!!
void showMe() {
// set appearance
noFill();
if (pFixed==true) {
// draw as 'FIXED' geometry
strokeWeight(1);
stroke(0, 0, 250, 100);
line(pos.x-pSize, pos.y, pos.z, pos.x+pSize, pos.y, pos.z);
line(pos.x, pos.y-pSize, pos.z, pos.x, pos.y+pSize, pos.z);
line(pos.x, pos.y, pos.z-pSize, pos.x, pos.y, pos.z+pSize);
}
else {
// draw as 'FREE' geometry
strokeWeight(0.5);
stroke(100);
line(pos.x-pSize/2, pos.y, pos.z, pos.x+pSize/2, pos.y, pos.z);
line(pos.x, pos.y-pSize/2, pos.z, pos.x, pos.y+pSize/2, pos.z);
line(pos.x, pos.y, pos.z-pSize/2, pos.x, pos.y, pos.z+pSize/2);
}
}
//----------Make the particle fixed / free
void fixMe() {
pFixed=true;
}
void freeMe() {
pFixed=false;
}
//----------Add a spring to my list of springs
void addSpring(Spring S) {
// store this spring
conSprings.add(S);
}
void attraction() {
PVector steer = new PVector();
int count = 0;
for (int i = 0; i < tmpAttList1.size(); i++) {
// get a PVector from the list
Attractor tmpAtt = (Attractor) tmpAttList1.get(i);
float distance = PVector.dist(tmpAtt.aPos, pos);
if (distance > 0 && distance < attMAXHEIGHT+1) {
PVector vecBetween = PVector.sub(tmpAtt.aPos, pos);
// get the length of the vector
vecBetween.normalize();
steer.add(vecBetween);
count++;
}
}
if (count > 0) {
steer.mult(1.0/count);
}
steer.mult(2);
acc.add(steer);
}
void attractionB() {
PVector steer = new PVector();
int count = 0;
for (int i = 0; i < tmpAttList2.size(); i++) {
// get a PVector from the list
Attractor tmpAtt = (Attractor) tmpAttList2.get(i);
float distance = PVector.dist(tmpAtt.aPos, pos);
if (distance > 0 && distance < attMAXHEIGHT+1) {
PVector vecBetween = PVector.sub(tmpAtt.aPos, pos);
// get the length of the vector
vecBetween.normalize();
steer.add(vecBetween);
count++;
}
}
if (count > 0) {
steer.mult(1.0/count);
}
steer.mult(2);
acc.add(steer);
}
}
//------------------------------------------------------------------------------------------------------------
class Spring {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
// declare all the class variables
Particle p00;
Particle p01;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Spring (Particle INPUT_00, Particle INPUT_01) {
// assign all the class variables
p00 = INPUT_00;
p01 = INPUT_01;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------Draw myself
void showMe() {
stroke(180);
strokeWeight(0.5);
noFill();
line(
p00.pos.x, p00.pos.y, p00.pos.z,
p01.pos.x, p01.pos.y, p01.pos.z
);
}
}
1