Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PhysicsEngine {
- constructor( scene, camera ) {
- // ... other code snippet
- this.scene = scene;
- this.camera = camera;
- // ... other code snippet
- this.bullets = [];
- this.bulletMeshes = [];
- this.shotHandler = ( event ) => this.shot( event );
- }
- initialize() {
- // Find this lien, comment it out and then add the below line
- // sphereBody.position.set( 0, 5, 0 );
- sphereBody.position.set( 10, 0, 10 );
- // ... other code snippet
- this.bindEvents();
- }
- uninitialize() {
- this.clearEvents();
- }
- bindEvents() {
- window.addEventListener(
- 'click',
- this.shotHandler
- );
- }
- clearEvents() {
- window.removeEventListener(
- 'click',
- this.shotHandler
- );
- }
- mock( count ) {
- // Find this lien, comment it out and then add the below line
- // scene.add( boxMesh );
- thia.scene.add( boxMesh );
- }
- shot( event ) {
- if( !event ) return;
- if( this.bullets.length >= 10 ) {
- for( let i = 0; i < this.bullets.length; i++ ) {
- this.bulletMeshes[i].geometry.dispose();
- this.scene.remove( this.bulletMeshes[i] );
- this.world.remove( this.bullets[i] );
- }
- this.bullets.length = 0;
- this.bulletMeshes.length = 0;
- }
- const ballShape = new CANNON.Sphere( 0.2 );
- const ballGeometry = new THREE.SphereGeometry( ballShape.radius, 32, 32 );
- const shootVelocity = 15;
- const ballMass = 20;
- const ballColor = 0x93882f;
- // 取得目前玩家位置
- const playerBody = this. sphereBody;
- let x = playerBody.position.x;
- let y = playerBody.position.y;
- let z = playerBody.position.z;
- // 子彈剛體與網格
- const ammoBody = new CANNON.Body({ mass: ballMass })
- ammoBody.addShape( ballShape );
- const ammoMaterial = new THREE.MeshStandardMaterial({ color: ballColor });
- const ammoMesh = new THREE.Mesh( ballGeometry, ammoMaterial );
- this.world.addBody( ammoBody );
- this.scene.add( ammoMesh );
- ammoMesh.castShadow = true;
- ammoMesh.receiveShadow = true;
- this.bullets.push( ammoBody );
- this.bulletMeshes.push( ammoMesh );
- // 取得滑鼠在網頁上 (x, y) 位置
- const raycaster = new THREE.Raycaster();
- let shootDirection = new THREE.Vector3();
- let mouse = new THREE.Vector2();
- mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouse.y = -( event.clientY / window.innerHeight ) * 2 + 1;
- // 透過 raycaster 取得目前玩家朝向方向
- raycaster.setFromCamera( mouse, this.camera );
- // 取得 raycaster 方向並決定發射方向
- shootDirection.copy( raycaster.ray.direction );
- ammoBody.velocity.set(
- shootDirection.x * shootVelocity,
- shootDirection.y * shootVelocity,
- shootDirection.z * shootVelocity
- );
- // Move the ball outside the player sphere
- x += shootDirection.x * ( this.sphereShape.radius * 1.02 + ballShape.radius );
- y += shootDirection.y * ( this.sphereShape.radius * 1.02 + ballShape.radius );
- z += shootDirection.z * ( this.sphereShape.radius * 1.02 + ballShape.radius );
- ammoBody.position.set( x, y, z );
- ammoMesh.position.set( x, y, z );
- }
- update( navigation ) {
- // ... other code snippet
- // Update bullet mesh positions
- const bullets = this.bullets;
- const bulletMeshes = this.bulletMeshes;
- for( let i = 0; i < bullets.length; i++ ) {
- bulletMeshes[i].position.copy( bullets[i].position );
- bulletMeshes[i].quaternion.copy( bullets[i].quaternion );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement