Code a 3d game engine with possibility of walking using vanilla html css js with no…
- User
Code a 3d game engine with possibility of walking using vanilla html css js with no frameworks or libraries. And without canvas element
- Assistant
Code a 3d game engine with possibility of walking using vanilla html css js with no frameworks or libraries. And without canvas element
Certainly! Creating a simple 3D game engine using only vanilla HTML, CSS, and JavaScript—without any frameworks, libraries, or the canvas element—is quite challenging, but it's possible by leveraging CSS 3D transforms. In this example, we'll build a basic 3D scene that allows the user to "walk" around using keyboard inputs.
Overview:
We'll create a simple scene with walls and a floor to simulate a corridor or room.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Game Engine with HTML, CSS, and JS</title>
<style>
/* CSS will be added here */
</style>
</head>
<body>
<div id="scene">
<div class="wall wall-front"></div>
<div class="wall wall-back"></div>
<div class="wall wall-left"></div>
<div class="wall wall-right"></div>
<div class="floor"></div>
</div>
<script>
// JavaScript will be added here
</script>
</body>
</html>
We use CSS to set up the 3D environment, style the walls and floor, and apply 3D transforms.
body {
margin: 0;
overflow: hidden;
perspective: 800px; /* Set the perspective to create depth */
background-color: #000;
}
#scene {
position: relative;
width: 100vw;
height: 100vh;
transform-style: preserve-3d; /* Ensure children are transformed in 3D space */
transform: translateZ(0); /* Initialize the transform */
}
.wall, .floor {
position: absolute;
width: 1000px;
height: 1000px;
background: rgba(200, 200, 200, 0.8);
box-sizing: border-box;
border: 1px solid #333;
}
.wall {
height: 500px;
}
.wall-front {
transform: translateZ(-500px);
}
.wall-back {
transform: translateZ(500px) rotateY(180deg);
}
.wall-left {
transform: translateX(-500px) rotateY(90deg);
}
.wall-right {
transform: translateX(500px) rotateY(-90deg);
}
.floor {
background: rgba(150, 150, 150, 0.8);
transform: rotateX(90deg) translateY(500px);
}
Explanation:
margin: 0 and overflow: hidden to ensure the scene fills the viewport without scrollbars. The perspective property gives the effect of depth.transform-style: preserve-3d property allows child elements to be positioned in 3D space.transform. Each wall is rotated and translated to form a closed room.We handle user input to move the player (camera) within the scene by applying transforms.
let player = {
x: 0,
z: 0,
rotY: 0
};
function updateScene() {
let scene = document.getElementById('scene');
// Apply the inverse transformations to simulate camera movement
scene.style.transform = `
translateZ(${ -player.z }px)
translateX(${ -player.x }px)
rotateY(${ -player.rotY }deg)
`;
}
document.addEventListener('keydown', function(event) {
const key = event.key.toLowerCase();
const speed = 10;
const rotSpeed = 5;
if (key === 'arrowup' || key === 'w') {
// Move forward
player.z += speed * Math.cos(player.rotY * Math.PI / 180);
player.x += speed * Math.sin(player.rotY * Math.PI / 180);
} else if (key === 'arrowdown' || key === 's') {
// Move backward
player.z -= speed * Math.cos(player.rotY * Math.PI / 180);
player.x -= speed * Math.sin(player.rotY * Math.PI / 180);
} else if (key === 'arrowleft' || key === 'a') {
// Rotate left
player.rotY -= rotSpeed;
} else if (key === 'arrowright' || key === 'd') {
// Rotate right
player.rotY += rotSpeed;
}
updateScene();
});
Explanation:
x, z) and rotation around the Y-axis (rotY).Here's the complete code with HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Game Engine with HTML, CSS, and JS</title>
<style>
body {
margin: 0;
overflow: hidden;
perspective: 800px;
background-color: #000;
}
#scene {
position: relative;
width: 100vw;
height: 100vh;
transform-style: preserve-3d;
transform: translateZ(0);
}
.wall, .floor {
position: absolute;
width: 1000px;
height: 1000px;
background: rgba(200, 200, 200, 0.8);
box-sizing: border-box;
border: 1px solid #333;
}
.wall {
height: 500px;
}
.wall-front {
transform: translateZ(-500px);
}
.wall-back {
transform: translateZ(500px) rotateY(180deg);
}
.wall-left {
transform: translateX(-500px) rotateY(90deg);
}
.wall-right {
transform: translateX(500px) rotateY(-90deg);
}
.floor {
background: rgba(150, 150, 150, 0.8);
transform: rotateX(90deg) translateY(500px);
}
</style>
</head>
<body>
<div id="scene">
<div class="wall wall-front"></div>
<div class="wall wall-back"></div>
<div class="wall wall-left"></div>
<div class="wall wall-right"></div>
<div class="floor"></div>
</div>
<script>
let player = {
x: 0,
z: 0,
rotY: 0
};
function updateScene() {
let scene = document.getElementById('scene');
scene.style.transform = `
translateZ(${ -player.z }px)
translateX(${ -player.x }px)
rotateY(${ -player.rotY }deg)
`;
}
document.addEventListener('keydown', function(event) {
const key = event.key.toLowerCase();
const speed = 10;
const rotSpeed = 5;
if (key === 'arrowup' || key === 'w') {
// Move forward
player.z += speed * Math.cos(player.rotY * Math.PI / 180);
player.x += speed * Math.sin(player.rotY * Math.PI / 180);
} else if (key === 'arrowdown' || key === 's') {
// Move backward
player.z -= speed * Math.cos(player.rotY * Math.PI / 180);
player.x -= speed * Math.sin(player.rotY * Math.PI / 180);
} else if (key === 'arrowleft' || key === 'a') {
// Rotate left
player.rotY -= rotSpeed;
} else if (key === 'arrowright' || key === 'd') {
// Rotate right
player.rotY += rotSpeed;
}
updateScene();
});
</script>
</body>
</html>
perspective property on the body element creates the depth necessary for 3D perception.rotateY) simulates the player turning left or right..html extension.player object to include a y property and adjust the updateScene function to enable vertical movement.By leveraging CSS 3D transforms and basic JavaScript, we've created a simple 3D environment that allows basic navigation without using the canvas element or any external libraries. While this is a rudimentary example, it demonstrates the fundamental concepts behind 3D rendering in web browsers using only native technologies.
Not yet included in a published release.