3D walker in a 3D grid / cube: what are the directions he can go to?

Hello all,

I was doing a 3D walker in a 3D-grid.

What are the directions he can go to?

I plan to have a list of PVectors and depending which one is active, we add it to his position again and again so he moves in that direction.

I just want the list of possible directions to be complete.

He can go towards x, towards y, towards z,
but also towards -x, towards -y, towards -z,

But then also diagonal…

I came up with the list.
Is it complete? Or redundant?

Thank you!

Warm regards,

Chrisir :wink:


  PVector[] pvList = {
    new PVector (0, 0, 1), 
    new PVector (0, 1, 0), 
    new PVector (1, 0, 0), 

    new PVector (1, 0, 1), 
    new PVector (1, 1, 0), 
    new PVector (0, 1, 1), 

    new PVector (1, 1, 1), 

    //---

    new PVector (0, 0, -1), 
    new PVector (0, -1, 0), 
    new PVector (-1, 0, 0), 

    new PVector (-1, 0, -1), 
    new PVector (-1, -1, 0), 
    new PVector (0, -1, -1), 

    new PVector (-1, -1, -1)

    
  };
1 Like

That list is correct.

There’s probably a fancy technique that you could use, but what you have is pretty much perfect.

No, it’s not complete

Hello,

Here is a ternary count which gives 3^3 = 27 counts:

PVector[] pvList1 = 
    {  
    new PVector ( 0,  0,  0),  // This can be removed
    new PVector ( 0,  0,  1),  // forward
    new PVector ( 0,  0,  2),  // reverse
    new PVector ( 0,  1,  0),   
    new PVector ( 0,  1,  1), 
    new PVector ( 0,  1,  2),     
    new PVector ( 0,  2,  0),   
    new PVector ( 0,  2,  1), 
    new PVector ( 0,  2,  2),   

    new PVector ( 1,  0,  0),   
    new PVector ( 1,  0,  1), 
    new PVector ( 1,  0,  2), 
    new PVector ( 1,  1,  0),   
    new PVector ( 1,  1,  1), 
    new PVector ( 1,  1,  2),     
    new PVector ( 1,  2,  0),   
    new PVector ( 1,  2,  1), 
    new PVector ( 1,  2,  2),      
    
    new PVector ( 2,  0,  0),   
    new PVector ( 2,  0,  1), 
    new PVector ( 2,  0,  2), 
    new PVector ( 2,  1,  0),   
    new PVector ( 2,  1,  1), 
    new PVector ( 2,  1,  2),     
    new PVector ( 2,  2,  0),   
    new PVector ( 2,  2,  1), 
    new PVector ( 2,  2,  2),  
    };

Replace all the 2’s with -1’s and you have a complete list that you can iterate down to what you want to use.

https://en.wikipedia.org/wiki/Ternary_numeral_system

:)

1 Like

Impressive.

I personally thought my list was for example

1,1,1

and

-1,-1,-1

but not

-1,1,-1

So not complete.

But I could not figure it out

Thank you!!

Chrisir

1 Like

here is it with -1

 PVector[] pvList = 
    {  
    // new PVector ( 0,  0,  0),   // This can be removed
    new PVector ( 0, 0, 1), // forward
    new PVector ( 0, 0, -1), // reverse
    new PVector ( 0, 1, 0), 
    new PVector ( 0, 1, 1), 
    new PVector ( 0, 1, -1), 
    new PVector ( 0, -1, 0), 
    new PVector ( 0, -1, 1), 
    new PVector ( 0, -1, -1), 

    new PVector ( 1, 0, 0), 
    new PVector ( 1, 0, 1), 
    new PVector ( 1, 0, -1), 
    new PVector ( 1, 1, 0), 
    new PVector ( 1, 1, 1), 
    new PVector ( 1, 1, -1), 
    new PVector ( 1, -1, 0), 
    new PVector ( 1, -1, 1), 
    new PVector ( 1, -1, -1), 

    new PVector ( -1, 0, 0), 
    new PVector ( -1, 0, 1), 
    new PVector ( -1, 0, -1), 
    new PVector ( -1, 1, 0), 
    new PVector ( -1, 1, 1), 
    new PVector ( -1, 1, -1), 
    new PVector ( -1, -1, 0), 
    new PVector ( -1, -1, 1), 
    new PVector ( -1, -1, -1), 
  };

