Hier ist ein verbesserter Code mit allen gewünschten Funktionen - vertikale Bewegung, Maussteuerung, verbesserte HUD, Physik, Landschaft, Sprint und realistisches Camera-Bobbing:
<!DOCTYPE html>
<html>
<head>
<title>Realistische 3D-Welt</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: 'Orbitron', sans-serif;
}
#hud {
position: fixed;
bottom: 20px;
left: 20px;
width: 300px;
color: #00ffaa;
font-family: 'Orbitron', sans-serif;
padding: 15px;
background: rgba(0, 20, 40, 0.7);
border-radius: 10px;
border: 2px solid #00ffaa;
box-shadow: 0 0 15px rgba(0, 255, 170, 0.5);
backdrop-filter: blur(5px);
z-index: 100;
}
.stat-bar {
height: 12px;
margin: 8px 0;
border-radius: 6px;
background: rgba(0, 0, 0, 0.5);
overflow: hidden;
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
}
.stat-fill {
height: 100%;
width: 100%;
border-radius: 6px;
transition: width 0.3s;
}
.health-fill {
background: linear-gradient(90deg, #ff3030, #ff5050);
}
.stamina-fill {
background: linear-gradient(90deg, #30a0ff, #50c0ff);
}
.stat-label {
display: flex;
justify-content: space-between;
font-size: 14px;
text-shadow: 0 0 5px rgba(0, 255, 170, 0.7);
}
.compass {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
width: 200px;
height: 30px;
background: rgba(0, 20, 40, 0.7);
border-radius: 15px;
border: 2px solid #00ffaa;
box-shadow: 0 0 15px rgba(0, 255, 170, 0.5);
color: #00ffaa;
text-align: center;
line-height: 30px;
font-size: 14px;
text-shadow: 0 0 5px rgba(0, 255, 170, 0.7);
}
#crosshair {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 10px;
height: 10px;
border: 2px solid rgba(0, 255, 170, 0.7);
border-radius: 50%;
}
#crosshair::before, #crosshair::after {
content: '';
position: absolute;
background: rgba(0, 255, 170, 0.7);
}
#crosshair::before {
width: 1px;
height: 6px;
top: 2px;
left: 4.5px;
}
#crosshair::after {
width: 6px;
height: 1px;
top: 4.5px;
left: 2px;
}
</style>
</head>
<body>
<div id="hud">
<div class="stat-label">
<span>GESUNDHEIT</span>
<span id="healthValue">100</span>
</div>
<div class="stat-bar">
<div id="healthBar" class="stat-fill health-fill" style="width: 100%"></div>
</div>
<div class="stat-label">
<span>AUSDAUER</span>
<span id="staminaValue">100</span>
</div>
<div class="stat-bar">
<div id="staminaBar" class="stat-fill stamina-fill" style="width: 100%"></div>
</div>
<div class="stat-label">
<span>POSITION</span>
<span id="position">X: 0 Y: 0 Z: 0</span>
</div>
</div>
<div class="compass" id="compass">N</div>
<div id="crosshair"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cannon-es@0.20.0/dist/cannon-es.min.js"></script>
<script>
// Initialisierung
let camera, scene, renderer, world;
let playerBody, terrain;
let moveForward = false, moveBackward = false, moveLeft = false, moveRight = false;
let canJump = true, isSprinting = false;
let health = 100, stamina = 100;
let cameraBob = 0, cameraBobDirection = 1;
let cameraBobIntensity = 0.02;
let playerHeight = 1.8;
let walkSpeed = 5;
let sprintSpeed = 10;
let currentSpeed = walkSpeed;
let playerVelocity = new THREE.Vector3();
// Maussteuerung
let pitchObject, yawObject;
let moveVector = new THREE.Vector3();
// Physik-Welt initialisieren
world = new CANNON.World({
gravity: new CANNON.Vec3(0, -9.82, 0)
});
// Scene Setup
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
scene.fog = new THREE.FogExp2(0x87ceeb, 0.01);
// Kamera
camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
// Maussteuerung
pitchObject = new THREE.Object3D();
pitchObject.add(camera);
yawObject = new THREE.Object3D();
yawObject.position.y = playerHeight;
yawObject.add(pitchObject);
scene.add(yawObject);
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Licht
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 0.5).normalize();
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// Terrain erstellen
createTerrain();
// Physik für Spieler
const playerShape = new CANNON.Sphere(0.5);
playerBody = new CANNON.Body({
mass: 70,
position: new CANNON.Vec3(0, playerHeight + 1, 0),
shape: playerShape,
linearDamping: 0.9
});
world.addBody(playerBody);
// Event Listener
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
document.addEventListener('mousemove', onMouseMove, false);
document.addEventListener('click', () => {
document.body.requestPointerLock();
});
window.addEventListener('resize', onWindowResize, false);
// Pointer Lock API
const element = document.body;
element.requestPointerLock = element.requestPointerLock ||
element.mozRequestPointerLock ||
element.webkitRequestPointerLock;
}
function createTerrain() {
// Terrain-Geometrie
const terrainSize = 100;
const terrainSegments = 100;
const terrainGeometry = new THREE.PlaneGeometry(terrainSize, terrainSize, terrainSegments, terrainSegments);
// Höhenkarte generieren
const vertices = terrainGeometry.attributes.position.array;
for (let i = 0; i < vertices.length; i += 3) {
const x = vertices[i];
const z = vertices[i + 2];
// Verschiedene Noise-Funktionen für natürlicheres Terrain
vertices[i + 1] = Math.sin(x * 0.1) * Math.cos(z * 0.1) * 2 +
Math.sin(x * 0.25) * Math.cos(z * 0.25) * 1 +
Math.sin(x * 0.05) * Math.cos(z * 0.05) * 4;
}
terrainGeometry.computeVertexNormals();
// Terrain-Material
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0x3d8c40,
flatShading: false,
wireframe: false
});
terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.rotation.x = -Math.PI / 2;
terrain.receiveShadow = true;
scene.add(terrain);
// Physik für Terrain
const terrainShape = new CANNON.Plane();
const terrainBody = new CANNON.Body({
mass: 0,
shape: terrainShape
});
terrainBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
world.addBody(terrainBody);
// Bäume und Felsen hinzufügen
addEnvironmentObjects();
}
function addEnvironmentObjects() {
// Bäume
for (let i = 0; i < 50; i++) {
const x = Math.random() * 100 - 50;
const z = Math.random() * 100 - 50;
// Baumstamm
const trunkGeometry = new THREE.CylinderGeometry(0.2, 0.4, 2, 8);
const trunkMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 });
const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
trunk.position.set(x, 1, z);
trunk.castShadow = true;
scene.add(trunk);
// Baumkrone
const crownGeometry = new THREE.ConeGeometry(1, 2, 8);
const crownMaterial = new THREE.MeshStandardMaterial({ color: 0x2d4c21 });
const crown = new THREE.Mesh(crownGeometry, crownMaterial);
crown.position.set(x, 3, z);
crown.castShadow = true;
scene.add(crown);
// Physik für Baum
const treeShape = new CANNON.Cylinder(0.2, 0.4, 2, 8);
const treeBody = new CANNON.Body({
mass: 0,
position: new CANNON.Vec3(x, 1, z),
shape: treeShape
});
world.addBody(treeBody);
}
// Felsen
for (let i = 0; i < 30; i++) {
const x = Math.random() * 100 - 50;
const z = Math.random() * 100 - 50;
const size = Math.random() * 1 + 0.5;
const rockGeometry = new THREE.DodecahedronGeometry(size, 0);
const rockMaterial = new THREE.MeshStandardMaterial({ color: 0x888888 });
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(x, size/2, z);
rock.rotation.set(Math.random(), Math.random(), Math.random());
rock.castShadow = true;
scene.add(rock);
// Physik für Felsen
const rockShape = new CANNON.Sphere(size);
const rockBody = new CANNON.Body({
mass: 0,
position: new CANNON.Vec3(x, size/2, z),
shape: rockShape
});
world.addBody(rockBody);
}
}
function onMouseMove(event) {
if (document.pointerLockElement === document.body) {
const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
const movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
yawObject.rotation.y -= movementX * 0.002;
pitchObject.rotation.x -= movementY * 0.002;
// Begrenzen der vertikalen Kamerabewegung
pitchObject.rotation.x = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitchObject.rotation.x));
}
}
function onKeyDown(event) {
switch (event.code) {
case 'KeyW': moveForward = true; break;
case 'KeyS': moveBackward = true; break;
case 'KeyA': moveLeft = true; break;
case 'KeyD': moveRight = true; break;
case 'Space':
if (canJump) {
playerBody.velocity.y = 5;
canJump = false;
}
break;
case 'ShiftLeft':
if (stamina > 10) {
isSprinting = true;
currentSpeed = sprintSpeed;
cameraBobIntensity = 0.04;
}
break;
}
}
function onKeyUp(event) {
switch (event.code) {
case 'KeyW': moveForward = false; break;
case 'KeyS': moveBackward = false; break;
case 'KeyA': moveLeft = false; break;
case 'KeyD': moveRight = false; break;
case 'ShiftLeft':
isSprinting = false;
currentSpeed = walkSpeed;
cameraBobIntensity = 0.02;
break;
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function updatePlayer(delta) {
// Bewegungsrichtung berechnen
moveVector.set(0, 0, 0);
if (moveForward) moveVector.z = -1;
if (moveBackward) moveVector.z = 1;
if (moveLeft) moveVector.x = -1;
if (moveRight) moveVector.x = 1;
moveVector.normalize();
// Richtung an Kamerarotation anpassen
if (moveVector.z !== 0) {
playerVelocity.z = moveVector.z * currentSpeed;
} else {
playerVelocity.z = 0;
}
if (moveVector.x !== 0) {
playerVelocity.x = moveVector.x * currentSpeed;
} else {
playerVelocity.x = 0;
}
// Rotation anwenden
const rotationMatrix = new THREE.Matrix4();
rotationMatrix.makeRotationY(yawObject.rotation.y);
playerVelocity.applyMatrix4(rotationMatrix);
// Physik anwenden
playerBody.velocity.x = playerVelocity.x;
playerBody.velocity.z = playerVelocity.z;
// Position aktualisieren
yawObject.position.copy(playerBody.position);
// Camera Bob
if ((moveForward || moveBackward || moveLeft || moveRight) && canJump) {
cameraBob += delta * 10 * (isSprinting ? 1.5 : 1);
const bobOffset = Math.sin(cameraBob) * cameraBobIntensity;
camera.position.y = bobOffset;
}
// Ausdauer verwalten
if (isSprinting && (moveForward || moveBackward || moveLeft || moveRight)) {
stamina = Math.max(0, stamina - delta * 15);
if (stamina <= 0) {
isSprinting = false;
currentSpeed = walkSpeed;
cameraBobIntensity = 0.02;
}
} else if (!isSprinting) {
stamina = Math.min(100, stamina + delta * 5);
}
// HUD aktualisieren
document.getElementById('healthBar').style.width = health + '%';
document.getElementById('healthValue').textContent = Math.round(health);
document.getElementById('staminaBar').style.width = stamina + '%';
document.getElementById('staminaValue').textContent = Math.round(stamina);
document.getElementById('position').textContent =
`X: ${Math.round(yawObject.position.x)} Y: ${Math.round(yawObject.position.y)} Z: ${Math.round(yawObject.position.z)}`;
// Kompass aktualisieren
const angle = (yawObject.rotation.y * (180/Math.PI) + 180) % 360;
let direction = "N";
if (angle > 22.5 && angle <= 67.5) direction = "NO";
else if (angle > 67.5 && angle <= 112.5) direction = "O";
else if (angle > 112.5 && angle <= 157.5) direction = "SO";
else if (angle > 157.5 && angle <= 202.5) direction = "S";
else if (angle > 202.5 && angle <= 247.5) direction = "SW";
else if (angle > 247.5 && angle <= 292.5) direction = "W";
else if (angle > 292.5 && angle <= 337.5) direction = "NW";
document.getElementById('compass').textContent = direction + " | " + Math.round(angle) + "°";
// Sprung-Status prüfen
if (Math.abs(playerBody.velocity.y) < 0.1) {
canJump = true;
}
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(0.1, clock.getDelta());
// Physik aktualisieren
world.step(delta);
// Spieler aktualisieren
updatePlayer(delta);
renderer.render(scene, camera);
}
const clock = new THREE.Clock();
init();
animate();
</script>
</body>
</html>