Im new to threejs, so sorry for newbie question.
I’m not sure why my intersect for box3 bounding boxes for my GLTF model and FBX model detect a collision. I drew a helper box around to make sure my GLTF model’s bounding box is moving along. But when I crash into the FBX building’s bounding box, it still does not detect a collision. I’m unsure why this is happening and cant seem to find a google answer for it too.
const buildingboundingBox = new THREE.Box3()
fbxLoader.load( //FBX model for the building
one_story.href,
(object) => {
object.scale.set(0.25, 0.25, 0.25)
object.position.set(60, 0, 0)
object.rotateY(-0.5 * Math.PI)
scene.add(object)
buildingboundingBox.setFromObject(object);
const buildingBBhelper = new THREE.Box3Helper(buildingboundingBox, 0x00ff00);
scene.add(buildingBBhelper);
}, (error) => { console.log(error) }
);
//#region CHARACTERS
//All logics for the character
const raycaster = new THREE.Raycaster();
raycaster.far = 100;
const characterboundingBox = new THREE.Box3()
gltf.load(character3.href, function (gltf) {
model = gltf.scene;
model.position.set(0, 0, 0)
model.scale.set(10, 10, 10)
container.add(model);
mixer = new THREE.AnimationMixer(model);
const clips = gltf.animations;
const idleClip = THREE.AnimationClip.findByName(clips, 'Idle');
const idleAction = mixer.clipAction(idleClip);
const idleShootClip = THREE.AnimationClip.findByName(clips, 'Idle_Gun');
const idleShootAction = mixer.clipAction(idleShootClip);
const walkClip = THREE.AnimationClip.findByName(clips, 'Run');
const walkAction = mixer.clipAction(walkClip);
const walkShootClip = THREE.AnimationClip.findByName(clips, 'Run_Gun');
const walkShootAction = mixer.clipAction(walkShootClip);
idleAction.play();
walkShootAction.stop();
walkAction.stop();
idleShootAction.stop();
let newPosition = container.position.clone().add(new THREE.Vector3(0, 5, 0));
function updateNewPosition() {
newPosition = container.position.clone().add(new THREE.Vector3(0, 5, 0));
}
//setting the bounding box for the character
characterboundingBox.setFromObject(model);
const helper = new THREE.Box3Helper(characterboundingBox, 0xff0000);
container.add(helper);
// Key and mouse events
window.addEventListener("keydown", (e) => {
const { keyCode } = e;
if ((keyCode === 87 || keyCode === 38) && isPlaying) {
// baseActions.idle.weight = 0;
// baseActions.run.weight = 5;
// activateAction(baseActions.run.action);
// activateAction(baseActions.idle.action);
idleAction.stop();
if (mousedown) { walkShootAction.play(); }
else { walkAction.play(); }
movingForward = true;
}
if (keyCode == 27 && isPlaying) {
isPlaying = false;
body.style.cursor = 'default';
}
if (keyCode == 32 && !isPlaying) {
isPlaying = true;
body.style.cursor = 'none';
}
if (characterboundingBox.intersectsBox(buildingboundingBox)) {
console.log("collided");
}
});
window.addEventListener("keyup", (e) => {
const { keyCode } = e;
if (keyCode === 87 || keyCode === 38) {
// baseActions.idle.weight = 1;
// baseActions.run.weight = 0;
// activateAction(baseActions.run.action);
// activateAction(baseActions.idle.action);
walkAction.stop();
walkShootAction.stop();
movingForward = false;
}
});
window.addEventListener("pointerdown", (e) => {
if (!isPlaying) return;
if (movingForward == true) {
idleAction.stop();
walkAction.stop();
idleShootAction.stop();
walkShootAction.play();
} else {
idleAction.stop();
walkAction.stop();
walkShootAction.stop();
idleShootAction.play();
}
// pointer.x = (e.clientX / window.innerWidth) * 2 - 1;
// pointer.y = - (e.clientY / window.innerHeight) * 2 + 1;
// pointer.x = 0;
// pointer.y = 0;
// raycaster.setFromCamera(pointer, camera);
updateNewPosition();
let forward = new THREE.Vector3(0, 0, 1);
forward.applyQuaternion(model.quaternion);
raycaster.set(newPosition, forward);
const intersects = raycaster.intersectObjects(scene.children);
for (let i = 0; i < intersects.length; i++) {
// console.log(intersects[i].object.name);
if (intersects[i].object.name == "Zombie_Arm") {
console.log("hit");
zombie1Health = 0;
}
if (intersects[i].object.name == "Zombie") {
console.log("hit");
zombie2Health = 0;
}
if (intersects[i].object.name == "Zombie_Chubby") {
console.log("hit");
zombie3Health = 0;
}
}
// scene.add(new THREE.ArrowHelper(raycaster.ray.direction, raycaster.ray.origin, 300, 0xff0000));
mousedown = true;
});
window.addEventListener("pointerup", (e) => {
if (movingForward == true) {
walkShootAction.stop();
walkAction.play();
} else {
idleShootAction.stop();
idleAction.play();
}
mousedown = false;
});
window.addEventListener("pointermove", (e) => {
if (isPlaying) {
const { movementX, movementY } = e;
const offset = new THREE.Spherical().setFromVector3(
camera.position.clone().sub(cameraOrigin)
);
const phi = offset.phi - movementY * 0.02;
offset.theta -= movementX * 0.02;
offset.phi = Math.max(0.01, Math.min(0.35 * Math.PI, phi));
camera.position.copy(
cameraOrigin.clone().add(new THREE.Vector3().setFromSpherical(offset))
);
camera.lookAt(container.position.clone().add(cameraOrigin));
}
});
});
//#endregion
function animate() {
// requestAnimationFrame(animate);
var delta = clock.getDelta();
// Check for collisions
if (characterboundingBox.intersectsBox(buildingboundingBox)) {
console.log("Collided with building!");
}
updateZombie1Animation();
updateZombie2Animation();
updateZombie3Animation();
if (mixer) mixer.update(delta);
if (zombMixer) zombMixer.update(delta);
if (zombMixer1) zombMixer1.update(delta);
if (zombMixer2) zombMixer2.update(delta);
if (movingForward) {
// Get the X-Z plane in which camera is looking to move the player
camera.getWorldDirection(tempCameraVector);
const cameraDirection = tempCameraVector.setY(0).normalize();
// Get the X-Z plane in which player is looking to compare with camera
model.getWorldDirection(tempModelVector);
const playerDirection = tempModelVector.setY(0).normalize();
// Get the angle to x-axis. z component is used to compare if the angle is clockwise or anticlockwise since angleTo returns a positive value
const cameraAngle = cameraDirection.angleTo(xAxis) * (cameraDirection.z > 0 ? 1 : -1);
const playerAngle = playerDirection.angleTo(xAxis) * (playerDirection.z > 0 ? 1 : -1);
// Get the angle to rotate the player to face the camera. Clockwise positive
const angleToRotate = playerAngle - cameraAngle;
// Get the shortest angle from clockwise angle to ensure the player always rotates the shortest angle
let sanitisedAngle = angleToRotate;
if (angleToRotate > Math.PI) {
sanitisedAngle = angleToRotate - 2 * Math.PI
}
if (angleToRotate < -Math.PI) {
sanitisedAngle = angleToRotate + 2 * Math.PI
}
// Rotate the model by a tiny value towards the camera direction
model.rotateY(
Math.max(-0.05, Math.min(sanitisedAngle, 0.05))
);
container.position.add(cameraDirection.multiplyScalar(0.05));
camera.lookAt(container.position.clone().add(cameraOrigin));
}
renderer.render(scene, camera)
}
renderer.setAnimationLoop(animate);