I'm creating a game in three.js. I'm new in this field but I've read a lot of collision documentation. I used raycast to get a collision between my boat contained in the cube and the islands contained in cubes. I tried to solve the problem this way.
collision2 = false;
var originPoint = MovingCube.position.clone();
clearText();
for (var vertexIndex = 0; vertexIndex < MovingCube.geometry.vertices.length; vertexIndex++)
{
var localVertex = MovingCube.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( MovingCube.matrix );
var directionVector = globalVertex.sub( MovingCube.position );
var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
var collisionResults = ray.intersectObjects( collidableMeshList );
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() ) {
appendText(" Hit "),
collision2 = true;
}
}
if ( keyboard.pressed("W") ){
obj.translateZ( moveDistance ),
if (collision2==true){
obj.translateZ( -moveDistance*4 ),
}
}
if ( keyboard.pressed("S") ){
obj.translateZ( - moveDistance ),;
if (collision2==true){
obj.translateZ( moveDistance*4 ),
}
}
if ( keyboard.pressed("A") ){
obj.rotateOnAxis( new THREE.Vector3(0,1,0), rotateAngle),
if (collision2==true){
obj.rotateOnAxis( new THREE.Vector3(0,1,0), -rotateAngle*3),
}
}
if ( keyboard.pressed("D") ){
obj.rotateOnAxis( new THREE.Vector3(0,1,0), -rotateAngle),
if (collision2==true){
obj.rotateOnAxis( new THREE.Vector3(0,1,0), rotateAngle*3),
}
}
Multiply by 3 "moveDistance" and "rotateAngle" so as not to crash my ship in the rays launched by the raycat during the collision. It often works, but sometimes the ship gets stuck or enters the island. I thought about moving the ship a few pixels when it collides with the island based on the face of the cube. Example: Ship collide with the face of the cube on the positive X axis, subtract pixels from the cube position to make the boat move away. But I do not know how to do something like that, it would be a good solution for me. How could I solve the problem of collisions with walls? (I do not want to use any physical engine right now) Thanks for those who will give me help!
EDIT:
I would like to point out which side of the cube (island) collided with the ship. If the face is facing the X axis (positive) then move the boat 40 pixels backwards (obj.position = + 40), this for each face. So you can never let the boat in the cube. I was looking at this example that I think could be useful, but I did not understand how to solve it yet.
https://stemkoski.github.io/Three.js/Mouse-Click.html
This is my cube that contains the boat.
var mats2 = [];
var cubeGeometry = new THREE.CubeGeometry(25,135,121,10,10,10);
for (var i = 0; i < 6; i ++) {
mats2.push(new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe:true } ));
}
MovingCube = new THREE.Mesh( cubeGeometry, mats2 );
scene.add( MovingCube );
collidableMeshList0.push(MovingCube);
