//Initialize algorithm, including the mass lattice void initialize() { int jx, jy, jz; //float vx,vy,vz; int i; int massCount = _dimX * _dimY * _dimZ; //How much to multiply forces by _mEach = (_dimX * _dimY * _dimZ) / mass; //Initialize frame counter to 0 fptr = 0; _lattice = new cMass[massCount]; i = 0; for (jz = 0; jz < _dimZ; jz++) { for (jy = 0; jy < _dimY; jy++) { for (jx = 0; jx < _dimX; jx++) { _lattice[i++] = new cMass(this, jx, jy, jz); } } } i = 0; while (i < massCount) { _lattice[i++].makeLattice(); } }
//Algorithm specific per-frame calculations void perFrame() { fptr++; clearForces(); for (int i = 0; i < _lattice.Length; i++) { cMass ptr = _lattice[i]; ptr.calcCons(); //calculate addition of all the forces ptr.doMovement(); //move according to time and calculated force, includes collision detection } }
//Algorithm specific per-frame calculations void perFrame() { fptr++; clearForces(); //First calculate midpoint for (int i = 0; i < _lattice.Length; i++) { cMass ptr = _lattice[i]; ptr.calcCons_h(); //calculate addition of all the forces ptr.doMovement_h(); //move according to time and calculated force, includes collision detection } //Then calculate over the midpoint to achieve final point for (int i = 0; i < _lattice.Length; i++) { cMass ptr = _lattice[i]; ptr.calcCons(); //calculate addition of all the forces ptr.doMovement(); //move according to time and calculated force, includes collision detection } }
//Calculates the connection variables public cmConnect(cMass cm, int axis, cMass alter) { this.lptr = 0; this.cm = cm; this.alter = alter; //Use Softbody defaults at first k = cm.myBody.defaultK; twoWay = cm.myBody.dTwoWay; this.axis = axis; //Calculate the rest lengths in x/y/z to the next mass float dx = (cm.myBody._dimX > 1 ? cm.myBody._sX / ((float)cm.myBody._dimX - 1f) : 0); float dy = (cm.myBody._dimY > 1 ? cm.myBody._sY / ((float)cm.myBody._dimY - 1f) : 0); float dz = (cm.myBody._dimZ > 1 ? cm.myBody._sZ / ((float)cm.myBody._dimZ - 1f) : 0); //The alternate axis is always the total amount of axis-this axis this.alterAxis = (SoftBody.maxCon - 1) - axis; //Calculate the length, taking the square of the steps in x,y,z in that axis int[,] aa = SoftBody.allAxis; this.length = Mathf.Sqrt(dx * dx * (aa[axis, 0] * aa[axis, 0]) + dy * dy * (aa[axis, 1] * aa[axis, 1]) + dz * dz * (aa[axis, 2] * aa[axis, 2])); }