diff --git a/ff0fsum4zx.js b/ff0fsum4zx.js index d1dc070..b00dd38 100644 --- a/ff0fsum4zx.js +++ b/ff0fsum4zx.js @@ -16,6 +16,9 @@ this.beforeRender = R3.EntityManager.Instance.findComponentById('7l8ar325qf'); */ this.footballPlayer = R3.EntityManager.Instance.findComponentById('t537n02x0s'); this.football = R3.EntityManager.Instance.findComponentById('umgbzb0ur2'); +this.goalL = R3.EntityManager.Instance.findComponentById('ackykfwyd4'); +this.goalR = R3.EntityManager.Instance.findComponentById('040yhx0atm'); +this.goalWithHoles = R3.EntityManager.Instance.findComponentById('p3oxe63wjm'); /** * Geometry @@ -28,6 +31,11 @@ this.ballGeometry = R3.EntityManager.Instance.findComponentById('dt6jl8kocw'); this.materialWhite = R3.EntityManager.Instance.findComponentById('iualutcl1y'); this.materialBlack = R3.EntityManager.Instance.findComponentById('2g36po454g'); +/** + * Scenes + */ +this.arScene = R3.EntityManager.Instance.findComponentById('grb0df22dd'); + this.playerRotation = { x : 0, y : 0 @@ -47,11 +55,20 @@ this.kickDuration = 0; this.kickDistance = 0; -R3.CustomCode.prototype.calcPosition = function(shotAngle, lookAngle, t) { +this.raycaster = new THREE.Raycaster(); +this.debug = true; +this.gravity = -9.8; -}; +if (this.debug) { + this.arrowHelper = new THREE.ArrowHelper( + new THREE.Vector3(0,0,-1), + new THREE.Vector3(0,0,0), + 1 + ); + this.arScene.instance.add(this.arrowHelper); +} R3.CustomCode.prototype.render = function(delta) { @@ -59,6 +76,16 @@ R3.CustomCode.prototype.render = function(delta) { function(ball) { + if (ball.stopped) { + return; + } + + if (ball.speed <= 0) { + ball.stopped = true; + } + + ball.speed -= delta * 2; + ball.t += delta; var u = new THREE.Vector3( @@ -68,29 +95,115 @@ R3.CustomCode.prototype.render = function(delta) { ); ball.mesh.position.x = ball.s0.x + u.x * ball.t; - ball.mesh.position.y = ball.s0.y + u.y * ball.t + 0.5 * (-9.8) * ball.t * ball.t; + ball.mesh.position.y = ball.s0.y + u.y * ball.t + 0.5 * (this.gravity) * ball.t * ball.t; ball.mesh.position.z = ball.s0.z + u.z * ball.t; - if (ball.mesh.position.y <= 0) { + /** + * Bounce + */ + if (ball.mesh.position.y <= 0.18) { ball.s0.x = ball.mesh.position.x; - ball.s0.y = 0; + ball.s0.y = 0.18; ball.s0.z = ball.mesh.position.z; ball.upAngle *= ball.bounciness; ball.t = 0; } - var v = new THREE.Vector3( - -ball.v.z, - 0, - ball.v.x - ); + /** + * Check for goal collision + */ + var goalDistanceL = this.goalL.position.distanceTo(ball.mesh.position); + var goalDistanceR = this.goalR.position.distanceTo(ball.mesh.position); + + if (goalDistanceL < (this.goalL.geometry.boundingSphere.radius - ball.mesh.geometry.boundingSphere.radius)) { + if (ball.scored === false) { + console.log('left goal!'); + ball.scored = true; + ball.scoredThroughGoal = 'left'; + } + } + + if (goalDistanceR < (this.goalR.geometry.boundingSphere.radius - ball.mesh.geometry.boundingSphere.radius)) { + if (ball.scored === false) { + console.log('right goal!'); + ball.scored = true; + ball.scoredThroughGoal = 'right'; + } + } - ball.mesh.rotateOnAxis(v.normalize(), -0.2); - //object.ball.translateOnAxis(object.v, 1); + /** + * Check for goal reflection + */ + var futureTime = ball.t + delta; - //object.ball.position.y = object.ball.position.y + 0.5 * (-9.8) * object.t * object.t; + ball.s1.x = ball.s0.x + u.x * futureTime; + ball.s1.y = ball.s0.y + u.y * futureTime + 0.5 * (this.gravity) * futureTime * futureTime; + ball.s1.z = ball.s0.z + u.z * futureTime; + + //var distance = ball.mesh.position.distanceTo(ball.s1); + + var direction = ball.s1.sub(ball.mesh.position).normalize(); + + if (this.debug) { + this.arrowHelper.position.copy(ball.mesh.position); + this.arrowHelper.setDirection(direction); + this.arrowHelper.setLength(1); + } + + if ( + !ball.scored && + ball.mesh.position.z < -4 && + ball.mesh.position.z > -5 + ) { + + console.warn('check for collision'); + this.raycaster.set(ball.mesh.position, direction); + this.raycaster.intersectObjects([ + this.goalWithHoles.instance + ]).map( + function(intersect) { + + if (intersect.distance < ball.mesh.geometry.boundingSphere.radius) { + + var normal = new THREE.Vector3( + intersect.object.geometry.getAttribute('normal').array[0], + intersect.object.geometry.getAttribute('normal').array[1], + intersect.object.geometry.getAttribute('normal').array[2] + ); + + var reflect = direction.reflect(normal); + + var angle = direction.angleTo(reflect); + + ball.directionAngle = angle; + ball.s0.x = ball.mesh.position.x; + ball.s0.y = ball.mesh.position.y; + ball.s0.z = ball.mesh.position.z; + ball.upAngle *= ball.bounciness; + ball.speed *= ball.bounciness * 1.5; + ball.t = 0; + console.log('collision', reflect); + + ball.v.copy(direction); + + ball.vRotationAxis.x *= -1; + ball.vRotationAxis.z *= -1; + } + } + ) + } + + // if (ball.mesh.position.z <= -5 && !ball.scored && ball.lastIntersect) { + // console.log(ball.lastIntersect); + // } + + + /** + * Rotate the ball + */ + ball.mesh.rotateOnAxis(ball.vRotationAxis, -ball.speed * 0.04); }.bind(this) @@ -174,6 +287,7 @@ R3.CustomCode.prototype.move = function(data) { ); this.activeBall.scale.copy(this.football.instance.scale); + this.activeBall.position.y = 0.18; this.football.parentScene.instance.add(this.activeBall); } @@ -210,7 +324,7 @@ R3.CustomCode.prototype.move = function(data) { // speed = 1.5; // } - console.log(speed); +// console.log(speed); var v = new THREE.Vector3(0, 0, -1); @@ -221,6 +335,12 @@ R3.CustomCode.prototype.move = function(data) { this.playerRotation.x ); + var vRotationAxis = new THREE.Vector3( + -v.z, + 0, + v.x + ).normalize(); + this.balls.push( { mesh : this.activeBall, @@ -232,9 +352,14 @@ R3.CustomCode.prototype.move = function(data) { kickSpeed : speed, upAngle : Math.PI * speed, v : v, + vRotationAxis : vRotationAxis, t : 0, - s0 : new THREE.Vector3(0,0,0), - bounciness : 0.5 + s0 : new THREE.Vector3(0,0.18,0), + s1 : new THREE.Vector3(0,0.18,0), + bounciness : 0.5, + scored : false, + scoredThroughGoal : null, + stopped :false } ); @@ -246,8 +371,12 @@ R3.CustomCode.prototype.move = function(data) { }.bind(this) +this.footballPlayer.instance.translateY(0.78); +this.football.instance.position.y = 0.18; + this.mouseMove.entityLoaded = this; this.touchMove.entityLoaded = this; this.beforeRender.entityLoaded = this; + //@ sourceURL=entityLoaded.js \ No newline at end of file