Viewing a 2D sketch in 3D
in
Contributed Library Questions
•
1 year ago
Hello.
I've been messing with this script for a few days and I'm trying to move from 2D to 3D. My first step was to at least get PeasyCam so I could revolve around the objects drawn in a 3D environment. The problem I'm running in to is if I add "background(255);" to the void draw ( ) expression then I lose the series of boxes drawn as it moves across the sketch (which I'd like to keep), but the red cross-hairs (which denote attractors) remain and I'm able to orbit/pan/zoom. If I remove "background(255);" from void draw ( ) then i get the series of boxes but my cross-hairs leave a trail when i obit/pan/zoom. This is due to the way i've constructed the script. So my question is : What is the best way to rewrite this so that I can keep both the series of boxes and cross-hairs without either of them leaving a trail as I orbit/pan/zoom. I'm guessing that somehow I need to figure out how to draw the boxes like the cross-hairs so the geometry continues to update - but without computing all the acceleration stuff. I hope that is clear. The script below allows the camera to orbit/pan/zoom but leaves cross hair trails.
PS - i know i'm using a couple libraries (PeasyCam and Old3D) but my question is about the structure of my code - not library specific. That's why I posted here.
- import ixagon.renderer.*;
- import peasy.*;
- PeasyCam cam;
- ForcePoly myPoly;
- Attractor[] attractor = new Attractor[100];
- int G = 10000;
- /////////////////////////////////////////////////////
- void setup() {
- cam = new PeasyCam(this,width/2, height/2, 0, 300);
- size(600, 240, OldP3D.NOSTALGIA);
- smooth();
- background(255);
- for (int i=0; i < attractor.length; i++) {
- attractor[i] = new Attractor( random(width), random(height) );
- }
- myPoly = new ForcePoly (0, 25, 2, 0);
- }
- void draw() {
- //background(255);
- myPoly.update();
- for (int i=0; i < attractor.length; i++) {
- attractor[i].update();
- }
- }
- void keyPressed() {
- setup();
- }
- /////////////////////////////////////////////////////
- class ForcePoly {
- float x,y, xspeed, yspeed, angA;
- float angV = 0;
- boolean dead = false;
- PVector a;
- ForcePoly(float _x, float _y, float _xspeed, float _yspeed) {
- x = _x;
- y = _y;
- xspeed = _xspeed;
- yspeed = _yspeed;
- }
- void update() {
- if (!dead) {
- x += xspeed;
- y += yspeed;
- a = new PVector();
- for (int i = 0; i < attractor.length; i++) {
- a.add(attractor[i].getAccel(x,y));
- }
- angA = a.heading2D();
- angV += angA/5;
- render();
- if (x > 300) {
- dead = true;
- }
- angV *= .9;
- }
- }
- void render() {
- rectMode(CENTER);
- stroke(0);
- fill(255);
- pushMatrix();
- translate(x*2,y);
- rotate(angV);
- box(60, 30, 1);
- popMatrix();
- }
- }
- ///////////////////////////////////////////////////
- class Attractor {
- float x,y;
- Attractor(float _x, float _y ) {
- x = _x;
- y = _y;
- }
- void update() {
- render();
- }
- void render() {
- stroke(255,0,0);
- line(x-4, y, x+4, y);
- line(x, y-4, x, y+4);
- }
- PVector getAccel(float px, float py) {
- PVector a = new PVector(0,0,0);
- float d2 = sq(dist(px,py,x,y));
- if (d2 > 10) {
- a.x = G * (x-px) / d2;
- a.y = G * (y-py) / d2;
- }
- return a;
- }
- }
1