better zoom management

This commit is contained in:
2025-11-10 08:01:56 +02:00
parent 22c27a7a9e
commit 03edeaef2d
4 changed files with 35 additions and 16 deletions
+11 -7
View File
@@ -2,13 +2,18 @@ import * as THREE from 'three'
import { QueryFilterFlags } from '@dimforge/rapier3d';
export class CharacterControls {
// temporary data
walkDirection = new THREE.Vector3()
rotateAngle = new THREE.Vector3(0, 1, 0)
rotateQuarternion = new THREE.Quaternion()
cameraTarget = new THREE.Vector3()
cameraY = 3
#cameraZ = 5
get cameraZ(){
return this.#cameraZ;
}
set cameraZ(v){
this.#cameraZ = Math.min(Math.max(v, 2), 10);
}
// constants
fadeDuration = 0.2
@@ -39,7 +44,6 @@ export class CharacterControls {
this.orbitControl = engine.orbitControls
this.camera = engine.camera
//this.updateCameraTarget(new THREE.Vector3(0,1,5))
this.direction = this.model.rotation.y;
this.directionVelocity = 0;
@@ -149,9 +153,9 @@ export class CharacterControls {
let cameraPosition = new THREE.Vector3().copy(this.camera.position)
let cameraDesiredPosition = new THREE.Vector3(
this.model.position.x + 5* Math.sin(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta),
this.model.position.x + this.#cameraZ* Math.sin(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta),
this.cameraY,
this.model.position.z + 5* Math.cos(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta)
this.model.position.z + this.#cameraZ* Math.cos(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta)
)
cameraPosition.lerp(cameraDesiredPosition, delta*2)
+8 -4
View File
@@ -170,10 +170,14 @@ class GameEngine extends THREE.EventDispatcher{
scene.background = new THREE.Color(1, 1, 1);
//console.log('GameEngine started')
renderer.domElement.addEventListener('wheel', (event) => {
gameEngine.camera.zoom -= event.deltaY / 1000;
gameEngine.camera.zoom = Math.max(gameEngine.camera.zoom, .4);
controls.rotateSpeed = 1 / gameEngine.camera.zoom;
gameEngine.camera.updateProjectionMatrix();
if (gameEngine.hero){
gameEngine.hero.characterControls.cameraZ += event.deltaY / 33;
}else{
gameEngine.camera.zoom -= event.deltaY / 1000;
gameEngine.camera.zoom = Math.max(gameEngine.camera.zoom, .4);
controls.rotateSpeed = 1 / gameEngine.camera.zoom;
gameEngine.camera.updateProjectionMatrix();
}
})
await this.initPhysics();
+15 -4
View File
@@ -1,4 +1,5 @@
import * as RAPIER from '@dimforge/rapier3d'
import { Vector3 } from 'three';
class Physics{
constructor(engine){
this.engine = engine
@@ -51,10 +52,20 @@ class Physics{
break
default:{
colliderDesc = RAPIER.ColliderDesc.trimesh(
mesh.geometry.attributes.position.array,
mesh.geometry.index?.array
)
const vertices = [];
const vertex = new Vector3();
const position = mesh.geometry.getAttribute( 'position' );
for ( let i = 0; i < position.count; i ++ ) {
vertex.fromBufferAttribute( position, i );
vertices.push( vertex.x, vertex.y, vertex.z );
}
// if the buffer is non-indexed, generate an index buffer
const indices = mesh.geometry.getIndex() === null
? Uint32Array.from( Array( parseInt( vertices.length / 3 ) ).keys() )
: mesh.geometry.getIndex().array;
colliderDesc = RAPIER.ColliderDesc.trimesh( vertices, indices );
}
break
}