You must have put a lot of work in this

How did you make the list? Not by hand I guess. But with code.

How did you make the 3D graphic as a demonstration?

It’s an awesome post.

Reminds me of the discussion and your post here by the way: Iterate once and only once through all the possible configurations of an array

ah, the code to generate the list could be:



size(300, 300); 

println("PVector[] pvList1 = "); 
println("    {"  );

for (int i0 = 0; i0 < 3; i0++) { 
  for (int i1 = 0; i1 < 3; i1++) { 
    for (int i2 = 0; i2 < 3; i2++) { 
      //String a = str(i0)
      //  +str(i1)
      //  +str(i2);
      println(
        "        new PVector ( "  
        + str(i0)+", "
        +str(i1)+", "
        +str(i2)
        +" ),");
    }//for
  }
}

println("};");

and the code to demonstrate the list as a graphic


float angle1; 

// auto-generated list of 3D PVectors for all possible directions in 3D (right, left, up, diagonal right downwards....)
PVector[] pvList1 = 
  {
  //  new PVector ( 0, 0, 0 ), 
  new PVector ( 0, 0, 1 ), 
  new PVector ( 0, 0, -1 ), 
  new PVector ( 0, 1, 0 ), 
  new PVector ( 0, 1, 1 ), 
  new PVector ( 0, 1, -1 ), 
  new PVector ( 0, -1, 0 ), 
  new PVector ( 0, -1, 1 ), 
  new PVector ( 0, -1, -1 ), 
  new PVector ( 1, 0, 0 ), 
  new PVector ( 1, 0, 1 ), 
  new PVector ( 1, 0, -1 ), 
  new PVector ( 1, 1, 0 ), 
  new PVector ( 1, 1, 1 ), 
  new PVector ( 1, 1, -1 ), 
  new PVector ( 1, -1, 0 ), 
  new PVector ( 1, -1, 1 ), 
  new PVector ( 1, -1, -1 ), 
  new PVector ( -1, 0, 0 ), 
  new PVector ( -1, 0, 1 ), 
  new PVector ( -1, 0, -1 ), 
  new PVector ( -1, 1, 0 ), 
  new PVector ( -1, 1, 1 ), 
  new PVector ( -1, 1, -1 ), 
  new PVector ( -1, -1, 0 ), 
  new PVector ( -1, -1, 1 ), 
  new PVector ( -1, -1, -1 ) 
};

void setup() {
  // init
  size(800, 600, P3D);
} // func 

void draw() {
  // runs on and on 
  background(0);
  lights();

  stroke(255, 0, 0);
  strokeWeight(8);

  pushMatrix();
  translate(width/2, height/2, 0);
  rotateX(-0.2992);
  rotateY(angle1); 

  int i=0; 
  for (PVector pv : pvList1) {
    float amt = map(i, 
      0, pvList1.length, 
      0, 1);
    stroke(lerpColor(color(255), color(255, 0, 0), amt));
    line(
      //0, 0, 0, 
      pv.x*8, pv.y*8, pv.z*8, 
      pv.x*88, pv.y*88, pv.z*88);
    i++;
  }//for
  popMatrix();
  //

  stroke(100); 
  //  noFill();
  fill(255, 0, 0);

  pushMatrix();
  translate(100, 100, 0);
  rotateZ(angle1);
  if (!keyPressed)
    angle1+=.04; 
  box(60);
  popMatrix();

  pushMatrix();
  translate(400, 100, 0); 
  rotateX(angle1);
  fill(0, 0, 255);
  box(60);
  popMatrix();

  pushMatrix();
  translate(700, 100, 0);
  rotateY(angle1);
  box(60);
  popMatrix();

  pushMatrix();
  translate(700, 300, -330);
  noStroke(); 
  sphere(50);
  popMatrix();
} // func 
//

Thank you!

Very inspiring!

1 Like