How to move an object back and forth in p5js javascript

bismo

After a few hours of work and research (I just started learning p5js and javascript) I have about 50 lines of code that creates a grid of circles and begins to move them across the canvas. Before I get into my issue, I will share the code.

var circles = [];

function setup() {
  createCanvas(500, 500);
  for (var a = 50; a < width - 300; a += 20) {
    for (var b = 50; b < height - 300; b += 20) {
      circles.push(new Circle(a, b));
    }
  }
}

function draw() {
  background(50);
  for (var b = 0; b < circles.length; b++) {
    circles[b].show();
  }
}

function Circle(x, y) {
  this.x = x;
  this.y = y;

  this.show = function() {
    fill(255);
    noStroke();
    ellipse(this.x, this.y, 10, 10);
    if (this.x < 51) {
      this.x += 1
      this.y += 1
    } 
    if (this.x < 430) {
      this.x += 1
      this.y += 1
    }
    if (this.x > 430) {
      this.x -= 1
      this.y -= 1
    }
    if (this.x < 51) {
      this.x += 1
      this.y += 1
    } 
  }
}

What I would like to do is move this grid of circles starting at (50,50) to the bottom right corner. Once it hits (width-50,height-50) I'd like the movement to reverse back to the starting point, and then back the other way. This code moves the circles to the bottom right corner successfully, them something goes wrong. The circles don't reverse their movement, rather, they get messed up. I thought the if statements would handle this but I must be missing something. I trouble shooted for about an hour and now I thought I'd ask SO. Thanks!

Rabbid76

Add a moving direction to the object. Change the direction when the object goes out of bounds:

var circles = [];

function setup() {
    createCanvas(500, 500);
    for (var a = 50; a < width - 300; a += 20) {
        for (var b = 50; b < height - 300; b += 20) {
            circles.push(new Circle(a, b));
        }
    }
}

function draw() {
    background(50);
    for (var b = 0; b < circles.length; b++) {
        circles[b].show();
    }
}
    
function Circle(x, y) {
    this.x = x;
    this.y = y;
    this.dx = 1;
    this.dy = 1

    this.show = function() {
        fill(255);
        noStroke();
        ellipse(this.x, this.y, 10, 10);

        this.x += this.dx
        this.y += this.dy

        if (this.x < 51 || this.y < 51) {
            this.dx = 1
            this.dy = 1
        } 
        if (this.x > 430 || this.y > 430) {
            this.dx = -1
            this.dy = -1
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

If you do not want to move the objects individually, you must use 1 direction of movement for all objects:

var circles = [];

function setup() {
    createCanvas(500, 500);
    for (var a = 50; a < width - 300; a += 20) {
        for (var b = 50; b < height - 300; b += 20) {
            circles.push(new Circle(a, b));
        }
    }
}

var dx = 1
var dy = 1
function draw() {
    background(50);
    mx = dx
    my = dy
    for (var b = 0; b < circles.length; b++) {
        circles[b].show(mx, my);
    }
}
    
function Circle(x, y) {
    this.x = x;
    this.y = y;

    this.show = function(mx, my) {
        fill(255);
        noStroke();
        ellipse(this.x, this.y, 10, 10);

        this.x += mx
        this.y += my

        if (this.x < 51) {
            dx = 1
            dy = 1
        } 
        if (this.x > 430) {
            dx = -1
            dy = -1
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript move object back and forth

Move object with tween back and forth with Godot 4

How to move an actor back and forth Unreal Engine

How to rotate an image back and forth with JavaScript

How to move items back and forth between a TreeView and CheckedListBox?

How to move UITableViewCell back and forth to show it can be swiped?

How to move back and forth on a game board depending on random number (dice)?

How to Move a DIV Back & Forth SLOWLY Overtop a Background?

How can I rotate this object back and forth forever?

I want to move object back and forth in pygame but it only moves left and then it stop

Getting objects to move back and forth in unity?

How do I rotate an object in p5js?

How to switch pyqt5 dialogs multiple times back and forth?

How to quickly move/rename certain items back and forth between specific folders?

I can move back or forth in line only 1 character a time. How I can fix it?

How to change or manage the "Carbon" timezone so it doesn't move the clock back and forth

Toggling JS button back and forth

Shake an object back and forth with easing at the end?

Having trouble lerping a game object back and forth

Javascript ball bouncing back and forth indefinitely

Communicating back and forth between Javascript and c#

Moving a div back and forth / animation using javascript

Using ListIterator to move back and forth over a LinkedList in Java

Move an element back and forth diagonally using Java Script

Move along an arc in p5js

How to reverse-i-search back and forth?

How to reverse-i-search back and forth?

How to loop a video back and forth with ffmpeg

How to swap text back and forth when clicking on it?