a graphical 3D grid of rectangles that you display in 3D and
a 3D array as a data structure.
A 3D grid (in the graphical sense) can easily be displayed using a 1D array, since the data structure does not determine the position of the items in 3D space. Just make a 1D array of PVector and use PVector with 3 parameters x,y,z to display the rectangle. When you for-loop over the items each item can be anywhere in space, independently of the position in the 1D array / list.
(A 3D grid can be displayed using a 3D array of course but this is slightly more complicate. It is only useful when you want neighbours to interact (e.g. 2 adjacent red boxes explode or something) because in a 3D grid it is much easier to check the neighbours of a rectangle in the grid. This would be more difficult (but still doable) in a 1D grid. But let's stick to the 1D array to display a 3D grid.)
How to fill the 1D array
To fill the array with the 3D positions (in setup()) you can use 3 nested for-loops i,j,k so that you can determine the x,y,z from them :
to position the box or the rect, you'll need pushMatrix, translate and popMatrix
remark
(interestingly, point and line are available in 3D but rect is not; since this is so you need pushMatrix, translate and popMatrix and with rect(0,0,10,10); - always use 0,0 here since translate takes care of the position in space. Or use box(10); )
please post your code and ask concrete questions then
Thanks Chrisir for the informations, i will write the code and post it here, infact i want to create a piano in perspective and play it via Kinect. I already created my piano with rect and loop for, but i didn't use Array for that.
Your use of the word "Array" is misleading. You're not looking for a data structure that uses indexes to address one of many elements. You want to draw a 3D box in a 3D space.
Wow, it's incredible, thanks for the code Chrisir. Instead of mousePressed i will use my Kinect value, because my character in the screen will play the piano by collision effect.
I'll try it, just there is a bit worry about the collision between my character and the keys. Because my character is not above the keys and that makes problem for the collision.
you could connect the dots with joints (arms, body, neck...)
2.
then you could rotate it and make it bigger (scale)
until he's sitting on / in front of the piano
OR
2.
you rotate and scale and translate the piano until it is right under the hands of the body.
actually the width of the piano must fit the guys hands when he plays the highest and the lowest note on the piano (the finger should hit the right key on the piano)
3.
of course you could "cheat"
I mean you get data and you analyze them which note / key has to be played. Play the note.
then you move the finger to the key position you want it to be (and not as you have it now probably to the exact coordinates kinect gives you).
that's cheating because in a way naturally, the character should hit the key automatically. Now you split up your kinect input to generate the tone / note and the display of the character. Maybe this helps.
I mean, the head and feed still can come from the kinect and the fingers you cheat.
so you can make it look like it did it right. Maybe.
Well, i want to play it with the foots, but the character is too big, i already resize the sketch from 640,400 to 1280,750, because i wanted to be in big sketch and when i did that my character resized too :/ but the character is not on top...
Answers
Hello!
yes. But nobody will write it for you.
Just follow these simple steps.
remark
Please distinguish between
a graphical 3D grid of rectangles that you display in 3D and
a 3D array as a data structure.
A 3D grid (in the graphical sense) can easily be displayed using a 1D array, since the data structure does not determine the position of the items in 3D space. Just make a 1D array of PVector and use PVector with 3 parameters
x,y,z
to display the rectangle. When you for-loop over the items each item can be anywhere in space, independently of the position in the 1D array / list.(A 3D grid can be displayed using a 3D array of course but this is slightly more complicate. It is only useful when you want neighbours to interact (e.g. 2 adjacent red boxes explode or something) because in a 3D grid it is much easier to check the neighbours of a rectangle in the grid. This would be more difficult (but still doable) in a 1D grid. But let's stick to the 1D array to display a 3D grid.)
How to fill the 1D array
To fill the array with the 3D positions (in
setup()
) you can use 3 nested for-loops i,j,k so that you can determine the x,y,z from them :be aware that you need a var
index
to use with the array, because in a nested for-loop i,j,k are not valid as an index for the 1D array.....nested for-loop means it's a for-loop inside a for-loop inside a for-loop
this is because we x,y,z in the grid (i,j,k) so we have rows, columns a depth for the grid
so the outer for-loop says: I want 10 times the inner loop
and the middle loop says I want 10 times the most inner loop.
So it's 10x 10x 10x rectangles.
The outer loop is the columns, then the rows, then each time the depth.
then display the data in draw()
Instead of
rect()
you can usebox()
- see referencehttps://www.processing.org/reference/
to position the box or the rect, you'll need pushMatrix, translate and popMatrix
remark
(interestingly,
point
andline
are available in 3D butrect
is not; since this is so you need pushMatrix, translate and popMatrix and withrect(0,0,10,10);
- always use 0,0 here since translate takes care of the position in space. Or usebox(10);
)please post your code and ask concrete questions then
Best, Chrisir ;-)
in the tutorials there is a tutorial about arrays and another one about Two-Dimensional Arrays.
3D arrays are like this, but just one dimension more.
(arrays are data structures)
there is also a tutorial about programming in 3D named P3D
https://processing.org/tutorials/
Thanks Chrisir for the informations, i will write the code and post it here, infact i want to create a piano in perspective and play it via Kinect. I already created my piano with rect and loop for, but i didn't use Array for that.
This is my actual code before using Array:
int[] blackRect = {1,1,0,1,1,1,0,1};
color[] couleur = {color(0),color(0),0,color(0),color(0),color(0),0,color(0)};
void setup(){
size(1280,750, P3D);
}
void draw(){
background(0);
translate(width/2, height/2, 30);
stroke(255);
noFill();
box(680, 400, 1000);
//blackRect
pushMatrix();
translate(94, 100, 275);
rotateX(PI/2);
int largeur_black = 19;
for (int x = 0; x < 8; x++)
{
fill(couleur[x]);
noStroke();
if(blackRect[x] == 1)
rect(30-(2 * x * largeur_black), 80, largeur_black, 100);
}
popMatrix();
//whiteRect
pushMatrix();
translate(103, 100, 150);
rotateX(PI/2);
int largeur_white = 38;
for(int i = 0; i < 9; i++)
{ fill(128);
stroke(0);
rect(30-(i*largeur_white), 110, largeur_white, 186);
}
popMatrix();
Your use of the word "Array" is misleading. You're not looking for a data structure that uses indexes to address one of many elements. You want to draw a 3D box in a 3D space.
Use the function box().
Yes, i need to draw my rectangles in Array list because after i can use collision effect easier for each rects separately.
int[] blackRect = {1,1,0,1,1,1,0,1};
color[] couleur = {color(0),color(0),0,color(0),color(0),color(0),0,color(0)};
void setup(){
size(1280,750, P3D);
}
void draw(){
background(0);
translate(width/2, height/2, 30);
stroke(255);
noFill();
box(680, 400, 1000);
//blackRect
pushMatrix();
translate(94, 100, 275);
rotateX(PI/2);
int largeur_black = 19;
for (int x = 0; x < 8; x++)
{
fill(couleur[x]);
noStroke();
if(blackRect[x] == 1)
rect(30-(2 * x * largeur_black), 80, largeur_black, 100);
}
popMatrix();
//whiteRect
pushMatrix();
translate(103, 100, 150);
rotateX(PI/2);
int largeur_white = 38;
for(int i = 0; i < 9; i++)
{ fill(128);
stroke(0);
rect(30-(i*largeur_white), 110, largeur_white, 186);
}
popMatrix();
}
you can "play" with the mouse - it recognizes the key you pressed
needs a long time at start-up though
Wow, it's incredible, thanks for the code Chrisir. Instead of mousePressed i will use my Kinect value, because my character in the screen will play the piano by collision effect.
yes.
it's working with an invisible PGraphics (visible when pressing space bar) that holds unique colors for each key
why needs a long time at start-up though ?
ask one of the gurus.....
or a bug
Now i integrated my character in it and even it take more time to start-up !
I think maybe is because of the size of my sketch.
yes
there are other methods and a lib for 3D picking instead of using PGraphics which seems to be very expensive on the cpu or whatever
or it's a bug
try assigning more memory to the sketch in the preferences first of course (at least 5 mb or so)
picking is the collision of rect with mouse
it is hard to calculate since most keys are diagonal
idea: you could cheat and just save the invisible image (hit space bar first and then save - you need to program this when save) to file A
then save sketch as... with a new name / new version
then strip the entire PGraphic and just load file A in setup() and use get(mouseX, mouseY) on this
should be much faster
I'll try it, just there is a bit worry about the collision between my character and the keys. Because my character is not above the keys and that makes problem for the collision.
The actual result at the moment:
that's very bad.
0.
you could connect the dots with joints (arms, body, neck...)
2.
then you could rotate it and make it bigger (scale)
until he's sitting on / in front of the piano
OR
2.
you rotate and scale and translate the piano until it is right under the hands of the body.
actually the width of the piano must fit the guys hands when he plays the highest and the lowest note on the piano (the finger should hit the right key on the piano)
3.
of course you could "cheat"
I mean you get data and you analyze them which note / key has to be played. Play the note.
then you move the finger to the key position you want it to be (and not as you have it now probably to the exact coordinates kinect gives you).
that's cheating because in a way naturally, the character should hit the key automatically. Now you split up your kinect input to generate the tone / note and the display of the character. Maybe this helps.
I mean, the head and feed still can come from the kinect and the fingers you cheat.
so you can make it look like it did it right. Maybe.
Best, Chrisir ;-)
Well, i want to play it with the foots, but the character is too big, i already resize the sketch from 640,400 to 1280,750, because i wanted to be in big sketch and when i did that my character resized too :/ but the character is not on top...
you can make the character smaller with scale.... and leave the rest at it is
also use
translate
to bring him into positionI don't think it is possible with scale, because i resized in joint position too, then it's something global.
Edit post. Select code. Press Ctrl + o.
(You might have to split the code into two posts if it's too long)
The problem solved, i share my code with you, but the character can play a whiteRect not the blackOne yet, this will solve later ;)
great!