Hello.
I fight with collisions.
I am using this code for collision detection:
this.collidableMeshList =[];
this.ray_lenght = 50;
var cube_geom= new THREE.BoxGeometry(80,80,180);
var pCube = new THREE.Mesh(cube_geom);
this.isColision = function (x,y,z) {
if(iscolision) ray_lenght =40;
else ray_lenght =100;
this.iscolision=false;
pCube.position.set(player.position.x+x,player.position.y+y,(player.position.z+z)-130)
var originPoint = pCube.position.clone();
for (var vertexIndex = 0; vertexIndex < pCube.geometry.vertices.length; vertexIndex++){
var localVertex = pCube.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( pCube.matrix );
var directionVector = globalVertex.sub( camera.position );
var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize(),20,ray_lenght );
var collisionResults = ray.intersectObjects( this.collidableMeshList, true );
if (collisionResults.length > 0 ) return true;
}
return false;
};
this.iscolision = false;
And such an action to take when it detects a collision:
yawObject.translateX(velocity.x);
yawObject.translateY(velocity.y);
yawObject.translateZ(velocity.z);
this.iscolision = this.isColision(velocity.x*2,velocity.y*2,velocity.z*2)
if(this.iscolision){
yawObject.translateX(- velocity.x);
yawObject.translateY(- velocity.y);
yawObject.translateZ(- velocity.z);
}
else this.iscolision =false;
I understand how it works and the code. There is no problem with that.
The problem starts with logic.
Well, when a collision occurs, the action is interrupted (the player does not move) but the condition still holds, so the player gets stuck in the obstacle.
I tried to change ray vectors but it generates different problems.
I have no idea how to do this.
I would be grateful for any suggestions.