Loading...
Logo
Processing Forum
lowercase's Profile
2 Posts
1 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message
    I have written a program that builds a cube out of multiple boxes.
    Another box impacts on the cube and causes the boxes to spread.
    This code works fine as long as you don't move the camera but if I rotate the camera in any way while the box is impacting with the cube the boxes redraw in very strange places. If the camera is moved after the impact there is no problem.
    I have tried OCD and PeasyCam but they both seem to have the same problem - is this a modelX problem?


    1. import processing.opengl.*;

    2. import peasy.*;
    3. PeasyCam cam;

    4. PVector v1,v2,v3;
    5. //moving box position
    6. float boxposition =-40;
    7. //sets size of cube    
    8. int  gridsize = 9;
    9. //create array for cube
    10. float[][]pntArray = new float[gridsize*gridsize*gridsize][3];


    11. void setup(){  
    12.   size(640, 800,OPENGL);
    13.   stroke(0);

    14. //populate the cube array
    15.   int m = 0;
    16.   for(int i = 0; i<pow(gridsize, 3); i++){ 
    17.         pntArray[i][0] = (floor(i/(pow(gridsize, 2))))*10;
    18.         pntArray[i][2] = (i%gridsize)*10;
    19.         if(pntArray[i][2]==(gridsize-1)*10){
    20.           m+=1;
    21.         }
    22.         if(m==gridsize){
    23.           m=0;
    24.         }
    25.         pntArray[i][1] = m*10;
    26.     }
    27.   
    28.   cam = new PeasyCam(this, 500);
    29.   cam.setMinimumDistance(250);
    30.   cam.setMaximumDistance(1000);

    31.   }

    32. void draw(){  
    33.     background(0);
    34.     lights();
    35.     
    36.     //set moving box to v2  
    37.     PVector v2 = new PVector( 35+boxposition,boxposition,30+boxposition);

    38. // move box
    39.        pushMatrix();
    40.         translate( v2.x,v2.y,v2.z);
    41.         fill(0,255,0);
    42.         box(10);
    43.         popMatrix();
    44.         
    45.   //scroll through boxes in cube
    46.   for(int i = 0; i<pntArray.length; i++){ 

    47.       // set v1 to current cube section
    48.         PVector v1 = new PVector( pntArray[i][0], pntArray[i][1], pntArray[i][2]);
    49.         
    50.        //clacuate distance between moving box and cube section    
    51.        float d = PVector.dist(v1, v2);
    52.        
    53.        fill(255);
    54.         //if block is close
    55.        if(d <20){        
    56.            // create new vector v3 between moving box and cube section
    57.             PVector v3 = PVector.sub(v1, v2 );
    58.             
    59.             // lengthen the vector to create impact 
    60.             v3.mult(1.03);

    61.             // draw the new impacted section
    62.             pushMatrix();
    63.               translate(v2.x,v2.y,v2.z);
    64.               translate( v3.x,v3.y,v3.z);
    65.               box(10);
    66.         
    67.               //calculate actual points in space and save back to Array

    68.                pntArray[i][0]=modelX(0, 0, 0);
    69.                pntArray[i][1]=modelY(0, 0, 0);
    70.                pntArray[i][2]=modelZ(0, 0, 0);
    71.              popMatrix();      
    72.         }        
    73.         //if MOVING BOX  not near to cube section
    74.         else{         
    75.         //draw cube sections that are unaffected  
    76.           pushMatrix();
    77.             translate( pntArray[i][0], pntArray[i][1], pntArray[i][2]);
    78.             box(10);
    79.           popMatrix();      
    80.         }        
    81.       }
    82.       // move box
    83.      boxposition+=0.3; 
    84. }