Skip to content

Instantly share code, notes, and snippets.

@aarroyoc
Created August 9, 2013 15:48
Show Gist options
  • Save aarroyoc/6194710 to your computer and use it in GitHub Desktop.
Save aarroyoc/6194710 to your computer and use it in GitHub Desktop.
Bloco Loop with Physijs and FirstPersonControls
/*
* bloco_loop.js
*/
var lastTemp;
var preventLoop=0;
var leftCube;
var rightCube;
var forwardCube;
var backwardCube;
var lastControls;
var lastControlX;
var lasControlZ;
var freezed;
var controlsCenter;
var collision=false;
var firstCollision=true;
var moveLeftBlock=false;
var moveRightBlock=false;
var moveBackwardBlock=false;
var moveForwardBlock=false;
function bloco_loop()
{
var delta=(Date.now()-lastTemp)/1000;
if (delta>0)
{
//cubo.rotation.y += degToRad(90)*delta;
//cubo.rotation.x += degToRad(25)*delta;
if(keys.UP==true)
z-=0.1;
if(keys.DOWN==true)
z+=0.1;
if(keys.LEFT==true)
x-=0.1;
if(keys.RIGHT==true)
x+=0.1;
if(keys.Q==true)
ylook+=1.0;
if(keys.E==true)
ylook-=1.0;
//camara.useQuaternion = true;
//camara.position.set(x, y, z);
//camara.rotation.y = degToRad(ylook);
/*var target={x: x,y: 0.0,z: z};
camara.lookAt(target);*/
/*camera.position.x = (-Math.sin(xlook) * Math.cos(ylook));;
camera.position.y = Math.sin(ylook);
camera.position.z = (Math.cos(xlook+Math.PI / 4) * Math.cos(ylook));*/
//Map limit
if (controls.object.position.z > 2800) controls.moveLeft = false;
if (controls.object.position.z < -2800) controls.moveRight = false;
if (controls.object.position.x > 2800) controls.moveBackward = false;
if (controls.object.position.x < -2800) controls.moveForward = false;
//Check HitBox
var xdesp=0, zdesp=0;
if(controls.moveLeft) zdesp=1;
if(controls.moveRight) zdesp=-1;
if(controls.moveBackward) xdesp=1;
if(controls.moveForward) xdesp=-1;
//var vec=controls.target.clone().sub( controls.object.position ).normalize();
/*var vec=new Array;
vec.push(new THREE.Vector3( 0, 0, -1 ).applyMatrix4(camara.matrixWorld));
vec.push(new THREE.Vector3(0,0,1).applyMatrix4(camara.matrixWorld));
vec.push(new THREE.Vector3(1,0,0).applyMatrix4(camara.matrixWorld));
vec.push(new THREE.Vector3(-1,0,0).applyMatrix4(camara.matrixWorld));
for(var i=0;i<vec.length;i++)
{
//var ray = new THREE.Raycaster( controls.object.position, vec.sub(camara.position).normalize() );
var ray=new THREE.Raycaster(camara.position, vec[i],camara.position,0.5);
var collisionResults = ray.intersectObjects( netrix_list );
//console.log("COLLISION RESULTS:"+collisionResults.length+" COLLISION RESULTS: "+collisionResults[0]+" VEC.LENGTH:"+vec.length());
if ( collisionResults.length > 0 && collisionResults[0].distance > 0.1)
{
preventLoop++;
if(preventLoop==25)
{
controls.object.position.set(0.0,0.0,0.0);
preventLoop=0;
}
if (zdesp == 1) {controls.moveLeft = false;controls.object.position.z+=0.5;}
if (zdesp == -1) {controls.moveRight = false;controls.object.position.z-=0.5;}
if (xdesp == 1) {controls.moveBackward = false;controls.object.position.x+=0.5;}
if (xdesp == -1) {controls.moveForward = false;controls.object.position.x-=0.5;}
}else{
}
}*/
//Crear 4 cubos alrededor de Bloco invisibles. Si uno toca algo, esa posición es desactivada y no puede moverse
moveLeftBlock=false;
moveRightBlock=false;
moveForwardBlock=false;
moveBackwardBlock=false;
if(leftCube==undefined)
{
var matPlayer=new THREE.MeshLambertMaterial({color: 0xFF00FF});
var geoPlayer=new THREE.CubeGeometry(1.0,1.0,1.0);
leftCube=new Physijs.BoxMesh(geoPlayer,Physijs.createMaterial(matPlayer,.8,.3),25);
leftCube.visible=true;
leftCube.position.set(0.0,0.0,-1.1);
leftCube.setCcdMotionThreshold(1);
leftCube.addEventListener("collision",function(object,vel,rot,normal){
//console.log(object);
//console.log(vel);
//console.log(rot);
//console.log(normal);
if(object.name!="Floor V2")
{
collision=true;
moveLeftBlock=true;
//controls.freeze=true;
//bloco_ui.displayText("Oh, no! You're freezing by Dr. Trunkabot. Restart in 5 seconds",5000);
//setTimeout(deFreeze,5000);
/*if (zdesp == 1) {controls.moveRight = true;}
if (zdesp == -1) {controls.moveLeft = true;}
if (xdesp == 1) {controls.moveBackward = true;}
if (xdesp == -1) {controls.moveForward = true;}*/
//controls.object.position=lastControls;
//controls.object.position.x=object.position.x+vel.x;
//controls.object.position.z=object.position.z+vel.z;
//controls.object.position.x=lastControls.x;
//controls.object.position.z=lastControls.z;
//controls.object.position.x=object.position.x;
//controls.object.position.z=object.position.z;
}
});
leftCube.addEventListener("ready",function(){
console.log("Object Ready!");
});
escena.add(leftCube);
rightCube=new Physijs.BoxMesh(geoPlayer,Physijs.createMaterial(matPlayer,.8,.3),25);
rightCube.visible=true;
rightCube.position.set(0.0,0.0,1.1);
rightCube.setCcdMotionThreshold(1);
rightCube.addEventListener("collision",function(object,vel,rot,normal){
if(object.name!="Floor V2")
{
collision=true;
moveRightBlock=true;
}
});
rightCube.addEventListener("ready",function(){
console.log("Object Ready!");
});
escena.add(rightCube);
forwardCube=new Physijs.BoxMesh(geoPlayer,Physijs.createMaterial(matPlayer,.8,.3),25);
forwardCube.visible=false;
forwardCube.position.set(-1.1,0.0,0.0);
forwardCube.setCcdMotionThreshold(1);
forwardCube.addEventListener("collision",function(object,vel,rot,normal){
if(object.name!="Floor V2")
{
collision=true;
moveForwardBlock=true;
}
});
forwardCube.addEventListener("ready",function(){
console.log("Object Ready!");
});
escena.add(forwardCube);
backwardCube=new Physijs.BoxMesh(geoPlayer,Physijs.createMaterial(matPlayer,.8,.3),25);
backwardCube.visible=false;
backwardCube.position.set(1.1,0.0,0.0);
backwardCube.setCcdMotionThreshold(1);
backwardCube.addEventListener("collision",function(object,vel,rot,normal){
if(object.name!="Floor V2")
{
collision=true;
moveBackwardBlock=true;
}
});
backwardCube.addEventListener("ready",function(){
console.log("Object Ready!");
});
escena.add(backwardCube);
//spherePlayer.setLinearVelocity(new THREE.Vector3(1.5,0.0,0.0));
}else{
//spherePlayer.position=controls.object.position;
leftCube.position.set(controls.object.position.x,leftCube.position.y,controls.object.position.z-1.1);
leftCube.__dirtyPosition=true;
rightCube.position.set(controls.object.position.x,rightCube.position.y,controls.object.position.z+1.1);
rightCube.__dirtyPosition=true;
forwardCube.position.set(controls.object.position.x-1.1,forwardCube.position.y,controls.object.position.z);
forwardCube.__dirtyPosition=true;
backwardCube.position.set(controls.object.position.x+1.1,backwardCube.position.y,controls.object.position.z);
backwardCube.__dirtyPosition=true;
//spherePlayer.applyImpulse(25.0,0.0,25.0);
}
if(collision==true)
{
/*if(controls.moveLeft) {controls.moveLeft=false;}
if(controls.moveRight) {controls.moveRight=false;}
if(controls.moveForward) {controls.moveForward=false;}
if(controls.moveBackward) {controls.moveBackward=false;}
preventLoop++;
if(preventLoop==25)
{
controls.object.position.set(0.0,0.0,0.0);
preventLoop=0;
}*/
if(moveLeftBlock) controls.moveLeft=false;
if(moveRightBlock) controls.moveRight=false;
if(moveForwardBlock) controls.moveForward=false;
if(moveBackwardBlock) controls.moveBackward=false;
console.log("BLOCKED: Left->"+moveLeftBlock+":Right->"+moveRightBlock+":Forward->"+moveForwardBlock+":Backward->"+moveBackwardBlock);
}
controls.update(delta);
escena.simulate();
render.render(escena, camara);
}
lastTemp=Date.now();
requestAnimationFrame(bloco_loop);
}
function deFreeze()
{
/*controls.freeze=false;
controls.object.position=controlsCenter;*/
location.reload();
}
window.addEventListener("DOMContentLoaded",bloco_update);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment