PVector array
in
Programming Questions
•
1 year ago
Hello,
I am trying to use PVector instead of x and y coordinates in Mover class. (There is an array of x and y coordinates.)
I believe, I made mistake by defining PVector array. Because I cannot access PVector coordinates to draw shapes.
Is the PVector array is written correctly or not? If not, how I have to write it?
What is more, how to get access to its v[i].x and v[i].y coordinates to draw shapes?
- import processing.opengl.*;
- import peasy.*;
- import processing.dxf.*;
- boolean dxfExport;
- PeasyCam cam;
- Mover[] movers = new Mover[5];
- void setup() {
- size(800, 800, P3D);
- cam = new PeasyCam(this, 300, 300, 0, 1000);
- cam.setMinimumDistance(1);
- cam.setMaximumDistance(100000);
- noStroke();
- fill(160);
- for (int i = 0; i < movers.length; i++) {
- movers[i] = new Mover();
- }
- }
- void draw() {
- if (dxfExport) {
- beginRaw(DXF, "myFirstGeometryOutput.dxf");
- }
- background(0);
- ambientLight(95, 95, 95);
- directionalLight(51, 102, 126, 0, 0, -1);
- directionalLight(60, 60, 60, 1, 60, 0);
- directionalLight(20, 20, 20, 0, -5, 0);
- lightSpecular(0, 50, 0);
- lightFalloff(1, 0, 0);
- for (int i = 0; i < movers.length; i++) {
- movers[i].update();
- movers[i].display();
- }
- if(dxfExport) {
- endRaw();
- dxfExport = false;
- }
- }
- void keyPressed() {
- // use a key press so that it doesn't make a million files
- switch (key) {
- case 'x':
- dxfExport = true;
- println("DXF export complete!");
- break;
- case 's':
- saveFrame("boxes.jpg");
- println("screen grab made");
- break;
- }
- }
- //int[] xpos = new int[100];
- //int[] ypos = new int[100];
- PVector[] v = new PVector[100,100];
- class Mover {
- PVector location;
- PVector velocity;
- PVector acceleration;
- float topspeed;
- float a;
- Mover() {
- location = new PVector(random(width)*2, random(height)*2);
- velocity = new PVector(0, 0);
- topspeed = 5;
- }
- void update() {
- }
- void display() {
- PVector mouse = new PVector(random(mouseX)*10, random(mouseY)*10);
- PVector acceleration = PVector.sub(mouse, location);
- acceleration.normalize();
- acceleration.mult(0.2);
- velocity.add(acceleration);
- velocity.limit(topspeed);
- location.add(velocity);
- for (int i=0; i<v.length-1; i++) {
- v[i].x = v[i+1].x;
- v[i].y = v[i+1].y;
- }
- //for (int i=0; i<xpos.length-1; i++) {
- //xpos[i] = xpos[i+1];
- //ypos[i] = ypos[i+1];
- //}
- v[v.length-1] = location;
- //xpos[xpos.length-1] = int(location.x);
- // ypos[ypos.length-1] = int(location.y);
- for (int i = 0; i<v.length; i++){
- //for (int i=0; i<xpos.length; i++) {
- stroke(255);
- strokeWeight(1);
- fill(255);
- float c1 = 10;
- float c2 = 10;
- beginShape();
- vertex(v[i].x+c1+48, v[i].y+c2, (i+1)*20);
- vertex(v[i].x+c1, v[i].y+c2, (i+1)*20);
- vertex(v[i].x+c1, v[i].y+c2, i*20);
- vertex(v[i].x+c1+48, v[i].y+c2, i*20);
- endShape(CLOSE);
- }
- }
- }
1