Move towars target
in
Programming Questions
•
1 year ago
I want to set targets and move to them, one it reaches the end of the targets it should go back.
Atm it's stuck at the target (i only did x for now, once that works y is no problem).
The problem is in the Mirror class in the void update.
Atm it's stuck at the target (i only did x for now, once that works y is no problem).
The problem is in the Mirror class in the void update.
- Mirror m = new Mirror(100, 100);
- PImage bground = loadImage("essent.png");
- PImage lspot = loadImage("lspot_01.png");
- void setup() {
- size(800, 800);
- m.addTarget(100, 100)
- .addTarget(700, 700)
- .setSpeed(2);
- }
- void draw() {
- tint(255, 10);
- image(bground, 0, 0);
- m.update();
- m.draw();
- }
- class Mirror {
- PVector pos;
- float speed = 1;
- ArrayList<PVector> targets = new ArrayList<PVector>();
- int targetIndex = 0;
- int targetDirection = 1;
- Mirror() {
- }
- Mirror(float x, float y) {
- pos = new PVector(x, y);
- }
- Mirror(PVector pos) {
- this.pos = pos;
- }
- // - - - - - - - - - - - - -
- Mirror setSpeed(float speed) {
- this.speed = speed;
- return this;
- }
- // - - - - - - - - - - - - -
- Mirror addTarget(PVector t) {
- targets.add(t);
- return this;
- }
- // - - - - - - - - - - - - -
- Mirror addTarget(float x, float y) {
- targets.add(new PVector(x, y));
- return this;
- }
- // - - - - - - - - - - - - -
- void setNextTarget() {
- targetIndex += targetDirection;
- if(targetIndex >= targets.size() || targetIndex < 0) {
- targetDirection = -targetDirection;
- setNextTarget();
- }
- }
- // - - - - - - - - - - - - -
- void update() {
- PVector target = targets.get(targetIndex);
- if(pos.x < target.x) {
- pos.x += speed;
- } else {
- pos.x -= speed;
- }
- println(target.x - pos.x);
- if(target.x - pos.x < speed) { // should take y in account as well
- setNextTarget();
- }
- }
- // - - - - - - - - - - - - -
- void draw() {
- // blend(lspot, 0, 0, lspot.width, lspot.height, int(pos.x), int(pos.y), lspot.width, lspot.height, LIGHTEST);
- fill(255, 0, 0);
- ellipse(pos.x, pos.y, 5, 5);
- }
- // - - - - - - - - - - - - -
- }
1