reset position if inside shape

edited August 2016 in Library Questions

hello,again,i have made this code,with a 3d model,the model starts dismorph after 100 frames.I have a box shape that is moved with mouse,i want when ever the vertices be inside the box,to reset their position to the original position,and reconstruct the model,while the others dismorph. Always resetting the position when they are in the box,and move when they are not.Can anyone help me.Until now what it happens,is that when the vertices are inside the box they stop moving.

Thanks in Advance

import peasy.*;
import saito.objloader.*;


OBJModel model ;
OBJModel tmpmodel ;

PeasyCam cam;

float easing = 0.005;
float r;
float k =0.00001;
int VertCount;
PVector[] Verts;
PVector Mouse;

void setup()
{
  size(800, 800, P3D);
  frameRate(30);
  noStroke();

  model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  model.enableDebug();
  model.scale(100);
  model.translateToCenter();

  tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  tmpmodel.enableDebug();
  tmpmodel.scale(100);
  tmpmodel.translateToCenter();



  cam = new PeasyCam(this, width/2, height/2, 0, 994);
}



void draw()
{
  background(129);
  lights();

  int VertCount = model.getVertexCount ();
  PVector[] Verts = new PVector[VertCount];
  PVector[] locas = new PVector[VertCount];
  float r =80;
  PVector Mouse = new PVector(mouseX-width/2, mouseY-height/2, 0);


  cam.setMouseControlled(true);










  //println(frameCount);
  pushMatrix();
  translate(width/2, height/2, 0);



  for (int i = 0; i < VertCount; i++) {
    //PVector orgv = model.getVertex(i);


    Verts[i]= model.getVertex(i);
    arrayCopy(Verts, locas);
    //PVector tmpv = new PVector();
    if (frameCount> 100) {



      float randX = random(-5, 5);
      float randY = random(-5, 5);
      float randZ = random(-5, 5);

      PVector Ran = new PVector(randX, randY, randZ);

      //float norX = abs(cos(k)) * randX;
      //float norY = abs(cos(k)) * randY;
      //float norZ = abs(cos(k)) * randZ;









      if (Verts[i].x > Mouse.x  - r/2 && Verts[i].x < Mouse.x  + r/2) {
        if (Verts[i].x > Mouse.y  - r/2 && Verts[i].x < Mouse.y  + r/2) {
          if (Verts[i].x > Mouse.z  - r/2 && Verts[i].x <  Mouse.z  + r/2) {


            arrayCopy(locas, Verts);
          }
        }
      } else {


        Verts[i].x+=Ran.x;
        Verts[i].y+=Ran.y;
        Verts[i].z+=Ran.z;

        if (Verts[i].x > width/2 || Verts[i].x < -width/2) {
          Verts[i].x+=-Ran.x;
        }
        if (Verts[i].y > height/2 || Verts[i].y < -height/2) {
          Verts[i].y+=-Ran.y;
        }
        if (Verts[i].z < -800/2 || Verts[i].z > 800/2) {  
          Verts[i].z+=-Ran.z;
        }
      }
      tmpmodel.setVertex(i, Verts[i].x, Verts[i].y, Verts[i].z);
    }
    k+=0.0001;
  }

  pushMatrix();
  translate(Mouse.x, Mouse.y, Mouse.z);
  noFill();
  stroke(255);
  box(r);
  popMatrix();


  noStroke();

  tmpmodel.draw();

  popMatrix();



  pushMatrix();
  translate(width/2, height/2, 0);
  noFill();
  stroke(255);
  box(width, height, 600);
  popMatrix();
}

