Hello. I have this game. I’m getting trouble with checking the material, so… there is empty cubes on the scene, but it’s not relevant. The trouble is that, when I try to check the material with this form:
check_object=(x,y,z)=>{
checked=false
for(i1 in cube){
if(distance_to_object(x,y,z,cube[i1].position.x,cube[i1].position.y,cube[i1].position.z)<0.4){
if(cube[i1].material==dirt_material||cube[i1].material==grass_material||cube[i1].material==trunk_complete_material||cube[i1].material==leaves_material)
checked=true
}
}
return checked
}
the checking process doesnt work, so this is my code:
https://jsfiddle.net/DrCristianstein/o4g187kc/1/
<link rel="icon" type="image/png" href="http://i66.tinypic.com/102qp9g.jpg">
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/100/three.js"></script>
<script src="perlin.js"></script>
<script>
k=[]
onkeydown=onkeyup=(e)=>{k[e.keyCode]=e.type=="keydown"}
onload=()=>{
d={}
w=Math.floor(Math.random()*1000000)
cube={}
chunk={}
xa=0
ya=0
document.body.requestPointerLock=document.body.requestPointerLock||document.body.mozRequestPointerLock
box=new THREE.BoxGeometry(1,1,1)
loader=new THREE.TextureLoader()
box_texture=loader.load("images/box.jpg")
brick_texture=loader.load("images/brick.jpg")
stone_texture=loader.load("images/stone.jpg")
leaves_texture=loader.load("images/leaves.jpg")
dirt_texture=loader.load("images/dirt.jpg")
trunk_side_texture=loader.load("images/trunk.jpg")
trunk_up_texture=loader.load("images/trunk_up.jpg")
grass_texture=loader.load("images/grass.jpg")
limestone_texture=loader.load("images/limestone.jpg")
granite_texture=loader.load("images/granite.png")
magma_texture=loader.load("images/magma.jpg")
mossy_rock_texture=loader.load("images/mossy_rock_texture.jpg")
box_material=new THREE.MeshPhysicalMaterial({map:box_texture})
bricks_material=new THREE.MeshPhysicalMaterial({map:brick_texture})
stone_material=new THREE.MeshPhysicalMaterial({map:stone_texture})
leaves_material=new THREE.MeshPhysicalMaterial({map:leaves_texture})
dirt_material=new THREE.MeshPhysicalMaterial({map:dirt_texture})
trunk_side_material=new THREE.MeshPhysicalMaterial({map:trunk_side_texture})
trunk_up_material=new THREE.MeshPhysicalMaterial({map:trunk_up_texture})
grass_material=new THREE.MeshPhysicalMaterial({map:grass_texture})
limestone_material=new THREE.MeshPhysicalMaterial({map:limestone_texture})
granite_material=new THREE.MeshPhysicalMaterial({map:granite_texture})
magma_material=new THREE.MeshPhysicalMaterial({map:magma_texture})
mossy_rock_material=new THREE.MeshPhysicalMaterial({map:mossy_rock_texture})
trunk_material_array=[]
trunk_material_array.push(trunk_side_material)
trunk_material_array.push(trunk_side_material)
trunk_material_array.push(trunk_up_material)
trunk_material_array.push(trunk_up_material)
trunk_material_array.push(trunk_side_material)
trunk_material_array.push(trunk_side_material)
trunk_complete_material=new THREE.MeshFaceMaterial(trunk_material_array)
document.title="DrCris' infinite maze."
document.body.style.margin=0
renderer=new THREE.WebGLRenderer()
document.body.appendChild(renderer.domElement)
camera=new THREE.PerspectiveCamera()
camera.xs=0
camera.ys=0
camera.zs=0
camera.near=0.1
camera.far=10000
scene=new THREE.Scene()
render()
setInterval(update,20)
scene.background=new THREE.Color("rgb(0,180,255)")
scene.fog=new THREE.Fog("rgb(0,180,255)",0.01,100)
camera.position.set(0,5,0)
sunlight1=new THREE.HemisphereLight(2,"rgba(255,255,200,1)")
scene.add(sunlight1)
sunlight2=new THREE.DirectionalLight("rgba(255,255,200,1)",2)
sunlight2.position.set(0,1,0)
sunlight2.target.position.set(0,0,0)
scene.add(sunlight2)
}
gchunk=(cx,cy,cz,chunkSize,chunkId)=>{
chunkId=Math.random()
chunk[chunkId]={}
chunk[chunkId].allDirt=new THREE.Geometry()
chunk[chunkId].all_dirtMesh=new THREE.Mesh(chunk[chunkId].allDirt,dirt_material)
scene.add(chunk[chunkId].all_dirtMesh)
chunk[chunkId].allGrass=new THREE.Geometry()
chunk[chunkId].all_grassMesh=new THREE.Mesh(chunk[chunkId].allGrass,grass_material)
scene.add(chunk[chunkId].all_grassMesh)
chunk[chunkId].allLeaves=new THREE.Geometry()
chunk[chunkId].all_leavesMesh=new THREE.Mesh(chunk[chunkId].allLeaves,leaves_material)
scene.add(chunk[chunkId].all_leavesMesh)
chunk[chunkId].allTrunks=new THREE.Geometry()
chunk[chunkId].all_trunksMesh=new THREE.Mesh(chunk[chunkId].allTrunks,trunk_complete_material)
scene.add(chunk[chunkId].all_trunksMesh)
for(x=cx-chunkSize;x<cx+chunkSize;x++){
for(y=cy-chunkSize;y<cy+chunkSize;y++){
for(z=cz-chunkSize;z<cz+chunkSize;z++){
surface=noise.simplex2(x/40,z/40)*10
//Layer of dirt:
if(y<surface&&noise3d(x/15,y/15,z/15)<7/8){
i1=Math.random()
cube[i1]=new THREE.Mesh(box)
cube[i1].position.set(x,y,z)
chunk[chunkId].allDirt.mergeMesh(cube[i1])
}
//Layer of grass:
if(Math.floor(y)==Math.floor(surface)&&noise3d(x/15,y/15,z/15)<7/8){
i1=Math.random()
cube[i1]=new THREE.Mesh(box)
cube[i1].position.set(x,y,z)
chunk[chunkId].allGrass.mergeMesh(cube[i1])
}
//Layer of bushes:
if(Math.floor(y)==Math.floor(surface+1)&&Math.random()<0.25&&noise3d(x/15,y/15,z/15)<7/8){
i1=Math.random()
cube[i1]=new THREE.Mesh(box)
cube[i1].position.set(x,y,z)
chunk[chunkId].allLeaves.mergeMesh(cube[i1])
}
//Layer of trunks:
if(Math.floor(y)==Math.floor(surface+1)&&Math.random()<0.05&&noise3d(x/15,y/15,z/15)<7/8){
for(i3=0;i3<30;i3++){
i2=Math.random()
cube[i2]=new THREE.Mesh(box)
cube[i2].position.set(x,y+i3,z)
chunk[chunkId].allTrunks.mergeMesh(cube[i2])
}
}
//Layer of leaves:
if(surface+8<y&&y<surface+32&&Math.random()<0.5){
i1=Math.random()
cube[i1]=new THREE.Mesh(box)
cube[i1].position.set(x,y,z)
chunk[chunkId].allLeaves.mergeMesh(cube[i1])
}
}
}
}
}
deChunk=(i1)=>{
scene.remove(chunk[i1].all_dirtMesh)
scene.remove(chunk[i1].all_grassMesh)
scene.remove(chunk[i1].all_leavesMesh)
}
distance_to_object=(x6,y6,z6,x5,y5,z5)=>{
return Math.pow(((x6-x5)*(x6-x5)+(y6-y5)*(y6-y5)+(z6-z5)*(z6-z5)),0.5)
}
check_into_object=(x4,y4,z4,x3,y3,z3)=>{
if(
(x4-0.4<x3+0.4&&x4+0.4>x3-0.4)
&&
(y4-0.4<y3+0.4&&y4+0.4>y3-0.4)
&&
(z4-0.4<z3+0.4&&z4+0.4>z3-0.4)
){
return true
}else{
return false
}
}
check_into_square=(x,y,z,x2,y2,z2,r)=>{
if(
(x-r<x2+r&&x+r>x2-r)
&&
(y-r<y2+r&&y+r>y2-r)
&&
(z-r<z2+r&&z+r>z2-r)
){
return true
}else{
return false
}
}
check_object=(x,y,z)=>{
checked=false
for(i1 in cube){
if(distance_to_object(x,y,z,cube[i1].position.x,cube[i1].position.y,cube[i1].position.z)<0.4){
if(cube[i1].material==dirt_material||cube[i1].material==grass_material||cube[i1].material==trunk_complete_material||cube[i1].material==leaves_material)
checked=true
}
}
return checked
}
render=()=>{
renderer.setSize(innerWidth,innerHeight)
camera.aspect=innerWidth/innerHeight
camera.updateProjectionMatrix()
requestAnimationFrame(render)
renderer.render(scene,camera)
}
onmousedown=()=>{
if(document.pointerLockElement===document.body||
document.mozPointerLockElement===document.body){
}else{
document.body.requestPointerLock()
}
}
onmousemove=(event)=>{
if(document.pointerLockElement===document.body||
document.mozPointerLockElement===document.body){
xa-=0.01*event.movementX
if(-1.5<ya&&0<event.movementY){
ya-=0.01*event.movementY
}
if(ya<1.5&&event.movementY<0){
ya-=0.01*event.movementY
}
}
}
update=()=>{
//This update the player.
if(-1.5>ya){ya=-1.5}
if(1.5<ya){ya=1.5}
camera.position.x+=camera.xs
camera.position.y+=camera.ys
camera.position.z+=camera.zs
camera.xs*=7/8
//camera.ys*=7/8
camera.zs*=7/8
if(-0.2<camera.ys){camera.ys-=0.005}
camera.lookAt(
camera.position.x+Math.sin(xa)*Math.cos(ya),
camera.position.y+Math.sin(ya),
camera.position.z+Math.cos(xa)*Math.cos(ya)
)
if(k[65]){
camera.xs+=0.005*Math.cos(xa)
camera.zs-=0.005*Math.sin(xa)
}
if(k[87]){
camera.xs+=0.005*Math.sin(xa)
camera.zs+=0.005*Math.cos(xa)
}
if(k[68]){
camera.xs-=0.005*Math.cos(xa)
camera.zs+=0.005*Math.sin(xa)
}
if(k[83]){
camera.xs-=0.005*Math.sin(xa)
camera.zs-=0.005*Math.cos(xa)
}
if(k[32]){
//camera.ys+=0.005
if(check_object(camera.position.x,camera.position.y-0.5,camera.position.z)){camera.ys=0.08}
}
if(k[88]){
//camera.ys-=0.005
}
//This makes the solid collision.
if(check_object(camera.position.x+camera.xs,camera.position.y,camera.position.z)){
camera.xs=0
}
if(check_object(camera.position.x,camera.position.y+camera.ys,camera.position.z)){
camera.ys=0
}
if(check_object(camera.position.x,camera.position.y,camera.position.z+camera.zs)){
camera.zs=0
}
//This updates the generation.
render_distance=16
chunk_size=4
fixed_render_distance=Math.floor(render_distance/(chunk_size*2))*(chunk_size*2)
camera.x=camera.position.x-16
camera.z=camera.position.z-16
camera.x=Math.floor(camera.position.x/(chunk_size*2))*(chunk_size*2)
camera.y=Math.floor(camera.position.y/(chunk_size*2))*(chunk_size*2)
camera.z=Math.floor(camera.position.z/(chunk_size*2))*(chunk_size*2)
for(x2=camera.x-fixed_render_distance;x2<camera.x+fixed_render_distance;x2+=chunk_size*2){
for(y2=camera.y-fixed_render_distance;y2<camera.y+fixed_render_distance;y2+=chunk_size*2){
for(z2=camera.z-fixed_render_distance;z2<camera.z+fixed_render_distance;z2+=chunk_size*2){
if(distance_to_object(camera.x,camera.y,camera.z,x2,y2,z2)<render_distance&&chunk["x"+x2+"y"+y2+"z"+z2]!=true){
gchunk(x2,y2,z2,chunk_size,"x"+x2+"y"+y2+"z"+z2)
chunk["x"+x2+"y"+y2+"z"+z2]=true
}
if(distance_to_object(camera.x,camera.y,camera.z,x2,y2,z2)>render_distance*3/2&&chunk["x"+x2+"y"+y2+"z"+z2]==true){
deChunk("x"+x2+"y"+y2+"z"+z2)
chunk["x"+x2+"y"+y2+"z"+z2]=false
}
}
}
}
}
noise3d=(x,y,z)=>{
return noise.simplex2(x,y)+noise.simplex2(y,z)+noise.simplex2(x,z)+noise.simplex2(y,x)+noise.simplex2(z,y)+noise.simplex2(z,x)
}
read=(obj)=>{
// Read.
return d[obj]
}
save=(obj,val)=>{
// Save.
d[obj]=val
}
</script>
So… I have some rendered minecraft terrain, but I need solid collision for make it playable, so… how can I do it? Thanks anyway.