2d Array Movement problem
in
Programming Questions
•
3 years ago
Hi All,
I'm working with 2d matrix movement and I haven't been able to fix a problem, the idea is to move some "dots" manipulating a 10x10 matrix.
For the moment I'm working with a 200x200 array (I have to fix it) but I have other "newbie" problems:
1. When the dots start to move the bottom row is not included, and I can't get why...
2. The movement doesn't start with the first click of the mouse, only with the second, and that brings me other further problems when extending the code.
3. The idea is to do one movement per click, but the loop is so quick that with every click the dots move 3 or 4 spaces.
Here is a functional short extract:
- int[][] dotMatrix;
- void setup() {
- size(200,200);
- background(200);
- noStroke();
- smooth();
- dotMatrix = new int[200][200]; // matrix size (to fix)
- dotMatrix[70][70] = 1; // feed matrix
- dotMatrix[90][70] = 1;
- dotMatrix[70][90] = 1;
- matrix();
- }
- void matrix() { // draw main matrix
- for (int i = 10; i <= 200; i = i+20) {
- for (int j = 10; j <= 200; j = j+20) {
- ellipse (i, j, 18, 18);
- }
- }
- }
- void transpUp() { // movement operation
- int translator = 20;
- for (int o = 10; o <= 190; o = o+translator) {
- for (int p = 10; p <= 190; p = p+translator) {
- int control = p;
- if (dotMatrix[o][p] == 1) {
- fill(255);
- ellipse(o,p,18,18);
- if (control <= 10) {
- control = control + 200;
- }
- int pp = control-translator;
- dotMatrix[o][pp] = 1;
- ellipse(o,p,18,18);
- dotMatrix[o][p] = 0;
- }
- }
- }
- }
- void draw(){
- for (int i = 10; i <= 190; i = i+20) {
- for (int j = 10; j <= 190; j = j+20) {
- if (dotMatrix[i][j] == 1) {
- fill(200);
- ellipse(i,j,18,18);
- } else {
- dotMatrix[i][j] = 0;
- }
- }
- }
- if ((mousePressed)) { // active movement with mouse
- transpUp();
- }
- }
Any help or comment is welcome !! thanks a lot.
1