finally I did it !, now I fail to prevent the computer from coming back after a move and playing with the mouse, but for that I still have to study, thanks again!
my mistake was in giving the coordinates to the pshape being created while now I create it of the right dimension and then with the function translate and with the popMatrix and pushMatrix the drawing where I want
ps with the image of the cam is more beautiful!
//import processing.video.*;
//Capture video;
int righe=10;//number of lines
int colonne=10;//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
cubo arraymatonelle[] =new cubo[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, P3D );
//< 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 cubo();
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 cubo {
//////////////////////// Dati
int numero;
PShape cubo;
PShape lato1;
PShape lato2;
PShape lato3;
PShape lato4;
PShape lato5;
PShape lato6;
boolean vuoto;
PImage img ;
PImage img2 ;
PVector cordinateFoto = new PVector();
PVector cordinate = new PVector();
PVector cordinatecopia = new PVector();
/////////////////////////////////costrutore
cubo() {
img = createImage(width/ righe, height/ colonne, RGB);
img2 = createImage(width/ righe, height/ colonne, RGB);
for (int i = 0; i < img2.pixels.length; i++) {
img2.pixels[i] = color(255*(int(random(0, 2))), 255*(int(random(0, 2))), 255*(int(random(0, 2))));
}
}
///////////////////////////////////////////////////////////////////////////////////////
void crea(int r, int c, boolean sevuoto, int a) {
cubo=createShape(GROUP);
lato1=createShape();
lato2=createShape();
lato3=createShape();
lato4=createShape();
lato5=createShape();
lato6=createShape();
numero=a;
cordinateFoto.set( r*img.width, c*img.height, 0);
cordinate.set (r*img.width, c*img.height, 0);
cordinatecopia.set( r*img.width, c*img.height, 0);
vuoto=sevuoto;
lato1.beginShape();/////////////////////////////////davanti
lato1.texture(img);
lato1.textureMode(NORMAL);
lato1.vertex(0, 0, 0, 0, 0);
lato1.vertex(img.width, 0,0, 1, 0);
lato1.vertex(img.width, img.height, 0, 1, 1);
lato1.vertex(0, img.height, 0, 0, 1);
lato1.endShape();////////////////////////////////////////
lato2.beginShape();/////////////////////////////////dietro
lato2.texture(img2);
lato2.textureMode(NORMAL);
lato2.vertex(img.width, 0,- 50, 0, 0);
lato2.vertex(0, 0,- 50, 1, 0);
lato2.vertex(0, img.height,- 50, 1, 1);
lato2.vertex(img.width, img.height,- 50, 0, 1);
lato2.endShape();//////////////////////////////////////////////
lato3.beginShape();//////////////////////////////////////sotto
lato3.texture(img2);
lato3.textureMode(NORMAL);
lato3.vertex(0, img.height, -50, 0, 0);
lato3.vertex(img.width, img.height, -50, 1, 0);
lato3.vertex(img.width, img.height, 0, 1, 1);
lato3.vertex(0, img.height, 0, 0, 1);
lato3.endShape();///////////////////////////////////////////////
lato4.beginShape();////////////////////////////////////sopra
lato4.texture(img2);
lato4.textureMode(NORMAL);
lato4.vertex(0, 0, 0, 0, 0);
lato4.vertex(img.width,0, 0, 1, 0);
lato4.vertex(img.width, 0, -50, 1, 1);
lato4.vertex(0, 0,-50, 0, 1);
lato4.endShape();//////////////////////////////////////////////////
lato5.beginShape();///////////////////////////////////////////////dx
lato5.texture(img2);
lato5.textureMode(NORMAL);
lato5.vertex(img.width, 0, -50, 0, 0);
lato5.vertex(img.width, 0, 0, 1, 0);
lato5.vertex(img.width, img.height, 0, 1, 1);
lato5.vertex(img.width, img.height,-50, 0, 1);
lato5.endShape();///////////////////////////////////////////////////
lato6.beginShape();////////////////////////////////////////////sx
lato6.texture(img2);
lato6.textureMode(NORMAL);
lato6.vertex(0, 0, 0, 0, 0);
lato6.vertex(0,0, -50, 1, 0);
lato6.vertex(0, img.height, -50, 1, 1);
lato6.vertex(0,img.height,0, 0, 1);
lato6.endShape();////////////////////////////////////////////////
cubo.addChild(lato1);
cubo.addChild(lato2);
cubo.addChild(lato3);
cubo.addChild(lato4);
cubo.addChild(lato5);
cubo.addChild(lato6);
}
/////////////////////////////////////////////////////
void disegna() {
pushMatrix ();
translate (cordinate.x, cordinate.y);
shape(cubo);
popMatrix ();
}
///////////////////////////////////////////////////////////
boolean update() {
if (vuoto==false) {
img.updatePixels();
immaginebk.loadPixels();
img.loadPixels();
img.copy(immaginebk, int( cordinateFoto.x), int ( cordinateFoto.y), img.width, img.height, 0, 0, img.width, img.height);
img.updatePixels();
}
else {
cubo.setVisible(false);
}
return vuoto;
}
//////////////////////////////////////////////////////////////
boolean muovi (PVector destistanazione) {
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() {
return cordinatecopia;
}
////////////////////////////////////////////////////
void updateCordinate() {
cordinatecopia=cordinate.copy();
}
/////////////////////////////////////////////////////
void muoviVuoto (PVector dest) {
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;
}