camera movement altering transformations
in
Contributed Library Questions
•
2 years ago
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?
- import processing.opengl.*;
- import peasy.*;
- PeasyCam cam;
- PVector v1,v2,v3;
- //moving box position
- float boxposition =-40;
- //sets size of cube
- int gridsize = 9;
- //create array for cube
- float[][]pntArray = new float[gridsize*gridsize*gridsize][3];
- void setup(){
- size(640, 800,OPENGL);
- stroke(0);
- //populate the cube array
- int m = 0;
- for(int i = 0; i<pow(gridsize, 3); i++){
- pntArray[i][0] = (floor(i/(pow(gridsize, 2))))*10;
- pntArray[i][2] = (i%gridsize)*10;
- if(pntArray[i][2]==(gridsize-1)*10){
- m+=1;
- }
- if(m==gridsize){
- m=0;
- }
- pntArray[i][1] = m*10;
- }
- cam = new PeasyCam(this, 500);
- cam.setMinimumDistance(250);
- cam.setMaximumDistance(1000);
- }
- void draw(){
- background(0);
- lights();
- //set moving box to v2
- PVector v2 = new PVector( 35+boxposition,boxposition,30+boxposition);
- // move box
- pushMatrix();
- translate( v2.x,v2.y,v2.z);
- fill(0,255,0);
- box(10);
- popMatrix();
- //scroll through boxes in cube
- for(int i = 0; i<pntArray.length; i++){
- // set v1 to current cube section
- PVector v1 = new PVector( pntArray[i][0], pntArray[i][1], pntArray[i][2]);
- //clacuate distance between moving box and cube section
- float d = PVector.dist(v1, v2);
- fill(255);
- //if block is close
- if(d <20){
- // create new vector v3 between moving box and cube section
- PVector v3 = PVector.sub(v1, v2 );
- // lengthen the vector to create impact
- v3.mult(1.03);
- // draw the new impacted section
- pushMatrix();
- translate(v2.x,v2.y,v2.z);
- translate( v3.x,v3.y,v3.z);
- box(10);
- //calculate actual points in space and save back to Array
- pntArray[i][0]=modelX(0, 0, 0);
- pntArray[i][1]=modelY(0, 0, 0);
- pntArray[i][2]=modelZ(0, 0, 0);
- popMatrix();
- }
- //if MOVING BOX not near to cube section
- else{
- //draw cube sections that are unaffected
- pushMatrix();
- translate( pntArray[i][0], pntArray[i][1], pntArray[i][2]);
- box(10);
- popMatrix();
- }
- }
- // move box
- boxposition+=0.3;
- }
1