You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
I noticed where wasn't an easy way to move and look around a scene using mouse and keyboard. Code below might be a good candidate for an easy drop-in SOBA component.
import{Component,Input}from'@angular/core';import{Camera,Vector3}from'three';import{NgtCanvasStore,NgtRender}from'@angular-three/core';//// adapted from three.js games fps example https://github.com/mrdoob/three.js/blob/master/examples/games_fps.html//
@Component({selector: 'pointer-lock-controls',template: '<ngt-group (ready)="ready()" (animateReady)="animate($event.state)"></ngt-group>'})exportclassPointerLockControlsComponent{
@Input()container?: HTMLElement;
@Input()viewerheight=1.5;
@Input()movespeed=1;
@Input()rotatefactor=2000;privatekeyStates=newMap<string,boolean>([]);privatecamera!: Camera;constructor(privatecanvasStore: NgtCanvasStore){}ready(){constcamera=this.canvasStore.get((s)=>s.camera);camera.rotation.order='YXZ';this.camera=camera;// movementdocument.addEventListener('keydown',(event)=>{this.keyStates.set(event.code,true);});document.addEventListener('keyup',(event)=>{this.keyStates.set(event.code,false);});if(!this.container){this.container=document.body;}this.container.addEventListener('mousedown',()=>{document.body.requestPointerLock();});// rotationdocument.body.addEventListener('mousemove',(event)=>{if(document.pointerLockElement===document.body){camera.rotation.y-=event.movementX/this.rotatefactor;camera.rotation.x-=event.movementY/this.rotatefactor;}});}privategetForwardVector(): Vector3{constplayerDirection=newVector3()this.camera.getWorldDirection(playerDirection);playerDirection.y=0;playerDirection.normalize();returnplayerDirection;}privategetSideVector(): Vector3{constplayerDirection=newVector3()this.camera.getWorldDirection(playerDirection);playerDirection.y=0;playerDirection.normalize();playerDirection.cross(this.camera.up);returnplayerDirection;}privateplayerVelocity=newVector3();privateupdateVelocity(deltaTime: number){constspeedDelta=deltaTime*8;if(this.keyStates.get('KeyW')){this.playerVelocity.add(this.getForwardVector().multiplyScalar(speedDelta));}if(this.keyStates.get('KeyS')){this.playerVelocity.add(this.getForwardVector().multiplyScalar(-speedDelta));}if(this.keyStates.get('KeyA')){this.playerVelocity.add(this.getSideVector().multiplyScalar(-speedDelta));}if(this.keyStates.get('KeyD')){this.playerVelocity.add(this.getSideVector().multiplyScalar(speedDelta));}}privatemoveCamera(deltaTime: number){// slow after mouse upletdamping=Math.exp(-3*deltaTime)-1;this.playerVelocity.addScaledVector(this.playerVelocity,damping);constdeltaPosition=this.playerVelocity.clone().multiplyScalar(deltaTime);if(this.camera){this.camera.position.add(deltaPosition);this.camera.position.y=this.viewerheight;}}animate({ delta }: NgtRender){this.updateVelocity(delta*this.movespeed);// check for inputthis.moveCamera(delta);// move player}}
The text was updated successfully, but these errors were encountered:
IRobot1
changed the title
First Person movement code
[Feature] First Person movement code
Mar 11, 2022
I noticed where wasn't an easy way to move and look around a scene using mouse and keyboard. Code below might be a good candidate for an easy drop-in SOBA component.
The text was updated successfully, but these errors were encountered: