PVector and movement problem
in
Programming Questions
•
10 months ago
Hey,
I am having some trouble with my program. I am looking to create a shape and have it move back and forth. I added velocity to my location but it's not working.
Also, when I println(sideFront[i]); I only get 3 values instead of 4. Not sure why.
Any help would be appreciated,
Thanks
- Face faces;
- void setup() {
- size(500,400);
- smooth();
- faces = new Face(10,10,25);
- }
- void draw() {
- background(200);
- faces.display();
- faces.check();
- }
- class Face {
- PVector location;
- PVector velocity;
- float faceScale;
- float[] frontx;
- float[] fronty;
- PVector[] sideFront;
- Face(float _x, float _y, float _faceScale) {
- location = new PVector(_x,_y);
- velocity = new PVector(2,0);
- faceScale = _faceScale;
- frontx = new float[] {location.x,location.x+faceScale,location.x+faceScale,location.x};
- fronty = new float[] {location.y,location.y,location.y+faceScale,location.y+faceScale};
- sideFront = new PVector[frontx.length];
- for (int i = 0; i<frontx.length;i++) {
- sideFront[i] = new PVector(frontx[i], fronty[i]);
- }
- }
- void display() {
- beginShape();
- for(int i = 0; i < frontx.length; i++) {
- vertex(sideFront[i].x, sideFront[i].y);
- // println(sideFront[i]);
- //println(location.x);
- }
- endShape(CLOSE);
- location.add(velocity);
- //println(location);
- }
- void check() {
- if ((location.x > location.x+faceScale) || (location.x < location.x-faceScale)) {
- velocity.x = velocity.x * -1;
- }
- }
- }
1