|
|
|
@ -85,13 +85,13 @@ export class Player { |
|
|
|
this.attachedItem = null; |
|
|
|
|
|
|
|
// Update player's position and rotation to match the camera's current state
|
|
|
|
this.position.copy(this.camera.position); |
|
|
|
this.rotation.setFromQuaternion(this.camera.quaternion, 'YXZ'); |
|
|
|
this.position.copy(this.playerRig.position); |
|
|
|
this.rotation.setFromQuaternion(this.playerRig.quaternion, 'YXZ'); |
|
|
|
|
|
|
|
} else if(e.code == 'KeyF' && this.currentIntItem && !this.attachedItem){ |
|
|
|
this.attachedItem = this.currentIntItem; |
|
|
|
this.attachedItem.isActive = true; |
|
|
|
//console.log("Attached item to player: ", this.attachedItem.object.name);
|
|
|
|
console.log("Attached item to player: ", this.attachedItem.object.name); |
|
|
|
} |
|
|
|
|
|
|
|
if (e.code === 'Space' && this.attachedItem) { |
|
|
|
@ -463,7 +463,7 @@ export class Player { |
|
|
|
ray.ray.direction.set(0, 0, -1).applyMatrix4(new THREE.Matrix4().extractRotation(controllerMatrix)); |
|
|
|
} else { |
|
|
|
// Use camera for desktop interaction ray
|
|
|
|
ray.set(this.camera.position, this.camera.getWorldDirection(new THREE.Vector3())); |
|
|
|
ray.setFromCamera({ x: 0, y: 0 }, this.camera); |
|
|
|
} |
|
|
|
|
|
|
|
const nearbyItems = this.itemList.filter(item => item.object && this.position.distanceTo(item.object.position) < this.maxInteractionDistance); |
|
|
|
@ -473,17 +473,22 @@ export class Player { |
|
|
|
const intersects = ray.intersectObjects(itemObj, true); |
|
|
|
|
|
|
|
if (intersects.length > 0) { |
|
|
|
const intersected = intersects[0].object; |
|
|
|
// Find the item whose object contains the intersected mesh
|
|
|
|
const foundItem = nearbyItems.find(item => { |
|
|
|
let found = false; |
|
|
|
item.object.traverse(child => { |
|
|
|
if (child === intersected) found = true; |
|
|
|
}); |
|
|
|
return found; |
|
|
|
}); |
|
|
|
let intersectedObject = intersects[0].object; |
|
|
|
|
|
|
|
// Traverse up to find the root object that is in our itemObj list
|
|
|
|
let rootObject = intersectedObject; |
|
|
|
while (rootObject.parent && itemObj.indexOf(rootObject) === -1) { |
|
|
|
rootObject = rootObject.parent; |
|
|
|
} |
|
|
|
|
|
|
|
// Find the item that corresponds to this root object
|
|
|
|
const foundItem = nearbyItems.find(item => item.object === rootObject); |
|
|
|
|
|
|
|
if (foundItem) { |
|
|
|
if (this.currentIntItem !== foundItem) { |
|
|
|
// Optional: Add some visual feedback for the newly highlighted item
|
|
|
|
console.log("Hovering over:", foundItem.object.name); |
|
|
|
} |
|
|
|
this.currentIntItem = foundItem; |
|
|
|
} else { |
|
|
|
this.currentIntItem = null; |
|
|
|
@ -497,16 +502,26 @@ export class Player { |
|
|
|
const itemCenter = new THREE.Vector3(); |
|
|
|
new THREE.Box3().setFromObject(this.attachedItem.object).getCenter(itemCenter); |
|
|
|
|
|
|
|
const forward = new THREE.Vector3(0, 0, -1).applyQuaternion(this.camera.quaternion); |
|
|
|
const targetPosition = itemCenter.clone().add(forward.multiplyScalar(2)); |
|
|
|
// Get the camera's world position to correctly calculate the lookAt matrix
|
|
|
|
const cameraWorldPosition = new THREE.Vector3(); |
|
|
|
this.camera.getWorldPosition(cameraWorldPosition); |
|
|
|
|
|
|
|
// Calculate the desired distance from the object
|
|
|
|
const forward = new THREE.Vector3(0, 0, -1).applyQuaternion(this.playerRig.quaternion); |
|
|
|
const desiredCameraPosition = itemCenter.clone().add(forward.multiplyScalar(2)); |
|
|
|
|
|
|
|
this.camera.position.lerp(targetPosition, 0.1); |
|
|
|
// Calculate the target position for the rig by subtracting the camera's local offset
|
|
|
|
// from the desired world position of the camera.
|
|
|
|
const targetPosition = desiredCameraPosition.clone().sub(this.camera.position); |
|
|
|
|
|
|
|
this.playerRig.position.lerp(targetPosition, 0.1); |
|
|
|
|
|
|
|
// The target rotation should make the camera (not the rig) look at the item center.
|
|
|
|
const targetRotation = new THREE.Quaternion().setFromRotationMatrix( |
|
|
|
new THREE.Matrix4().lookAt(this.camera.position, itemCenter, this.camera.up) |
|
|
|
new THREE.Matrix4().lookAt(cameraWorldPosition, itemCenter, this.playerRig.up) |
|
|
|
); |
|
|
|
|
|
|
|
this.camera.quaternion.slerp(targetRotation, 0.1); |
|
|
|
this.playerRig.quaternion.slerp(targetRotation, 0.1); |
|
|
|
} |
|
|
|
|
|
|
|
_updatePlayerMovement(delta) { |
|
|
|
@ -516,7 +531,7 @@ export class Player { |
|
|
|
this.rotation.x = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, this.rotation.x)); |
|
|
|
|
|
|
|
// Only update rotation here. Position will be updated in the main loop after the physics step.
|
|
|
|
this.camera.rotation.copy(this.rotation); |
|
|
|
this.playerRig.rotation.copy(this.rotation); |
|
|
|
|
|
|
|
let direction = new THREE.Vector3(); |
|
|
|
if (this.input.forward) direction.z -= 1; |
|
|
|
@ -531,6 +546,7 @@ export class Player { |
|
|
|
move.applyEuler(this.rotation); |
|
|
|
move.multiplyScalar(this.moveSpeed * delta); |
|
|
|
|
|
|
|
// Updating the position of the RB based on the position calcuted by Rapier
|
|
|
|
const newPosition = this.position.clone().add(move); |
|
|
|
|
|
|
|
if( newPosition.y <= 10 ) newPosition.y = 10; |
|
|
|
@ -540,7 +556,7 @@ export class Player { |
|
|
|
} |
|
|
|
|
|
|
|
update(delta, spawnedObjects) { |
|
|
|
//console.log(`Number of Controllers: ${this.vrControllers.length}`);
|
|
|
|
|
|
|
|
if (this.renderer.xr.isPresenting) { |
|
|
|
this._handleVRJoystick(); |
|
|
|
this._handleVRTeleport(spawnedObjects); |
|
|
|
|