Creating a 3D simulation of a car driving on a windy road in HTML5 involves using WebGL, which is a JavaScript API for rendering 3D graphics in the browser. To simplify the process, we can use a library like Three.js, which provides a higher-level interface for working with WebGL.
Below is an example of an HTML5 program that simulates an infinite loop of a car driving on a winding 3D road. The road will be procedurally generated to give the appearance of continuity, and the car will follow a predefined path.
Full Code Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Car Driving Simulation</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r146/three.min.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);
// Road geometry (procedural winding road)
const roadWidth = 2;
const roadLength = 50;
const roadGeometry = new THREE.BoxGeometry(roadWidth, 0.2, roadLength);
const roadMaterial = new THREE.MeshStandardMaterial({ color: 0x444444 });
const roadSegments = [];
let totalRoadLength = 0;
function createRoadSegment(position) {
const road = new THREE.Mesh(roadGeometry, roadMaterial);
road.position.copy(position);
road.rotation.y = position.angle || 0;
scene.add(road);
roadSegments.push(road);
totalRoadLength += roadLength;
}
// Initial road segments
createRoadSegment(new THREE.Vector3(0, 0, 0));
createRoadSegment(new THREE.Vector3(0, 0, -roadLength));
// Car model
const carGeometry = new THREE.BoxGeometry(1, 0.5, 2);
const carMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const car = new THREE.Mesh(carGeometry, carMaterial);
car.position.set(0, 0.5, 5);
scene.add(car);
// Camera setup
camera.position.set(0, 3, 10);
camera.lookAt(car.position);
// Animation variables
let carSpeed = 0.1;
let roadOffset = 0;
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Move the car forward
car.position.z -= carSpeed;
// Update road offset
roadOffset += carSpeed;
if (roadOffset >= roadLength) {
roadOffset -= roadLength;
const lastRoad = roadSegments.shift();
lastRoad.position.z -= roadLength * roadSegments.length;
const angle = Math.random() * 0.4 - 0.2; // Random slight turn
lastRoad.rotation.y = angle;
roadSegments.push(lastRoad);
}
// Adjust camera to follow the car
camera.position.lerp(new THREE.Vector3(car.position.x, 3, car.position.z + 10), 0.1);
camera.lookAt(car.position);
// Render the scene
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Start animation
animate();
</script>
</body>
</html>
Explanation of the Code
-
Scene Setup:
- A
THREE.Scene object is created to hold all 3D objects.
- A
THREE.PerspectiveCamera is used to define the view into the 3D world.
- A
THREE.WebGLRenderer renders the scene onto the HTML canvas.
-
Lighting:
- An ambient light and a directional light are added to illuminate the scene and make the car and road visible.
-
Road Geometry:
- The road is represented as a series of
THREE.Mesh objects with a box geometry.
- Each road segment is positioned end-to-end to create a continuous road.
- When the car moves past a road segment, it is reused at the end of the road to simulate an infinite loop.
-
Car Model:
- The car is a simple red box (
THREE.Mesh) placed slightly above the road.
-
Animation:
- The car moves forward along the z-axis at a constant speed.
- The road segments are dynamically shifted to simulate an endless winding road.
- The camera follows the car smoothly using
lerp interpolation.
-
Responsive Design:
- The program adjusts the camera aspect ratio and renderer size when the window is resized.
How It Works
- The car continuously moves forward along the z-axis.
- As the car progresses, the road segments are reused in a circular buffer to create the illusion of an infinite road.
- The road has slight random turns to simulate a "winding" effect.
- The camera smoothly follows the car, providing a dynamic third-person perspective.
Running the Code
- Copy the code into an
.html file (e.g., index.html).
- Open the file in a modern web browser.
- You should see a red car driving along a winding gray road in a 3D environment.
This implementation provides a basic framework for a 3D car driving simulation. You can enhance it further by adding textures, more complex car models, or physics-based movement.