interactive objects integration

This commit is contained in:
2025-11-04 16:41:42 +02:00
parent 429ab07db5
commit 4236927537
13 changed files with 178 additions and 243 deletions
+7 -124
View File
@@ -9,7 +9,6 @@ class Hero{
this.object = object;
this.data = data;
this.model = object.scene
//console.log(object)
}
init(gameEngine){
@@ -17,10 +16,6 @@ class Hero{
gameEngine.camera.position.set(0,17,-30)
gameEngine.camera.lookAt(new THREE.Vector3(this.model.position.x, 5, this.model.position.z))
// this.heroCamera = new THREE.Object3D()
// this.model.add(this.heroCamera)
// this.heroCamera.applyMatrix4(gameEngine.camera.matrix)
this.mixer = new AnimationMixer(this.model);
gameEngine.mixers.push( this.mixer );
@@ -32,7 +27,7 @@ class Hero{
this.pointerControls = new PointerControls(gameEngine.camera, this.model, gameEngine.renderer.domElement);
gameEngine.hero = this;
let po = gameEngine.phy.add(this.model, 'kinematicPositionBased', false, undefined, 'capsule', { radius: 0.5, halfHeight: 1})
let po = gameEngine.physics.add(this.model, 'kinematicPositionBased', false, undefined, 'capsule', { radius: 0.5, halfHeight: 1})
po.collider.setTranslationWrtParent({ x: 0, y: 2.0, z: 0 });
//po.collider.setActiveEvents(RAPIER.ActiveEvents.COLLISION_EVENTS);
@@ -59,128 +54,16 @@ class Hero{
let dlt = this.clock.getDelta();
this.delta += dlt;
//if (this.delta > 0.00001){
this.characterControls.update(this.gameEngine.phy.world, this.delta, pc)
this.gameEngine.phy.step()
this.characterControls.update(this.gameEngine.physics.world, this.delta, pc)
this.gameEngine.physics.step()
this.delta = 0;
//}
}
}
return;
let { inputVelocity, velocity, quat, v, targetQuaternion, clock, rotation } = this;
// let pc = this.pointerControls;
// pc.update();
if (this.ready) {
if (this.canJump) {
//walking
this.mixer.update(this.distance / 100)
} else {
//were in the air
this.mixer.update(this.delta)
}
const p = this.characterCollider.position
p.y -= 1
this.model.position.y = this.characterCollider.position.y
this.distance = this.model.position.distanceTo(p)
// const rotationMatrix = new THREE.Matrix4()
// rotationMatrix.lookAt(p, this.model.position, this.model.up)
// targetQuaternion.setFromRotationMatrix(rotationMatrix)
// if (!this.model.quaternion.equals(targetQuaternion)) {
// this.model.quaternion.rotateTowards(targetQuaternion, this.delta * 10)
// }
if (this.canJump) {
inputVelocity.set(0, 0, 0)
if (pc.moveForward) {
inputVelocity.z = 1
}
if (pc.moveBackward) {
inputVelocity.z = -1
}
if (pc.moveLeft) {
//inputVelocity.x = -1
rotation.y+=2*this.delta
}
if (pc.moveRight) {
//inputVelocity.x = 1
rotation.y-=2*this.delta
}
//let d = this.gameEngine.camera.position.distanceTo(v);
inputVelocity.setLength(this.delta * 10)
// apply camera rotation to inputVelocity
//euler.y = this.gameEngine.camera.rotation.y;
quat.setFromEuler(rotation)
inputVelocity.applyQuaternion(quat)
//this.gameEngine.camera.updateProjectionMatrix()
//this.gameEngine.orbitControls.update()
// if (this.gameEngine.orbitControls){
//this.gameEngine.orbitControls.target = v//this.model.position;
// //this.gameEngine.orbitControls.update()
// }
//this.gameEngine.camera.rotateOnAxis(v, rotation.y)
}
if(inputVelocity.x || inputVelocity.z){
this.setAction(this.actionWalk, true)
}else{
this.setAction(this.actionIdle, true)
}
// this.gameEngine.camera.position.lerp(this.characterCollider.position, 1)
// this.gameEngine.camera.rotation.y = rotation.y;
this.model.position.lerp(this.characterCollider.position, 1)
this.model.rotation.y = rotation.y;
}
velocity.set(inputVelocity.x, inputVelocity.y, inputVelocity.z)
this.colliderBody.applyImpulse(velocity)
this.delta = Math.min(clock.getDelta(), 0.1)
this.gameEngine.phy.world.step(this.delta)
//cannonDebugRenderer.update()
this.characterCollider.position.set(this.colliderBody.position.x, this.colliderBody.position.y, this.colliderBody.position.z)
// boxes.forEach((b, i) => {
// boxMeshes[i].position.set(b.position.x, b.position.y, b.position.z)
// boxMeshes[i].quaternion.set(b.quaternion.x, b.quaternion.y, b.quaternion.z, b.quaternion.w)
// })
this.characterCollider.getWorldPosition(v)
//this.gameEngine.cameraPivot.position.lerp(v, 0.1)
// if (this.gameEngine.camera.position.distanceTo(v) > 2){
// this.gameEngine.camera.position.set(v.x+1, 3, v.z + 3);
// }
//this.gameEngine.camera.lookAt(v.x, 3, v.z);
//this.gameEngine.camera.rotation.y = this.model.rotation.y;
// this.gameEngine.camera.updateMatrix();
// this.model.updateMatrix();
let cam = this.gameEngine.camera;
let vv = new THREE.Vector3(), qq = new THREE.Quaternion;
this.heroCamera.getWorldPosition(vv)
//this.heroCamera.getWorldQuaternion(qq);
cam.position.copy(vv)
//cam.setRotationFromQuaternion(qq)
cam.lookAt(new THREE.Vector3(this.model.position.x, 2.8, this.model.position.z))
//console.log(vv)
// cam.matrixAutoUpdate = false;
// cam.matrix.multiplyMatrices(this.model.matrix, cam.matrix)
// //cam.matrix.decompose(cam.position, cam.quaternion, cam.scale)
// //this.gameEngine.camera.updateMatrix();
// cam.matrixAutoUpdate = true;
// this.gameEngine.orbitControls.update()
destroy(){
delete this.gameEngine.hero;
this.gameEngine.mixers.splice(this.gameEngine.mixers.indexOf(this.mixer), 1);
}
}