it is not Spanish but Italian:blush:
I donāt want to rotate the grid but I want to move a single cube (with constant z) instead of the empty space that is created at the beginning, all this with a video in real time, like the game of 16 posted above, for the code post I sorry but I have the object cubes (or tiles for the 2d) and a subroutine that chooses a cube (I have the tile) adjacent to the void to move it, the 3D I need to then zoom and move the camera, then maybe make us a simple game in which, after a certain number of computer moves, the player must recompose the image but as already said they are at the beginning and all this is done for study
this is the 2d code with the load of an image in the given directory (it is adapted so there is for example the copy of the image to scale it and adapt the webcam to the screen, I donāt know if it is the best method but it is the simplest that I found), now I am in difficulty for the movement to be adapted in 3d, the code is the following in a single screen, the ārigheā and ācolonnaā variables at the beginning indicate the pieces of the puzzle while the translation speed is given by the velocita variable:
//import processing.video.*;
//Capture video;
int righe=9;//number of lines
int colonne=9;//number of columns
float velocita=.1;//translation speed
///
PImage immagine ;
PImage immaginebk;
int rigaVuota=int(random(0, righe));/// line empty tile
int colonnaVuota=int(random(0, colonne));///column empty tile
int mattonellaVuota=(rigaVuota*colonnaVuota );// the empty tile
int mattonelladamuovere;//tile to move
float passoW;//the space that the tile must travel for x
float passoH;//the space that the tile must travel for y
mattonella arraymatonelle[] =new mattonella[righe*colonne];
boolean finito=true;///variable control,have you finished the movement?
boolean sceglinuova=true;///variable control,should I choose another tile?
boolean trovatovuoto=true;//variable control, did you find the void?
PVector cordinatevuoto;
PVector codinatemuovi;
PVector codinatetemp;
////////////////////////////////////////
void setup() {
size(900, 600, P2D );
//< fullScreen( P2D);
passoW=width/righe;
passoH=height/colonne;
cordinatevuoto= new PVector();
codinatemuovi= new PVector();
immaginebk = createImage(width, height, RGB);
immagine = createImage(640, 480, RGB);
// video = new Capture(this, 640, 480);
// video.start();
int a=0;
for (int c = 0; c <colonne; c++) {
for (int r = 0; r <righe; r++) {
arraymatonelle[a] =new mattonella();
arraymatonelle[a].crea(r, c, (a==(mattonellaVuota)), a);
a++;
}
}
}
void draw() {
//background(0);
// video.read();
// video.loadPixels();
// immagine=video;
// video.loadPixels();
immagine = loadImage("img.jpg");
immagine.updatePixels();
immaginebk.copy(immagine, 0, 0, 640, 480, 0, 0, width, height);
immaginebk.updatePixels();
// if (video.available()) {
{
background(0);
int a=-1;
for (int c = 0; c <colonne; c++) {
for (int r = 0; r <righe; r++)
{
a++;
arraymatonelle[a].disegna();
if (( arraymatonelle[a].update()==true)&&(trovatovuoto==true ))
{
trovatovuoto=false;
cordinatevuoto=arraymatonelle[a].returnCordinate();
mattonellaVuota=a;
codinatemuovi=sceglimuovi(cordinatevuoto);
}
if ((sceglinuova==true)&&(trovatovuoto==false )) {
codinatetemp=arraymatonelle[a].returnCordinate();
if ((codinatetemp.x==codinatemuovi.x)&&(codinatetemp.y==codinatemuovi.y))
{
sceglinuova=false;
mattonelladamuovere=a;
}
}
if ((sceglinuova==false)&&(trovatovuoto==false))
{
finito=false;
finito= arraymatonelle[mattonelladamuovere].muovi(cordinatevuoto);
if (finito) {
arraymatonelle[mattonellaVuota].muoviVuoto(codinatemuovi);
arraymatonelle[mattonellaVuota].disegna();
arraymatonelle[mattonelladamuovere].disegna();
arraymatonelle[mattonelladamuovere].updateCordinate();
arraymatonelle[mattonellaVuota].updateCordinate();
trovatovuoto=true;
sceglinuova=true;
} else {
arraymatonelle[mattonelladamuovere].disegna();
}
}
}
}
}
}
//}
class mattonella {
//////////////////////// Dati
int numero;
boolean vuoto;//are you the empty tile?
PImage img ;//image to save a piece of the main image
PVector cordinateFoto = new PVector();//vector with the coordinates of the piece in the main image
PVector Rigaecolonna = new PVector();//row and column
PVector cordinate = new PVector();//original coordinates
PVector cordinatecopia = new PVector();//copy of the original coordinates to move the empty tile
/////////////////////////////////costrutore
mattonella() {
img = createImage(width/ righe, height/ colonne, RGB);
}
///////////////////////////////////////////////////////////////////////////////////////
void crea(int r, int c, boolean sevuoto,int a) {
numero=a; //tile number
Rigaecolonna.set(r, c);
img = createImage(width/ righe, height/ colonne, RGB);
cordinateFoto.set( Rigaecolonna.x*img.width, Rigaecolonna.y*img.height);
cordinate.set( Rigaecolonna.x*img.width, Rigaecolonna.y*img.height);
cordinatecopia.set( Rigaecolonna.x*img.width, Rigaecolonna.y*img.height);
vuoto=sevuoto;
}
/////////////////////////////////////////////////////
void disegna() {
image(img, cordinate.x, cordinate.y);
}
///////////////////////////////////////////////////////////
boolean update() {
immaginebk.loadPixels();
img.loadPixels();
if (vuoto==false) {//if it is not empty take the coordinates from the main image and copy the photo
img.copy(immaginebk, int( cordinateFoto.x), int ( cordinateFoto.y), img.width, img.height, 0, 0, img.width, img.height);
img.updatePixels();
} else {
for (int i=0; i<img.pixels.length; i++) {
img .pixels[i] = color(0, 0, 0);//if not color black
}
}
img.updatePixels();
return vuoto;//tell to main program if you are the empty tile
}
//////////////////////////////////////////////////////////////
boolean muovi (PVector destistanazione) {//this moves the tile to the destination, to be adapted for 3d?
PVector dest= new PVector() ;
dest=destistanazione.copy();
if ((abs(dest.y-cordinate.y)<=velocita)&&
(abs(cordinate.x-dest.x))<=velocita )
{
cordinate=dest.copy();
return true;
}else{
if (dest.x>cordinate.x)
{
cordinate.add(+velocita,0);
}
else
{
cordinate.add(-velocita,0);
}
if (dest.y>cordinate.y)
{
cordinate.add(0,+velocita);
}
else
{
cordinate.add(0,-velocita);
}
return false;
}
}
////////////////////////////////////////////////////////////
PVector returnCordinate() {//says the original coordinates
return cordinatecopia;
}
////////////////////////////////////////////////////
void updateCordinate() {
cordinatecopia=cordinate.copy();//update the tile coordinates when the movement is finished
}
/////////////////////////////////////////////////////
void muoviVuoto (PVector dest) {//instant movement for the empty tile
PVector fine= new PVector() ;
fine=dest.copy();
cordinate= fine.copy();
}
}
PVector sceglimuovi(PVector cordinate ) //he subroutine which, using the size of the tiles, chooses which to move
{
int caso;
PVector ctemp = new PVector();
ctemp=cordinate.copy();
caso=int(random(0, 4));
switch(caso) {
case 0:
if (cordinate.x==0) {
ctemp.add(+passoW, 0);
} else {
ctemp.add(-passoW, 0);
}
break;
case 1:
if (cordinate.x==(righe-1)*passoW)
{
ctemp.add(-passoW, 0);
} else {
ctemp.add(+passoW, 0);
}
break;
case 2:
if (cordinate.y==0) {
ctemp.add(0, +passoH);
} else {
ctemp.add(0, -passoH);
}
break;
case 3:
if (cordinate.y==(colonne-1)*passoH) {
ctemp.add(0, -passoH);
} else {
ctemp.add(0, +passoH);
}
break;
}
return ctemp;
}
it is probably more complicated than it should be, but it is made to study objects and I changed my mind at work, then it will have to be optimized,thank you again and I hope it is clearer now!
the choice must also be optimized so that it does not come back