🎮ArcadeLab

Impossible Obby 3D

by BoldPenguin14
355 lines9.8 KB🛠️ Three.js (3D graphics)
▶ Play

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Impossible Obby 3D</title>
<style>
*{box-sizing:border-box}
body{margin:0;overflow:hidden;font-family:Arial;background:#75caff;touch-action:none}
canvas{display:block;touch-action:none}
#ui{position:fixed;inset:0;color:white;pointer-events:none;text-shadow:2px 2px 3px #222}
#top{display:flex;justify-content:space-between;padding:12px;font-weight:bold}
.panel{background:#202bba;padding:11px 14px;border:2px solid white;border-radius:12px}
#cameraHint{position:absolute;top:65px;width:100%;text-align:center;font-size:13px}
#controls{position:absolute;bottom:22px;width:100%;display:flex;justify-content:space-between;align-items:end;padding:0 22px;pointer-events:auto}
#joystick{width:125px;height:125px;border-radius:50%;background:#ffffff55;border:3px solid white;position:relative;touch-action:none}
#knob{width:48px;height:48px;border-radius:50%;background:white;position:absolute;left:35px;top:35px}
#jump{width:85px;height:85px;border-radius:50%;border:3px solid white;background:#ff8b19;color:white;font-weight:bold;font-size:19px;touch-action:none}
#menu{position:fixed;inset:0;background:#0009;display:flex;align-items:center;justify-content:center;z-index:5;color:white;text-align:center}
#menuBox{background:#263253;padding:28px;border-radius:20px;border:3px solid white;width:85%;max-width:380px}
#menu button{background:#ff8c19;color:white;border:0;border-radius:12px;padding:15px 30px;font-size:22px;font-weight:bold}
#menu h1{font-size:34px}
</style>
</head>
<body>

<div id="ui">
 <div id="top">
  <div class="panel">STAGE: <span id="stage">1</span>/30</div>
  <div class="panel">FALLS: <span id="falls">0</span></div>
 </div>
 <div id="cameraHint">DRAG SCREEN TO MOVE CAMERA</div>
 <div id="controls">
  <div id="joystick"><div id="knob"></div></div>
  <button id="jump">JUMP</button>
 </div>
</div>

<div id="menu">
 <div id="menuBox">
  <h1>IMPOSSIBLE OBBY</h1>
  <p>30 stages. Can you survive?</p>
  <p>Move with joystick. Press JUMP!</p>
  <p>Drag screen to move camera!</p>
  <button id="start">PLAY NOW</button>
 </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
const scene=new THREE.Scene();
scene.background=new THREE.Color(0x75caff);
scene.fog=new THREE.Fog(0x75caff,45,130);

const camera=new THREE.PerspectiveCamera(70,innerWidth/innerHeight,.1,300);
const renderer=new THREE.WebGLRenderer({antialias:true});
renderer.setSize(innerWidth,innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio,1.5));
renderer.shadowMap.enabled=true;
document.body.appendChild(renderer.domElement);

scene.add(new THREE.HemisphereLight(0xffffff,0x778899,2));
const sun=new THREE.DirectionalLight(0xffffff,2);
sun.position.set(10,25,10);
scene.add(sun);

const mat=c=>new THREE.MeshStandardMaterial({color:c});
const colors=[0x278cff,0xff9d24,0x9b55ff,0x20d879];
const green=mat(0x20d879),red=mat(0xff2222);
const skin=mat(0xffc18c),shirt=mat(0x1675ff);
const pants=mat(0x242c55),black=mat(0x111111);

function block(x,y,z,sx,sy,sz,m){
 const b=new THREE.Mesh(new THREE.BoxGeometry(sx,sy,sz),m);
 b.position.set(x,y,z);
 b.castShadow=b.receiveShadow=true;
 scene.add(b);
 return b;
}

// PLAYER
const player=new THREE.Group();
[
 [0,.95,0,.8,.9,.4,shirt],
 [0,1.65,0,.65,.65,.65,skin],
 [-.55,.95,0,.25,.85,.3,skin],
 [.55,.95,0,.25,.85,.3,skin],
 [-.22,.25,0,.3,.6,.35,pants],
 [.22,.25,0,.3,.6,.35,pants],
 [-.15,1.72,-.33,.07,.09,.035,black],
 [.15,1.72,-.33,.07,.09,.035,black]
].forEach(a=>player.add(block(...a)));
scene.add(player);

const platforms=[],hazards=[],checkpoints=[];
const startPlatform=block(0,-.35,0,10,.7,10,green);
platforms.push(startPlatform);

for(let i=1;i<=30;i++){
 const x=Math.sin(i*1.5)*3,z=-i*5,y=Math.min(i*.12,3);
 platforms.push(block(x,y,z,3.6,.5,3.6,mat(colors[i%4])));

 if(i%3===0)
  hazards.push(block(x,y-.5,z-2.1,3,.3,1.5,red));

 if(i%4===0){
  const m=block(x+2,y+.1,z-1,1.4,.4,1.4,mat(0xff9d24));
  m.userData.moving=true;
  m.userData.startX=x+2;
  hazards.push(m);
 }

 if(i%5===0){
  const cp=block(x,y+.8,z,.5,1.6,.5,mat(0xffff00));
  checkpoints.push({block:cp,x,y,z,stage:i});
 }
}
platforms.push(block(0,3,-158,10,1,10,mat(0xffd52a)));

// GAME STATE
let stage=1,falls=0,playing=false;
let velocityY=0,grounded=false,jump=false;
let checkpoint={x:0,y:0,z:0};
const keys={};

// CAMERA
let yaw=Math.atan2(7,10),pitch=.45;
const distance=13;
const target=new THREE.Vector3();

function updateCamera(){
 target.set(player.position.x,player.position.y+1,player.position.z);
 const h=Math.cos(pitch)*distance;
 camera.position.lerp(new THREE.Vector3(
  target.x+Math.sin(yaw)*h,
  target.y+Math.sin(pitch)*distance,
  target.z+Math.cos(yaw)*h
 ),.18);
 camera.lookAt(target);
}

let dragging=false,lastX=0,lastY=0,pointer=null;
renderer.domElement.addEventListener("pointerdown",e=>{
 if(!playing)return;
 dragging=true;pointer=e.pointerId;
 lastX=e.clientX;lastY=e.clientY;
 renderer.domElement.setPointerCapture(e.pointerId);
});
renderer.domElement.addEventListener("pointermove",e=>{
 if(!dragging||e.pointerId!==pointer)return;
 yaw-=(e.clientX-lastX)*.006;
 pitch=Math.max(.08,Math.min(1.4,pitch-(e.clientY-lastY)*.006));
 lastX=e.clientX;lastY=e.clientY;
});
function stopDrag(e){
 if(e.pointerId===pointer){dragging=false;pointer=null;}
}
renderer.domElement.addEventListener("pointerup",stopDrag);
renderer.domElement.addEventListener("pointercancel",stopDrag);

// KEYBOARD
addEventListener("keydown",e=>{
 keys[e.key.toLowerCase()]=true;
 if(e.code==="Space")jump=true;
});
addEventListener("keyup",e=>keys[e.key.toLowerCase()]=false);

// JOYSTICK
const joystick=document.getElementById("joystick");
const knob=document.getElementById("knob");
let joyX=0,joyY=0,joyId=null;

function moveJoy(e){
 const r=joystick.getBoundingClientRect();
 let x=e.clientX-r.left-r.width/2;
 let y=e.clientY-r.top-r.height/2;
 const len=Math.hypot(x,y);
 if(len>38){x=x/len*38;y=y/len*38;}
 joyX=x/38;joyY=y/38;
 knob.style.transform=`translate(${x}px,${y}px)`;
}
joystick.addEventListener("pointerdown",e=>{
 joyId=e.pointerId;
 joystick.setPointerCapture(joyId);
 moveJoy(e);
});
joystick.addEventListener("pointermove",e=>{
 if(e.pointerId===joyId)moveJoy(e);
});
function resetJoy(){
 joyX=joyY=0;joyId=null;
 knob.style.transform="translate(0,0)";
}
joystick.addEventListener("pointerup",resetJoy);
joystick.addEventListener("pointercancel",resetJoy);
document.getElementById("jump").addEventListener("pointerdown",e=>{
 e.preventDefault();jump=true;
});

// RESPAWN
function respawn(){
 player.position.set(checkpoint.x,checkpoint.y,checkpoint.z);
 velocityY=0;grounded=true;
 falls++;
 document.getElementById("falls").textContent=falls;
}

// WIN
function win(){
 playing=false;
 document.getElementById("menuBox").innerHTML=`
 <h1>YOU WON!</h1>
 <p>YOU BEAT ALL 30 STAGES!</p>
 <button onclick="location.reload()">PLAY AGAIN</button>`;
 document.getElementById("menu").style.display="flex";
}

// UPDATE
function update(){
 if(!playing)return;

 let dx=joyX,dz=joyY;
 if(keys.a||keys.arrowleft)dx-=1;
 if(keys.d||keys.arrowright)dx+=1;
 if(keys.w||keys.arrowup)dz-=1;
 if(keys.s||keys.arrowdown)dz+=1;

 const len=Math.hypot(dx,dz);
 if(len>1){dx/=len;dz/=len;}

 player.position.x+=dx*.14;
 player.position.z+=dz*.14;

 if(jump&&grounded){velocityY=.28;grounded=false;}
 jump=false;

 const previousY=player.position.y;
 velocityY-=.012;
 player.position.y+=velocityY;
 grounded=false;

 // PLATFORM COLLISION
 for(const p of platforms){
  const dxp=Math.abs(player.position.x-p.position.x);
  const dzp=Math.abs(player.position.z-p.position.z);
  const top=p.position.y+p.geometry.parameters.height/2;

  if(
   dxp<p.geometry.parameters.width/2+.25 &&
   dzp<p.geometry.parameters.depth/2+.25 &&
   velocityY<=0 &&
   previousY>=top-.05 &&
   player.position.y<=top+.05
  ){
   player.position.y=top;
   velocityY=0;
   grounded=true;
  }
 }

 // CHECKPOINTS
 for(const cp of checkpoints){
  if(
   Math.abs(player.position.x-cp.x)<2 &&
   Math.abs(player.position.z-cp.z)<2 &&
   player.position.y>cp.y
  ){
   if(cp.stage>stage-1){
    checkpoint={x:cp.x,y:cp.y,z:cp.z};
    stage=cp.stage;
    document.getElementById("stage").textContent=stage;
   }
  }
 }

 // STAGE PROGRESS
 const reached=Math.min(30,Math.floor(-player.position.z/5));
 if(reached>stage){
  stage=reached;
  document.getElementById("stage").textContent=stage;
 }

 // MOVING OBSTACLES
 for(const h of hazards){
  if(h.userData.moving){
   h.position.x=h.userData.startX+Math.sin(Date.now()*.002)*2;
  }
 }

 // FIXED LAVA COLLISION
 for(const h of hazards){
  if(h.userData.moving)continue;

  const px=Math.abs(player.position.x-h.position.x);
  const pz=Math.abs(player.position.z-h.position.z);
  const py=Math.abs(player.position.y-h.position.y);

  if(
   px<h.geometry.parameters.width/2+.4 &&
   pz<h.geometry.parameters.depth/2+.3 &&
   py<1.2
  ){
   respawn();
   break;
  }
 }

 // FALL PROTECTION
 if(player.position.y < -6 || Math.abs(player.position.x)>15)
  respawn();

 // FINISH
 if(player.position.z < -153 && player.position.y>1)
  win();

 if(Math.abs(dx)+Math.abs(dz)>.1)
  player.rotation.y=Math.atan2(dx,-dz);

 updateCamera();
}

// ANIMATION
function animate(){
 requestAnimationFrame(animate);
 hazards.forEach(h=>{
  if(h.userData.moving)h.rotation.y+=.015;
 });
 update();
 renderer.render(scene,camera);
}
animate();

// START
document.getElementById("start").onclick=()=>{
 player.position.set(0,0,0);
 velocityY=0;grounded=true;
 yaw=Math.atan2(7,10);pitch=.45;
 camera.position.set(7,7,10);
 camera.lookAt(0,1,0);
 playing=true;
 updateCamera();
 document.getElementById("menu").style.display="none";
};

// RESIZE
addEventListener("resize",()=>{
 camera.aspect=innerWidth/innerHeight;
 camera.updateProjectionMatrix();
 renderer.setSize(innerWidth,innerHeight);
});
</script>
</body>
</html>

Game Source: Impossible Obby 3D

Creator: BoldPenguin14

Libraries: three

Complexity: complex (355 lines, 9.8 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: impossible-obby-3d-boldpenguin14" to link back to the original. Then publish at arcadelab.ai/publish.