how to export oop objects in dxf?
in
Core Library Questions
•
2 years ago
I'm running into a problem with exporting multiple objects in one code with a key stroke activated export. The issue is if I put the record strings in the drawBall() string it will only give me one object, however it yeilds nothing with placed up where the oop instantiation is, but the other point and image exporters work still. does anyone have any advice?
*note: code is a little sloppy looking because i have been cutting and pasting stuff around to get it to work
- import peasy.*;
- import processing.dxf.*;
- boolean record;
- PeasyCam cam;
- float [] xpos = new float[800];
- float [] ypos = new float [800];
- float [] zpos = new float [800];
- Ball[] ballies = new Ball[5];
- attractor[] attractor = new attractor[5];
- int counter;
- PrintWriter OUTPUT;
- // setup
- void keyPressed(){
- if (key == 'r') record = true;
- switch(key){
- case 'i':
- saveFrame("image"+counter+".jpg");
- break;
- case 'b':
- exportBallPoints2Text();
- break;
- case 'a':
- exportAttractorPoints2Text();
- break;
- }
- counter++;
- }
- void exportBallPoints2Text(){
- OUTPUT = createWriter("Balls"+counter+".txt");
- for (int i=0; i<ballies.length; i++) {
- OUTPUT.println(ballies[i].p.x + "," + ballies[i].p.y + "," + ballies[i].p.z);
- }
- counter++;
- OUTPUT.flush();
- OUTPUT.close();
- println("complete success (balls) !");
- }
- void exportAttractorPoints2Text(){
- OUTPUT = createWriter("attractor"+counter+".txt");
- for (int p=0; p<attractor.length; p++) {
- OUTPUT.println(attractor[p].x + "," + attractor[p].y + "," + attractor[p].z);
- }
- counter++;
- OUTPUT.flush();
- OUTPUT.close();
- println("complete success (attractor) !");
- }
- void setup() {
- size(800, 800, P3D);
- cam = new PeasyCam(this, 100);
- cam.setMinimumDistance(100);
- cam.setMaximumDistance(200);
- if (record) {
- beginRaw(DXF, "output.dxf");
- }
- for (int i=0; i<ballies.length; i++) {
- ballies[i] = new Ball(
- 400, 400, random(-10,10), random(-10,10),random(-10,10), 25);
- }
- if (record) {
- endRaw();
- record = false;
- }
- for (int p=0; p < attractor.length; p++) {
- attractor[p] = new attractor( width/8 + random(width), height/8 + random(height), random(50));
- smooth();
- }
- }
- //........................................................
- //........................................................
- void draw() {
- background(0);
- stroke(250,250,250);
- strokeWeight(2);
- point(xpos[0], ypos[0], zpos[0]);
- for (int g = 1; g <800; g++) {
- point(xpos[g], ypos[g], zpos[g]);
- }
- for (int h = 0; h < 100; h++) {//draw x axis
- stroke(255,h*5,0);
- point(h,0,0);
- }
- for (int h = 0; h < 100; h++) {// draw y axis
- stroke(h*5,255,0);
- point(0,h,0);
- }
- for (int h = 0; h < 100; h++) {// draw z axis
- strokeWeight(3);
- stroke(0,255,h*5);
- point(0,0,h);
- }
- for (int i=0; i<ballies.length; i++) {
- ballies[i].update();
- }
- for (int p=0; p < attractor.length; p++) {
- attractor[p].update();
- }
- }
- //........................................................
- class Ball {
- // ball fields
- float x;
- float y;
- float z;
- float speedX;
- float speedY;
- float speedZ;
- float angA;
- float angV = 0;
- boolean dead = false;
- PVector a;
- PVector v, p, standardV, offsetV;
- // ball constructor
- Ball(float _x, float _y, float _z, float _speedX, float _speedY, float _speedZ) {
- p = new PVector(_x, _y, _z);
- v = new PVector(_speedX, _speedY, speedZ);
- standardV = new PVector(_speedX, _speedY, speedZ);
- }
- // ball methods
- /*
- void update() {
- position();
- checkCollisions();
- drawBall();
- }
- */
- ///////////////////////////////
- void update() {
- if (!dead) {
- a = new PVector();
- for (int i = 0; i < attractor.length; i++) {
- a.add(attractor[i].getAccel(p.x,p.y,p.z));
- }
- angA = a.heading2D();
- angV += angA/.25;
- v = new PVector( standardV.x, standardV.y, standardV.z);
- a.normalize();
- a.mult(v.mag());
- a.mult(0.5);
- v.add(a);
- //checkCollisions();
- p.add(v);
- angV *= .9;
- }
- drawBall();
- }
- /////////////////////////////////////
- void drawBall() {
- pushMatrix();
- translate( p.x, p.y, p.z);
- //front
- beginShape();
- vertex(p.x+25,p.y,p.z+50);
- vertex(p.x+25,p.y,p.z);
- vertex(p.x+35,p.y,p.z);
- vertex(p.x+35,p.y,p.z+65);
- vertex(p.x-35,p.y,p.z+65);
- vertex(p.x-35,p.y,p.z);
- vertex(p.x-25,p.y,p.z);
- vertex(p.x-25,p.y,p.z+50);
- endShape(CLOSE);
- /*
- beginShape();
- vertex(p.x-35,p.y,p.z+65);
- vertex(p.x-35,p.y-random(2,10),p.z+65);
- vertex(p.x-35,p.y-random(2,10),p.z);
- vertex(p.x-35,p.y,p.z);
- endShape(CLOSE);
- */
- //sphere(5);
- popMatrix();
- }
- /*
- void checkCollisions() {
- if(p.x + v.x > width || p.x + v.x < 0) {
- v.x *= -1;
- standardV.x *= -1;
- }
- if(p.y + v.y > height || p.y + v.y < 0) {
- v.y *= -1;
- }
- if(p.y + v.y > height || p.y + v.y < 0) {
- v.y *= -1;
- }
- }
- */
- }
- /////////////////////////////////////////////////////
- class attractor {
- float x,y,z;
- attractor(float _x, float _y, float _z ) {
- x = _x;
- y = _y;
- z = _z;
- }
- void update() {
- render();
- }
- void render() {
- stroke(255,0,0);
- pushMatrix();
- translate(x,y,z);
- box(5);
- popMatrix();
- }
- PVector getAccel(float px, float py, float pz) {
- PVector a = new PVector(0,0,0);
- float d2 = sq(dist(px,py,pz,x,y,z));
- if (d2 > 10) {
- a.x = G * (x-px) / d2;
- a.y = G * (y-py) / d2;
- a.z = G * (z-pz) / d2;
- }
- return a;
- }
- }
1