This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we can easily notice shadows if we dynamically move lights during the game | |
spotLight.position.x = ball.position.x; | |
spotLight.position.y = ball.position.y; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
paddle1 = new THREE.Mesh( | |
new THREE.CubeGeometry(paddleWidth, paddleHeight, paddleDepth, paddleQuality, paddleQuality, paddleQuality), | |
paddle1Material); | |
// add the sphere to the scene | |
scene.add(paddle1); | |
paddle1.receiveShadow = true; | |
paddle1.castShadow = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// checks if either player or opponent has reached 7 points | |
function matchScoreCheck() | |
{ | |
// if player has 7 points | |
if (score1 >= maxScore) | |
{ | |
// stop the ball | |
ballSpeed = 0; | |
// write to the banner | |
document.getElementById("scores").innerHTML = "Player wins!"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// game-related variables | |
var score1 = 0, score2 = 0; | |
// you can change this to any positive whole number | |
var maxScore = 7; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// and if ball is travelling towards player (-ve direction) | |
if (ballDirX < 0) | |
{ | |
// stretch the paddle to indicate a hit | |
paddle1.scale.y = 15; | |
// switch direction of ball travel to create bounce | |
ballDirX = -ballDirX; | |
// we impact ball angle when hitting it |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if ball is aligned with paddle1 on x plane | |
// remember the position is the CENTER of the object | |
// we only check between the front and the middle of the paddle (one-way collision) | |
if (ball.position.x <= paddle1.position.x + paddleWidth | |
&& ball.position.x >= paddle1.position.x) | |
{ | |
// and if ball is aligned with paddle1 on y plane | |
if (ball.position.y <= paddle1.position.y + paddleHeight/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set the scene size | |
var WIDTH = 640, | |
HEIGHT = 360; | |
// create a WebGL renderer, camera | |
// and a scene | |
var renderer = new THREE.WebGLRenderer(); | |
// start the renderer | |
renderer.setSize(WIDTH, HEIGHT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// limit ball's y-speed to 2x the x-speed | |
// this is so the ball doesn't speed from left to right super fast | |
// keeps game playable for humans | |
if (ballDirY > ballSpeed * 2) | |
{ | |
ballDirY = ballSpeed * 2; | |
} | |
else if (ballDirY < -ballSpeed * 2) | |
{ | |
ballDirY = -ballSpeed * 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if ball goes off the top side (side of table) | |
if (ball.position.y <= -fieldHeight/2) | |
{ | |
ballDirY = -ballDirY; | |
} | |
// if ball goes off the bottom side (side of table) | |
if (ball.position.y >= fieldHeight/2) | |
{ | |
ballDirY = -ballDirY; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a point light | |
pointLight = new THREE.PointLight(0xF8D898); | |
// set its position | |
pointLight.position.x = -1000; | |
pointLight.position.y = 0; | |
pointLight.position.z = 1000; | |
pointLight.intensity = 2.9; | |
pointLight.distance = 10000; | |
scene.add(pointLight); |