Skip to content

Instantly share code, notes, and snippets.

@GraemeFulton
Created July 25, 2015 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GraemeFulton/3ba2a85920e4d5e515f1 to your computer and use it in GitHub Desktop.
Save GraemeFulton/3ba2a85920e4d5e515f1 to your computer and use it in GitHub Desktop.
Matrix using bufferGeometry allowing movement forward and backwards
/**
* Terrain array
*/
function TerrainMatrix(){
this.floor = [];
this.tileHeight=100;
this.tileWidth=100;
this.tileRowNumber = 3;
}
/**
* Terrain functions
*/
TerrainMatrix.prototype={
constructor: TerrainMatrix,
/**
* createTerrainMatrix
* @TODO: create the matrix of terrains - need to add 9 bits of terrain
*/
createTerrainMatrix:function(scene, perlinNoise){
var xPos=0;
//we want a 3 by 3 matrix
for(var x = 1; x<4; x+=1){
console.log(x)
if(x==1){
xPos= -this.tileWidth;
}
else if(x==2){
xPos= this.tileWidth;
}
else if (x==3){
xPos = 0
}
//every 100px on the z axis, add a bit of ground
for ( var z= this.tileHeight; z > (this.tileHeight * -this.tileRowNumber); z-=this.tileHeight ) {
//Create the perlin noise for the surface of the ground
var perlinSurface = new PerlinSurface(perlinNoise, this.tileWidth, this.tileHeight);
var ground = perlinSurface.surface;
//rotate 90 degrees around the xaxis so we can see the terrain
ground.rotation.x = -Math.PI/-2;
// Then set the z position to where it is in the loop (distance of camera)
ground.position.z = z;
ground.position.y -=4;
ground.position.x =xPos;
//add the ground to the scene
scene.add(ground);
//finally push it to the floor array
this.floor.push(ground);
}
}
},
/**
* moveWithCamera
* when the camera gets past the first terrain, put the other in front of it
*/
moveWithCamera(camera){
// loop through each star
for(var i=0; i<this.floor.length; i++) {
//if the camera has moved past the entire square, move the square
if((this.floor[i].position.z - this.tileHeight)>camera.position.z){
this.floor[i].position.z-=(this.tileHeight*2);
}
//if the camera has moved past the entire square in the opposite direction, move the square the opposite way
else if((this.floor[i].position.z + this.tileHeight)<camera.position.z){
this.floor[i].position.z+=(this.tileHeight*2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment