Last active
December 30, 2015 07:29
-
-
Save cubicleDowns/7796565 to your computer and use it in GitHub Desktop.
Checking for a Tic-Tac-Toe winner using Rays with THREE.js. You can find more at my blog, blog.tempt3d.com.
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
function createRays() { | |
var setup = { | |
xDirection: xDirection = new THREE.Vector3(-1,0,0), | |
yDirection: yDirection = new THREE.Vector3(0,-1,0), | |
zDirection: zDirection = new THREE.Vector3(0,0,-1), | |
xy1Direction: xy1Direction = new THREE.Vector3(-1,-1,0).normalize(), | |
xy2Direction: xy2Direction = new THREE.Vector3(-1,1,0).normalize(), | |
xz1Direction: xz1Direction = new THREE.Vector3(-1,0,-1).normalize(), | |
xz2Direction: xz2Direction = new THREE.Vector3(-1,0,1).normalize(), | |
yz1Direction: yz1Direction = new THREE.Vector3(0,-1,-1).normalize(), | |
yz2Direction: yz2Direction = new THREE.Vector3(0,1,-1).normalize(), | |
xyz1Direction: xyz1Direction = new THREE.Vector3(-1,-1,-1).normalize(), | |
xyz2Direction: xyz2Direction = new THREE.Vector3(1,-1,-1).normalize(), | |
xyz3Direction: xyz3Direction = new THREE.Vector3(-1,-1,1).normalize(), | |
xyz4Direction: xyz4Direction = new THREE.Vector3(1,-1,1).normalize(), | |
length: 20 | |
}; | |
// should be 27 of these guys. | |
create2DVectors(setup); | |
// should be 18 of these guys (for a 3x3x3) | |
create2DDiagonals(setup); | |
// should be 4 of these guys | |
create3DDiagonals(setup); | |
} | |
function create2DVectors (setup) { | |
var i,j, | |
x,y,z, | |
arrow, | |
origin, | |
ray; | |
// create 9 rays looking down X | |
for(i = 0; i < gameDimensions; i++){ | |
for(j = 0; j < gameDimensions; j++){ | |
x = 4 * cubeSeparation; | |
y = (i - 1) * cubeSeparation; | |
z = (j - 1) * cubeSeparation; | |
origin = new THREE.Vector3(x,y,z); | |
arrow = new THREE.ArrowHelper(setup.xDirection, origin, setup.length, 0xFF0000); | |
_demo.scene.add(arrow); | |
_demo.arrows.push(arrow); | |
ray = new THREE.Raycaster(origin, setup.xDirection); | |
ray.num = '6'; | |
rays.push(ray); | |
} | |
} | |
// create 9 rays looking down Y | |
for(i = 0; i < gameDimensions; i++){ | |
for(j = 0; j < gameDimensions; j++){ | |
y = 4 * cubeSeparation; | |
x = (i - 1) * cubeSeparation; | |
z = (j - 1) * cubeSeparation; | |
origin = new THREE.Vector3(x,y,z); | |
arrow = new THREE.ArrowHelper(setup.yDirection, origin, setup.length, 0x00FF00); | |
_demo.scene.add(arrow); | |
_demo.arrows.push(arrow); | |
ray = new THREE.Raycaster(origin, setup.yDirection); | |
ray.num = '6'; | |
rays.push(ray); | |
} | |
} | |
// create 9 rays looking down Z | |
for(i = 0; i < gameDimensions; i++){ | |
for(j = 0; j < gameDimensions; j++){ | |
z = 4 * cubeSeparation; | |
y = (i - 1) * cubeSeparation; | |
x = (j - 1) * cubeSeparation; | |
origin = new THREE.Vector3(x,y,z); | |
arrow = new THREE.ArrowHelper(setup.zDirection, origin, setup.length, 0x0000FF); | |
_demo.scene.add(arrow); | |
_demo.arrows.push(arrow); | |
ray = new THREE.Raycaster(origin, setup.zDirection); | |
ray.num = '6'; | |
rays.push(ray); | |
} | |
} | |
} | |
function create2DDiagonals(setup) { | |
var i,j, | |
x,y,z, | |
arrow1, | |
arrow2, | |
origin1, | |
origin2, | |
ray1, | |
ray2; | |
// create 6 rays for XY diagonals | |
for(i = 0; i < gameDimensions; i++){ | |
x = 4 * cubeSeparation; | |
y = 4 * cubeSeparation; | |
z = (i - 1) * cubeSeparation; | |
origin1 = new THREE.Vector3(x,y,z).add(jigger); | |
origin2 = new THREE.Vector3(x,-y,z).add(jigger); | |
arrow1 = new THREE.ArrowHelper(setup.xy1Direction, origin1, setup.length, 0xCC0000); | |
arrow2 = new THREE.ArrowHelper(setup.xy2Direction, origin2, setup.length, 0xCC0000); | |
_demo.scene.add(arrow1); | |
_demo.scene.add(arrow2); | |
_demo.arrows.push(arrow1); | |
_demo.arrows.push(arrow2); | |
ray1 = new THREE.Raycaster(origin1, setup.xy1Direction); | |
ray2 = new THREE.Raycaster(origin2, setup.xy2Direction); | |
ray1.num = '3'; | |
ray2.num = '3'; | |
rays.push(ray1); | |
rays.push(ray2); | |
} | |
// create 6 rays for XZ diagonals | |
for(i = 0; i < gameDimensions; i++){ | |
x = 4 * cubeSeparation; | |
y = (i - 1) * cubeSeparation; | |
z = 4 * cubeSeparation; | |
origin1 = new THREE.Vector3(x,y,z).add(jigger); | |
origin2 = new THREE.Vector3(x,y,-z).add(jigger); | |
arrow1 = new THREE.ArrowHelper(setup.xz1Direction, origin1, setup.length, 0x00CC00); | |
arrow2 = new THREE.ArrowHelper(setup.xz2Direction, origin2, setup.length, 0x00CC00); | |
_demo.scene.add(arrow1); | |
_demo.scene.add(arrow2); | |
_demo.arrows.push(arrow1); | |
_demo.arrows.push(arrow2); | |
ray1 = new THREE.Raycaster(origin1, setup.xz1Direction); | |
ray2 = new THREE.Raycaster(origin2, setup.xz2Direction); | |
ray1.num = '3'; | |
ray2.num = '3'; | |
rays.push(ray1); | |
rays.push(ray2); | |
} | |
// create 6 rays for YZ diagonals | |
for(i = 0; i < gameDimensions; i++){ | |
x = (i - 1) * cubeSeparation; | |
y = 4 * cubeSeparation; | |
z = 4 * cubeSeparation; | |
origin1 = new THREE.Vector3(x,y,z).add(jigger); | |
origin2 = new THREE.Vector3(x,-y,z).add(jigger); | |
arrow1 = new THREE.ArrowHelper(setup.yz1Direction, origin1, setup.length, 0x0000CC); | |
arrow2 = new THREE.ArrowHelper(setup.yz2Direction, origin2, setup.length, 0x0000CC); | |
_demo.arrows.push(arrow1); | |
_demo.arrows.push(arrow2); | |
_demo.scene.add(arrow1); | |
_demo.scene.add(arrow2); | |
ray1 = new THREE.Raycaster(origin1, setup.yz1Direction); | |
ray2 = new THREE.Raycaster(origin2, setup.yz2Direction); | |
ray1.num = '3'; | |
ray2.num = '3'; | |
rays.push(ray1); | |
rays.push(ray2); | |
} | |
} | |
function create3DDiagonals (setup) { | |
var i,j, | |
x,y,z, | |
arrow1, arrow2, arrow3, arrow4, | |
origin1, origin2, origin3, origin4, | |
ray1, ray2, ray3, ray4, | |
distance = 4 * cubeSeparation; | |
x = distance; | |
y = distance; | |
z = distance; | |
origin1 = new THREE.Vector3(x,y,z); | |
origin2 = new THREE.Vector3(-x,y,z); | |
origin3 = new THREE.Vector3(x,y,-z); | |
origin4 = new THREE.Vector3(-x,y,-z); | |
arrow1 = new THREE.ArrowHelper(setup.xyz1Direction, origin1, setup.length, 0x333333); | |
arrow2 = new THREE.ArrowHelper(setup.xyz2Direction, origin2, setup.length, 0x333333); | |
arrow3 = new THREE.ArrowHelper(setup.xyz3Direction, origin3, setup.length, 0x333333); | |
arrow4 = new THREE.ArrowHelper(setup.xyz4Direction, origin4, setup.length, 0x333333); | |
_demo.arrows.push(arrow1); | |
_demo.arrows.push(arrow2); | |
_demo.arrows.push(arrow3); | |
_demo.arrows.push(arrow4); | |
_demo.scene.add(arrow1); | |
_demo.scene.add(arrow2); | |
_demo.scene.add(arrow3); | |
_demo.scene.add(arrow4); | |
ray1 = new THREE.Raycaster(origin1, setup.xyz1Direction); | |
ray2 = new THREE.Raycaster(origin2, setup.xyz2Direction); | |
ray3 = new THREE.Raycaster(origin3, setup.xyz3Direction); | |
ray4 = new THREE.Raycaster(origin4, setup.xyz4Direction); | |
rays.push(ray1); | |
rays.push(ray2); | |
rays.push(ray3); | |
rays.push(ray4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment