Skip to content

Instantly share code, notes, and snippets.

View BNG_Pong_camera
camera = new THREE.PerspectiveCamera(
VIEW_ANGLE,
ASPECT,
NEAR,
FAR);
scene = new THREE.Scene();
// add the camera to the scene
scene.add(camera);
View BNG_Pong_paddlecreate
// set up the paddle vars
paddleWidth = 10;
paddleHeight = 30;
paddleDepth = 10;
paddleQuality = 1;
// set up paddle 1
paddle1 = new THREE.Mesh(
new THREE.CubeGeometry(
paddleWidth,
View BNG_Pong_plane
// create the plane's material
var planeMaterial =
new THREE.MeshLambertMaterial(
{
color: 0x4BD121
});
// create the playing surface plane
var plane = new THREE.Mesh(
new THREE.PlaneGeometry(
View BNG_Pong_pointlight
// // 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;
View BNG_Pong_basicsphere
// ---------------------------------
// Based on Aerotwist's cool tutorial - http://www.aerotwist.com/tutorials/getting-started-with-three-js/
// ---------------------------------
// set up the sphere vars
// lower 'segment' and 'ring' values will increase performance
var radius = 5,
segments = 6,
rings = 6;
View BNG_Pong_Renderer
// 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);
View BNG_Pong_ballvars
// ball's x-direction, y-direction and speed per frame
var ballDirX = 1, ballDirY = 1, ballSpeed = 2;
View BNG_Pong_ballyspeedlimit
// 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;
View BNG_Pong_ballMove
// update ball position over time
ball.position.x += ballDirX * ballSpeed;
ball.position.y += ballDirY * ballSpeed;
View BNG_Pong_wallcoll
// 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;