Certainly! Creating a full-featured 3D car game from scratch is a big project, but I can provide you with a simple 3D car driving demo using Three.js, a popular JavaScript 3D library. This example will let you control a basic car model with the arrow keys.
How it works:
Uses Three.js for 3D rendering.
Simple car (a box) you can drive around a plane.
Basic keyboard controls (arrow keys).
To run this code:
Copy and paste it into an .html file.
Open it in your browser.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple 3D Car Game</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Three.js CDN -->
<script src="https://cdn.jsdelivr.net/npm/three@0.153.0/build/three.min.js"></script>
<script>
// Scene, Camera, Renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xa0a0a0);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(0, 10, 20);
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Ground
const groundGeometry = new THREE.PlaneGeometry(100, 100);
const groundMaterial = new THREE.MeshPhongMaterial({color: 0x228822});
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// Car (simple box)
const car = new THREE.Group();
const bodyGeometry = new THREE.BoxGeometry(2, 0.5, 4);
const bodyMaterial = new THREE.MeshPhongMaterial({color: 0xff0000});
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.position.y = 0.5;
car.add(body);
// Wheels
const wheelGeometry = new THREE.CylinderGeometry(0.4, 0.4, 0.5, 16);
const wheelMaterial = new THREE.MeshPhongMaterial({color: 0x222222});
for (let x of [-0.9, 0.9]) {
for (let z of [-1.5, 1.5]) {
const wheel = new THREE.Mesh(wheelGeometry, wheelMaterial);
wheel.rotation.z = Math.PI / 2;
wheel.position.set(x, 0.2, z);
car.add(wheel);
}
}
scene.add(car);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(10, 20, 10);
scene.add(dirLight);
// Controls
let speed = 0;
let angle = 0;
const maxSpeed = 0.3;
const acceleration = 0.01;
const turnSpeed = 0.03;
const friction = 0.98;
const keys = {};
window.addEventListener('keydown', e => keys[e.key] = true);
window.addEventListener('keyup', e => keys[e.key] = false);
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Controls
if (keys['ArrowUp']) speed = Math.min(maxSpeed, speed + acceleration);
if (keys['ArrowDown']) speed = Math.max(-maxSpeed, speed - acceleration);
if (keys['ArrowLeft']) angle += turnSpeed * (speed !== 0 ? Math.sign(speed) : 0);
if (keys['ArrowRight']) angle -= turnSpeed * (speed !== 0 ? Math.sign(speed) : 0);
// Move car
car.rotation.y = angle;
car.position.x += Math.sin(angle) * speed;
car.position.z += Math.cos(angle) * speed;
// Friction
speed *= friction;
// Camera follows car
camera.position.x = car.position.x - Math.sin(angle) * 10;
camera.position.z = car.position.z - Math.cos(angle) * 10;
camera.position.y = 6;
camera.lookAt(car.position);
renderer.render(scene, camera);
}
animate();
// Responsive
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
Features:
Arrow keys to drive and steer.
Simple 3D car and ground.
Camera follows the car.
You can expand this by adding obstacles, better models, or more features! If you want a more advanced example or have specific features in mind, let me know!