Whats`s wrong PVector
in
Programming Questions
•
2 years ago
I am trying to create this sequecence of lines which are changed by attractors, but I don`t know why this is not working.
//libraries
//import processing.opengl.*;
//import superCAD.*;
//import superCAD.*;
String cadSoftware, ext;
boolean record = false;
boolean record = false;
/////////////////////////////////////////////
int bgc = 0;
ForceLine[] lines = new ForceLine[40];
Attractor[] attractor = new Attractor[3];
int G = 10000000;
/////////////////////////////////////////////////////
void setup() {
void setup() {
size(1000, 1000,P3D);
background(bgc);
smooth();
noLoop();
background(bgc);
smooth();
noLoop();
for (int i=0; i < attractor.length; i++) {
attractor[i] = new Attractor( width/2, height/2, 0);
}
///////////////////////////////////////////////////
for (int i = 0; i < lines.length; i++) {
lines[i] = new ForceLine (0,0,i*(-100));
}
}
attractor[i] = new Attractor( width/2, height/2, 0);
}
///////////////////////////////////////////////////
for (int i = 0; i < lines.length; i++) {
lines[i] = new ForceLine (0,0,i*(-100));
}
}
void draw() {
background(bgc);
if(record)
beginRaw("superCAD."+cadSoftware, "output."+ext);
attractor[0].x = 450;
attractor[0].y = 0;
attractor[0].z = 0;
attractor[0].update();
attractor[1].x = 0;
attractor[1].y = 450;
attractor[1].z = 0;
attractor[1].update();
attractor[2].x = 800;
attractor[2].y = 450;
attractor[2].z =0;
attractor[2].update();
////////////////////////
background(bgc);
if(record)
beginRaw("superCAD."+cadSoftware, "output."+ext);
attractor[0].x = 450;
attractor[0].y = 0;
attractor[0].z = 0;
attractor[0].update();
attractor[1].x = 0;
attractor[1].y = 450;
attractor[1].z = 0;
attractor[1].update();
attractor[2].x = 800;
attractor[2].y = 450;
attractor[2].z =0;
attractor[2].update();
////////////////////////
for (int i=0; i < lines.length; i++) {
lines[i].update();
}
lines[i].update();
}
if (record) {
endRaw();
record = false;
}
}
void keyPressed() {
switch(key){
case 'r':
cadSoftware = "Rhino";
ext = "rvb";
break;
case 's':
cadSoftware = "SketchUP";
ext = "rb";
break;
case 'a':
cadSoftware = "AutoLISP";
ext = "lsp";
break;
case 'p':
cadSoftware = "PovRAY";
ext = "pov";
break;
case 'm':
cadSoftware = "Maya";
ext = "mel";
break;
case 'o':
cadSoftware = "ObjFile";
ext = "obj";
break;
case 'c':
cadSoftware = "ArchiCAD";
ext = "gdl";
break;
case 'r':
cadSoftware = "Rhino";
ext = "rvb";
break;
case 's':
cadSoftware = "SketchUP";
ext = "rb";
break;
case 'a':
cadSoftware = "AutoLISP";
ext = "lsp";
break;
case 'p':
cadSoftware = "PovRAY";
ext = "pov";
break;
case 'm':
cadSoftware = "Maya";
ext = "mel";
break;
case 'o':
cadSoftware = "ObjFile";
ext = "obj";
break;
case 'c':
cadSoftware = "ArchiCAD";
ext = "gdl";
break;
}
record = true;
}
void mousePressed() {
redraw();
}
redraw();
}
/////////////////////////////////////////////////////
class ForceLine {
PVector a;
float ix, iy,iz;
Ball[] balls = new Ball[11];
ForceLine (float _x, float _y, float _z) {
a = new PVector(_x,_y,_z);
for (int i=0; i <balls.length; i++) {
ix =round (_x+(i*100));
iy = round(_y);
iz=round(_z+ 0);
balls[i] = new Ball(ix, iy,iz);
}
}
// ball functions
void update() {
for (int i=0; i <balls.length; i++) {
balls[i].update();
}
render();
}
void render() {
stroke(255,0,0);
PVector a;
float ix, iy,iz;
Ball[] balls = new Ball[11];
ForceLine (float _x, float _y, float _z) {
a = new PVector(_x,_y,_z);
for (int i=0; i <balls.length; i++) {
ix =round (_x+(i*100));
iy = round(_y);
iz=round(_z+ 0);
balls[i] = new Ball(ix, iy,iz);
}
}
// ball functions
void update() {
for (int i=0; i <balls.length; i++) {
balls[i].update();
}
render();
}
void render() {
stroke(255,0,0);
noFill();
beginShape();
for (int i=0; i <balls.length; i++) {
curveVertex (balls[i].a.x,balls[i].a.y,balls[i].a.z );
beginShape();
for (int i=0; i <balls.length; i++) {
curveVertex (balls[i].a.x,balls[i].a.y,balls[i].a.z );
}
endShape();
}
endShape();
}
}
/////////////////////////////////////////////////////
class Ball {
PVector a;
// ball constructor
Ball(float _x, float _y,float _z) {
a = new PVector(_x,_y,_z);
float x,y,z;
x = _x;
y = _y;
z = _z;
}
/////////////////////////////////////////////////////
class Ball {
PVector a;
// ball constructor
Ball(float _x, float _y,float _z) {
a = new PVector(_x,_y,_z);
float x,y,z;
x = _x;
y = _y;
z = _z;
}
// ball functions
void update() {
a = new PVector();
for (int i = 0; i < attractor.length; i++) {
a.add(attractor[i].getAccel(a.x,a.y,a.z));
// a = new PVector();
// for (int k = 0; k < attractor.length; i++) {
//a.add(attractor[k].getAccel(x,y,z));
// }
}
void update() {
a = new PVector();
for (int i = 0; i < attractor.length; i++) {
a.add(attractor[i].getAccel(a.x,a.y,a.z));
// a = new PVector();
// for (int k = 0; k < attractor.length; i++) {
//a.add(attractor[k].getAccel(x,y,z));
// }
}
}
}
class Attractor {
float x,y,z;
Attractor(float _x, float _y, float _z ) {
x = _x;
y = _y;
z = _z;
}
void update() {
render();
stroke(255,0,0);
}
void render() {
stroke(255,0,0);
line(x-4, y,z-4, x+4, y,z);
line(x, y-4,z, x, y+4,z-4);
}
PVector getAccel(float px, float py, float pz) {
PVector a = new PVector(0,0,0);
float d2 = (dist(px,py,pz,x,y,z))*(dist(px,py,pz,x,y,z));
if (d2 > 1) {
a.x = G * (x-px) / d2;
a.y = G * (y-py) / d2;
a.z = G *(z-pz)/d2;
}
return a;
float x,y,z;
Attractor(float _x, float _y, float _z ) {
x = _x;
y = _y;
z = _z;
}
void update() {
render();
stroke(255,0,0);
}
void render() {
stroke(255,0,0);
line(x-4, y,z-4, x+4, y,z);
line(x, y-4,z, x, y+4,z-4);
}
PVector getAccel(float px, float py, float pz) {
PVector a = new PVector(0,0,0);
float d2 = (dist(px,py,pz,x,y,z))*(dist(px,py,pz,x,y,z));
if (d2 > 1) {
a.x = G * (x-px) / d2;
a.y = G * (y-py) / d2;
a.z = G *(z-pz)/d2;
}
return a;
}
}
}
1