Answers

  • We can't run this without the model files

  • where should i put them ?

  • or you can try it with the saito obj loader example file

  • or PShapeObj example file

  • The effect will not be exact the same because i have transformed the faces in blender,but the logic will be the same.

  • Answer ✓

    I found the solution,i created an extra model(The same),and take the coordinates from it.Here is the code

    import peasy.*;
    import saito.objloader.*;
    
    
    //PrintWriter output;
    OBJModel model ;
    OBJModel Smodel ;
    OBJModel tmpmodel ;
    
    PeasyCam cam;
    
    float easing = 0.005;
    float r;
    float k =0.00001;
    int VertCount;
    PVector[] Verts;
    PVector Mouse;
    
    void setup()
    {
      size(800, 800, P3D);
      frameRate(30);
      noStroke();
    
      model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
      model.enableDebug();
      model.scale(100);
      model.translateToCenter();
    
      Smodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
      Smodel.enableDebug();
      Smodel.scale(100);
      Smodel.translateToCenter();
    
    
      tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
      tmpmodel.enableDebug();
      tmpmodel.scale(100);
      tmpmodel.translateToCenter();
    
      //output = createWriter("positions.txt"); 
    
      cam = new PeasyCam(this, width/2, height/2, 0, 994);
    }
    
    
    
    void draw()
    {
      background(129);
      lights();
    
      int VertCount = model.getVertexCount ();
      PVector[] Verts = new PVector[VertCount];
      PVector[] locas = new PVector[VertCount];
      float r =200;
      PVector Mouse = new PVector(mouseX-width/2, mouseY-height/2, 0);
    
    
      cam.setMouseControlled(true);
    
    
    
    
    
    
    
    
    
    
      //println(frameCount);
      pushMatrix();
      translate(width/2, height/2, 0);
    
    
    
      for (int i = 0; i < VertCount; i++) {
        //PVector orgv = model.getVertex(i);
    
        locas[i]= model.getVertex(i);
        Verts[i]= Smodel.getVertex(i);
    
    
        //PVector tmpv = new PVector();
    
    
        if (frameCount> 100) {
    
    
    
          float randX = random(-5, 5);
          float randY = random(-5, 5);
          float randZ = random(-5, 5);
    
          PVector Ran = new PVector(randX, randY, randZ);
    
          //float norX = abs(cos(k)) * randX;
          //float norY = abs(cos(k)) * randY;
          //float norZ = abs(cos(k)) * randZ;
    
    
    
    
    
    
    
    
    
          if (Verts[i].y > Mouse.y  - r/2 && Verts[i].y < Mouse.y  + r/2 && Verts[i].x > Mouse.x  - r/2 && Verts[i].x < Mouse.x  + r/2 && Verts[i].z > Mouse.z  - r/2 && Verts[i].z <  Mouse.z  + r/2) {
            tmpmodel.setVertex(i, locas[i].x, locas[i].y, locas[i].z);
          } else {   
    
    
    
            Verts[i].x+=Ran.x;
            Verts[i].y+=Ran.y;
            Verts[i].z+=Ran.z;
    
            if (Verts[i].x > width/2 || Verts[i].x < -width/2) {
              Verts[i].x+=-Ran.x;
            }
            if (Verts[i].y > height/2 || Verts[i].y < -height/2) {
              Verts[i].y+=-Ran.y;
            }
            if (Verts[i].z < -800/2 || Verts[i].z > 800/2) {  
              Verts[i].z+=-Ran.z;
            }
            tmpmodel.setVertex(i, Verts[i].x, Verts[i].y, Verts[i].z);
          }
          k+=0.0001;
        }
        // output.println("Verts " + Verts[i] + " locas " +locas[i]);
      }
    
      pushMatrix();
      translate(Mouse.x, Mouse.y, Mouse.z);
      noFill();
      stroke(255);
      box(r);
      popMatrix();
    
    
      noStroke();
    
      tmpmodel.draw();
    
      popMatrix();
    
    
    
      pushMatrix();
      translate(width/2, height/2, 0);
      noFill();
      stroke(255);
      box(width, height, 600);
      popMatrix();
      //output.flush(); // Writes the remaining data to the file
      //output.close(); // Finishes the file
    }
    
    void keyPressed() {
      if (keyCode == UP) {
        setup();
      }
    }
    
Sign In or Register to comment.