From 1a84b5c0f0c68a2f4f729bf80026b901691d4823 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 29 Dec 2015 18:13:34 -0800 Subject: [PATCH 1/3] Fixing issue with muzzle flash and hit point being out of sync for pistol --- examples/toybox/pistol/pistol.js | 206 +++++++++++++++++-------------- 1 file changed, 115 insertions(+), 91 deletions(-) diff --git a/examples/toybox/pistol/pistol.js b/examples/toybox/pistol/pistol.js index 8ef26b94c1..df249a0aaf 100644 --- a/examples/toybox/pistol/pistol.js +++ b/examples/toybox/pistol/pistol.js @@ -44,6 +44,7 @@ this.showLaser = false; + }; Pistol.prototype = { @@ -58,20 +59,36 @@ if (!this.equipped) { return; } - this.toggleWithTriggerPressure(); + this.updateProps(); if (this.showLaser) { this.updateLaser(); } + this.toggleWithTriggerPressure(); + + + }, + + updateProps: function() { + var gunProps = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); + this.position = gunProps.position; + this.rotation = gunProps.rotation; + this.firingDirection = Quat.getFront(this.rotation); + var upVec = Quat.getUp(this.rotation); + this.barrelPoint = Vec3.sum(this.position, Vec3.multiply(upVec, this.laserOffsets.y)); + this.laserTip = Vec3.sum(this.barrelPoint, Vec3.multiply(this.firingDirection, this.laserLength)); + this.barrelPoint = Vec3.sum(this.barrelPoint, Vec3.multiply(this.firingDirection, this.firingOffsets.z)) + var pickRay = { + origin: this.barrelPoint, + direction: this.firingDirection + }; }, toggleWithTriggerPressure: function() { this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[this.hand]); if (this.triggerValue < RELOAD_THRESHOLD) { - // print('RELOAD'); this.canShoot = true; } if (this.canShoot === true && this.triggerValue === 1) { - // print('SHOOT'); this.fire(); this.canShoot = false; } @@ -91,17 +108,10 @@ }, updateLaser: function() { - var gunProps = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); - var position = gunProps.position; - var rotation = gunProps.rotation; - this.firingDirection = Quat.getFront(rotation); - var upVec = Quat.getUp(rotation); - this.barrelPoint = Vec3.sum(position, Vec3.multiply(upVec, this.laserOffsets.y)); - var laserTip = Vec3.sum(this.barrelPoint, Vec3.multiply(this.firingDirection, this.laserLength)); - this.barrelPoint = Vec3.sum(this.barrelPoint, Vec3.multiply(this.firingDirection, this.firingOffsets.z)) + Overlays.editOverlay(this.laser, { start: this.barrelPoint, - end: laserTip, + end: this.laserTip, alpha: 1 }); }, @@ -114,19 +124,6 @@ }); }, - preload: function(entityID) { - this.entityID = entityID; - // this.initControllerMapping(); - this.laser = Overlays.addOverlay("line3d", { - start: ZERO_VECTOR, - end: ZERO_VECTOR, - color: COLORS.RED, - alpha: 1, - visible: true, - lineWidth: 2 - }); - }, - triggerPress: function(hand, value) { if (this.hand === hand && value === 1) { //We are pulling trigger on the hand we have the gun in, so fire @@ -135,17 +132,18 @@ }, fire: function() { - var pickRay = { - origin: this.barrelPoint, - direction: this.firingDirection - }; + Audio.playSound(this.fireSound, { position: this.barrelPoint, volume: this.fireVolume }); + var pickRay = { + origin: this.barrelPoint, + direction: this.firingDirection + }; this.createGunFireEffect(this.barrelPoint) - var intersection = Entities.findRayIntersectionBlocking(pickRay, true); + var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects) { this.createEntityHitEffect(intersection.intersection); if (Math.random() < this.playRichochetSoundChance) { @@ -170,11 +168,11 @@ }, createEntityHitEffect: function(position) { - var flash = Entities.addEntity({ + var sparks = Entities.addEntity({ type: "ParticleEffect", position: position, lifetime: 4, - "name": "Flash Emitter", + "name": "Sparks Emitter", "color": { red: 228, green: 128, @@ -228,7 +226,7 @@ }); Script.setTimeout(function() { - Entities.editEntity(flash, { + Entities.editEntity(sparks, { isEmitting: false }); }, 100); @@ -282,70 +280,96 @@ }); }, 100); - var flash = Entities.addEntity({ - type: "ParticleEffect", - position: position, - lifetime: 4, - "name": "Muzzle Flash", - "color": { - red: 228, - green: 128, - blue: 12 - }, - "maxParticles": 1000, - "lifespan": 0.1, - "emitRate": 1000, - "emitSpeed": 0.5, - "speedSpread": 0, - "emitOrientation": { - "x": -0.4, - "y": 1, - "z": -0.2, - "w": 0.7071068286895752 - }, - "emitDimensions": { - "x": 0, - "y": 0, - "z": 0 - }, - "polarStart": 0, - "polarFinish": Math.PI, - "azimuthStart": -3.1415927410125732, - "azimuthFinish": 2, - "emitAcceleration": { - "x": 0, - "y": 0, - "z": 0 - }, - "accelerationSpread": { - "x": 0, - "y": 0, - "z": 0 - }, - "particleRadius": 0.05, - "radiusSpread": 0.01, - "radiusStart": 0.05, - "radiusFinish": 0.05, - "colorSpread": { - red: 100, - green: 100, - blue: 20 - }, - "alpha": 1, - "alphaSpread": 0, - "alphaStart": 0, - "alphaFinish": 0, - "additiveBlending": true, - "textures": "http://ericrius1.github.io/PartiArt/assets/star.png" + Entities.editEntity(this.flash, { + isEmitting: true }); - Script.setTimeout(function() { - Entities.editEntity(flash, { + Entities.editEntity(_this.flash, { isEmitting: false }); }, 100) - } + }, + + preload: function(entityID) { + this.entityID = entityID; + this.laser = Overlays.addOverlay("line3d", { + start: ZERO_VECTOR, + end: ZERO_VECTOR, + color: COLORS.RED, + alpha: 1, + visible: true, + lineWidth: 2 + }); + + var gunProps = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); + var position = gunProps.position; + var rotation = gunProps.rotation; + this.firingDirection = Quat.getFront(rotation); + var upVec = Quat.getUp(rotation); + this.barrelPoint = Vec3.sum(position, Vec3.multiply(upVec, this.laserOffsets.y)); + this.barrelPoint = Vec3.sum(this.barrelPoint, Vec3.multiply(this.firingDirection, this.firingOffsets.z)) + + // this.flash = Entities.addEntity({ + // type: "ParticleEffect", + // parentID: this.entityID, + // position: this.barrelPoint, + // "name": "Muzzle Flash", + // // isEmitting: false, + // "color": { + // red: 228, + // green: 128, + // blue: 12 + // }, + // "maxParticles": 1000, + // "lifespan": 0.1, + // "emitRate": 1000, + // "emitSpeed": 0.5, + // "speedSpread": 0, + // "emitOrientation": { + // "x": -0.4, + // "y": 1, + // "z": -0.2, + // "w": 0.7071068286895752 + // }, + // "emitDimensions": { + // "x": 0, + // "y": 0, + // "z": 0 + // }, + // "polarStart": 0, + // "polarFinish": Math.PI, + // "azimuthStart": -3.1415927410125732, + // "azimuthFinish": 2, + // "emitAcceleration": { + // "x": 0, + // "y": 0, + // "z": 0 + // }, + // "accelerationSpread": { + // "x": 0, + // "y": 0, + // "z": 0 + // }, + // "particleRadius": 0.05, + // "radiusSpread": 0.01, + // "radiusStart": 0.05, + // "radiusFinish": 0.05, + // "colorSpread": { + // red: 100, + // green: 100, + // blue: 20 + // }, + // "alpha": 1, + // "alphaSpread": 0, + // "alphaStart": 0, + // "alphaFinish": 0, + // "additiveBlending": true, + // "textures": "http://ericrius1.github.io/PartiArt/assets/star.png" + // }); + + }, + }; From 4cc94b44ba0f8ed1320f6dcb35e1cc67ca362292 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 29 Dec 2015 18:30:07 -0800 Subject: [PATCH 2/3] muzzle is in sync --- examples/toybox/pistol/pistol.js | 140 +++++++++++++++---------------- 1 file changed, 69 insertions(+), 71 deletions(-) diff --git a/examples/toybox/pistol/pistol.js b/examples/toybox/pistol/pistol.js index df249a0aaf..372c704219 100644 --- a/examples/toybox/pistol/pistol.js +++ b/examples/toybox/pistol/pistol.js @@ -29,20 +29,12 @@ this.equipped = false; this.forceMultiplier = 1; this.laserLength = 100; - this.laserOffsets = { - y: .095 - }; - this.firingOffsets = { - z: 0.16 - } + this.fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); this.ricochetSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/Ricochet.L.wav"); this.playRichochetSoundChance = 0.1; this.fireVolume = 0.2; this.bulletForce = 10; - - - this.showLaser = false; }; @@ -143,7 +135,7 @@ direction: this.firingDirection }; this.createGunFireEffect(this.barrelPoint) - var intersection = Entities.findRayIntersection(pickRay, true); + var intersection = Entities.findRayIntersectionBlocking(pickRay, true); if (intersection.intersects) { this.createEntityHitEffect(intersection.intersection); if (Math.random() < this.playRichochetSoundChance) { @@ -301,76 +293,82 @@ visible: true, lineWidth: 2 }); - + this.laserOffsets = { + y: .095 + }; + this.firingOffsets = { + z: 0.16 + } var gunProps = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); var position = gunProps.position; - var rotation = gunProps.rotation; + var rotation = Quat.fromPitchYawRollDegrees(0, 0, 0); this.firingDirection = Quat.getFront(rotation); var upVec = Quat.getUp(rotation); this.barrelPoint = Vec3.sum(position, Vec3.multiply(upVec, this.laserOffsets.y)); this.barrelPoint = Vec3.sum(this.barrelPoint, Vec3.multiply(this.firingDirection, this.firingOffsets.z)) - // this.flash = Entities.addEntity({ - // type: "ParticleEffect", - // parentID: this.entityID, - // position: this.barrelPoint, - // "name": "Muzzle Flash", - // // isEmitting: false, - // "color": { - // red: 228, - // green: 128, - // blue: 12 - // }, - // "maxParticles": 1000, - // "lifespan": 0.1, - // "emitRate": 1000, - // "emitSpeed": 0.5, - // "speedSpread": 0, - // "emitOrientation": { - // "x": -0.4, - // "y": 1, - // "z": -0.2, - // "w": 0.7071068286895752 - // }, - // "emitDimensions": { - // "x": 0, - // "y": 0, - // "z": 0 - // }, - // "polarStart": 0, - // "polarFinish": Math.PI, - // "azimuthStart": -3.1415927410125732, - // "azimuthFinish": 2, - // "emitAcceleration": { - // "x": 0, - // "y": 0, - // "z": 0 - // }, - // "accelerationSpread": { - // "x": 0, - // "y": 0, - // "z": 0 - // }, - // "particleRadius": 0.05, - // "radiusSpread": 0.01, - // "radiusStart": 0.05, - // "radiusFinish": 0.05, - // "colorSpread": { - // red: 100, - // green: 100, - // blue: 20 - // }, - // "alpha": 1, - // "alphaSpread": 0, - // "alphaStart": 0, - // "alphaFinish": 0, - // "additiveBlending": true, - // "textures": "http://ericrius1.github.io/PartiArt/assets/star.png" - // }); + this.flash = Entities.addEntity({ + type: "ParticleEffect", + position: this.barrelPoint, + "name": "Muzzle Flash", + isEmitting: false, + "color": { + red: 228, + green: 128, + blue: 12 + }, + "maxParticles": 1000, + "lifespan": 0.1, + "emitRate": 1000, + "emitSpeed": 0.5, + "speedSpread": 0, + "emitOrientation": { + "x": -0.4, + "y": 1, + "z": -0.2, + "w": 0.7071068286895752 + }, + "emitDimensions": { + "x": 0, + "y": 0, + "z": 0 + }, + "polarStart": 0, + "polarFinish": Math.PI, + "azimuthStart": -3.1415927410125732, + "azimuthFinish": 2, + "emitAcceleration": { + "x": 0, + "y": 0, + "z": 0 + }, + "accelerationSpread": { + "x": 0, + "y": 0, + "z": 0 + }, + "particleRadius": 0.05, + "radiusSpread": 0.01, + "radiusStart": 0.05, + "radiusFinish": 0.05, + "colorSpread": { + red: 100, + green: 100, + blue: 20 + }, + "alpha": 1, + "alphaSpread": 0, + "alphaStart": 0, + "alphaFinish": 0, + "additiveBlending": true, + "textures": "http://ericrius1.github.io/PartiArt/assets/star.png" + }); + + Script.setTimeout(function() { + Entities.editEntity(_this.flash, {parentID: _this.entityID}); + }, 500) }, - - }; // entity scripts always need to return a newly constructed object of our type From 5caa6cbdbf99b1531d757976b539c6606b497b9b Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 29 Dec 2015 18:38:57 -0800 Subject: [PATCH 3/3] Update pistol.js leading zeroes --- examples/toybox/pistol/pistol.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/toybox/pistol/pistol.js b/examples/toybox/pistol/pistol.js index 372c704219..7fb05d992f 100644 --- a/examples/toybox/pistol/pistol.js +++ b/examples/toybox/pistol/pistol.js @@ -251,11 +251,11 @@ "z": 0 }, "accelerationSpread": { - "x": .2, + "x": 0.2, "y": 0, - "z": .2 + "z": 0.2 }, - "radiusSpread": .04, + "radiusSpread": 0.04, "particleRadius": 0.07, "radiusStart": 0.07, "radiusFinish": 0.07, @@ -294,7 +294,7 @@ lineWidth: 2 }); this.laserOffsets = { - y: .095 + y: 0.095 }; this.firingOffsets = { z: 0.16 @@ -373,4 +373,4 @@ // entity scripts always need to return a newly constructed object of our type return new Pistol(); -}); \ No newline at end of file +});