HTML
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>4x4 Dağ Off-Road — Gelişmiş Fizik</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; }
body {
background: #090d16;
color: #f8fafc;
font-family: system-ui, -apple-system, sans-serif;
overflow: hidden;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
#hud {
position: fixed;
top: 16px;
left: 16px;
display: flex;
gap: 10px;
flex-wrap: wrap;
pointer-events: none;
}
.panel {
background: rgba(15, 23, 42, 0.82);
border: 1px solid rgba(255, 255, 255, 0.14);
backdrop-filter: blur(8px);
padding: 9px 13px;
border-radius: 10px;
font-size: 12px;
min-width: 108px;
}
.label { color: #94a3b8; font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; }
.val { font-size: 17px; font-weight: 700; color: #38bdf8; margin-top: 2px; }
.val.warn { color: #f59e0b; }
.val.danger { color: #ef4444; }
#controls {
position: fixed;
bottom: 16px;
left: 16px;
background: rgba(15, 23, 42, 0.82);
border: 1px solid rgba(255, 255, 255, 0.14);
backdrop-filter: blur(8px);
padding: 10px 14px;
border-radius: 10px;
font-size: 12px;
color: #cbd5e1;
pointer-events: none;
line-height: 1.6;
}
kbd {
background: #1e293b;
border: 1px solid #475569;
border-radius: 4px;
padding: 1px 6px;
font-family: monospace;
color: #f8fafc;
}
#banner {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(15, 23, 42, 0.94);
border: 1px solid #38bdf8;
padding: 22px 30px;
border-radius: 14px;
text-align: center;
display: none;
}
#banner h2 { color: #38bdf8; margin-bottom: 8px; }
#banner p { color: #cbd5e1; font-size: 14px; }
</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="hud">
<div class="panel">
<div class="label">Mesafe</div>
<div class="val" id="distVal">0 m</div>
</div>
<div class="panel">
<div class="label">Yükseklik</div>
<div class="val" id="altVal">0 m</div>
</div>
<div class="panel">
<div class="label">Hız</div>
<div class="val" id="spdVal">0 km/s</div>
</div>
<div class="panel">
<div class="label">Aktarma / Diff</div>
<div class="val" id="diffVal">4L + DIFF</div>
</div>
<div class="panel">
<div class="label">Zemin / Tutunma</div>
<div class="val" id="surfVal">KAYA %95</div>
</div>
</div>
<div id="controls">
<kbd>W</kbd>/<kbd>↑</kbd> Gaz
<kbd>S</kbd>/<kbd>↓</kbd> Fren / Geri
<kbd>A</kbd>/<kbd>D</kbd> veya <kbd>←</kbd>/<kbd>→</kbd> Eğim Dengesi
<kbd>L</kbd> Diff Kilidi Aç/Kapat
<kbd>R</kbd> Kurtar / Sıfırla
</div>
<div id="banner">
<h2>Zirveye Ulaştın!</h2>
<p>Dağ parkurunu tamamladın. <kbd>R</kbd> ile tekrar deneyebilir veya zirvede gezebilirsin.</p>
</div>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener("resize", resize);
resize();
// Sabit zaman adımlı fizik ayarları
const FIXED_DT = 1 / 60;
const SUBSTEPS = 4;
const SUB_DT = FIXED_DT / SUBSTEPS;
const keys = {};
window.addEventListener("keydown", (e) => {
if (!keys[e.code] && e.code === "KeyL") {
car.diffLocked = !car.diffLocked;
}
keys[e.code] = true;
if (e.code === "KeyR") resetCar();
});
window.addEventListener("keyup", (e) => {
keys[e.code] = false;
});
const TERRAIN_STEP = 14;
const MAX_X = 9600;
// Zemin tipi: 'grass', 'rock', 'mud'
function getSurfaceType(x) {
if (x < 650) return "grass";
const mudZones = [
[1450, 2150],
[4100, 4950],
[6800, 7550]
];
for (const [a, b] of mudZones) {
if (x >= a && x <= b) return "mud";
}
const slope = Math.abs(getSlopeAngle(x));
if (slope > 0.24 || x > 5000) return "rock";
return "grass";
}
function getTerrainY(x) {
if (x < 0) return 520;
const startFlat = Math.min(1, Math.max(0, (x - 260) / 360));
const climb = -Math.pow(Math.min(x, MAX_X) / MAX_X, 0.88) * 1520;
const ridge1 = Math.sin(x * 0.0022) * 120;
const ridge2 = Math.cos(x * 0.0057 + 0.8) * 62;
const bumps = Math.sin(x * 0.018) * 19 + Math.cos(x * 0.041) * 9;
const ramp1 = Math.exp(-Math.pow((x - 1800) / 360, 2)) * -180;
const dip1 = Math.exp(-Math.pow((x - 3200) / 260, 2)) * 125;
const mudRise = Math.exp(-Math.pow((x - 4500) / 380, 2)) * -150;
const ramp2 = Math.exp(-Math.pow((x - 5600) / 420, 2)) * -235;
const ridge3 = Math.exp(-Math.pow((x - 7400) / 310, 2)) * -175;
const summitFactor = Math.min(1, Math.max(0, (x - 8700) / 450));
const rawY = 520 + (climb + ridge1 + ridge2 + bumps + ramp1 + dip1 + mudRise + ramp2 + ridge3) * startFlat;
const plateauY = 520 - 1480;
return rawY * (1 - summitFactor) + plateauY * summitFactor;
}
function getTerrainNormal(x) {
const dx = 5;
const y1 = getTerrainY(x - dx);
const y2 = getTerrainY(x + dx);
const tx = dx * 2;
const ty = y2 - y1;
// Yukarı bakan birim normal vektörü
let nx = ty;
let ny = -tx;
const len = Math.hypot(nx, ny) || 1;
nx /= len;
ny /= len;
if (ny > 0) { nx = -nx; ny = -ny; }
return { nx, ny, tx: tx / len, ty: ty / len, angle: Math.atan2(ty, tx) };
}
function getSlopeAngle(x) {
return getTerrainNormal(x).angle;
}
// Rijit gövde off-road aracı
const car = {
x: 160,
y: getTerrainY(160) - 62,
vx: 0,
vy: 0,
angle: 0,
omega: 0,
mass: 1650, // kg
inertia: 42000, // kg*m^2
wheelRadius: 22,
halfWheelbase: 45,
attachOffsetY: 22,
wheelSpin: 0,
diffLocked: true,
rolloverTimer: 0,
wheels: {
rear: { compression: 0, prevCompression: 0, grounded: false, slip: 0, grip: 1 },
front: { compression: 0, prevCompression: 0, grounded: false, slip: 0, grip: 1 }
}
};
let camX = car.x - 240;
let camY = car.y - 340;
let accumulator = 0;
let lastTime = performance.now();
let reachedSummit = false;
const particles = [];
function resetCar() {
car.x = 160;
car.y = getTerrainY(160) - 62;
car.vx = 0;
car.vy = 0;
car.angle = 0;
car.omega = 0;
car.wheelSpin = 0;
car.rolloverTimer = 0;
car.wheels.rear.compression = 0;
car.wheels.rear.prevCompression = 0;
car.wheels.front.compression = 0;
car.wheels.front.prevCompression = 0;
reachedSummit = false;
document.getElementById("banner").style.display = "none";
}
function spawnParticle(x, y, count, opts = {}) {
for (let i = 0; i < count; i++) {
particles.push({
x,
y,
vx: (Math.random() - 0.5) * (opts.spread || 3.5) - car.vx * 0.14,
vy: -Math.random() * (opts.upSpeed || 2.5) - 0.4,
size: (opts.minSize || 3) + Math.random() * (opts.maxSize || 5),
alpha: opts.alpha || 0.78,
drag: opts.drag || 0.06,
color: opts.color || "rgba(180, 145, 105, 0.7)"
});
}
}
// Yerel gövde noktasını dünya koordinatına çevir
function localToWorld(lx, ly) {
const c = Math.cos(car.angle);
const s = Math.sin(car.angle);
return {
x: car.x + lx * c - ly * s,
y: car.y + lx * s + ly * c
};
}
// Noktanın rijit gövde hızı: v_p = v_cm + omega x r
function pointVelocity(rx, ry) {
return {
vx: car.vx - car.omega * ry,
vy: car.vy + car.omega * rx
};
}
function stepPhysicsSubstep(dt) {
const gravity = 980; // px/s^2 ölçekli yerçekimi
const throttle = (keys["KeyW"] || keys["ArrowUp"]) ? 1 : 0;
const brakeOrRev = (keys["KeyS"] || keys["ArrowDown"]) ? 1 : 0;
const tiltLeft = (keys["KeyA"] || keys["ArrowLeft"]) ? 1 : 0;
const tiltRight = (keys["KeyD"] || keys["ArrowRight"]) ? 1 : 0;
let fx = 0;
let fy = car.mass * gravity; // aşağı doğru +y
let torque = 0;
// Aerodinamik sürüklenme
const speed = Math.hypot(car.vx, car.vy);
fx += -0.45 * car.vx * speed;
fy += -0.45 * car.vy * speed;
const springK = 135000;
const damperC = 7800;
const maxTravel = 18;
const wheelSpecs = [
{ key: "rear", lx: -car.halfWheelbase },
{ key: "front", lx: car.halfWheelbase }
];
let activeWheels = 0;
let avgGrip = 0;
for (const spec of wheelSpecs) {
const w = car.wheels[spec.key];
w.prevCompression = w.compression;
// Bağlantı noktası (süspansiyon üstü)
const mount = localToWorld(spec.lx, car.attachOffsetY);
const groundY = getTerrainY(mount.x);
const surf = getSurfaceType(mount.x);
const norm = getTerrainNormal(mount.x);
// Bağlantı noktasından zemine tekerlek yarıçapı kadar mesafe
const clearance = (groundY - mount.y) - car.wheelRadius;
const rawComp = Math.max(0, Math.min(maxTravel, -clearance));
w.compression = rawComp;
w.grounded = clearance <= 1.5;
// Zemin yüzeyine göre sürtünme katsayısı
let baseGrip = 1.05;
let rollingResist = 0.035;
if (surf === "mud") {
baseGrip = car.diffLocked ? 0.72 : 0.48;
rollingResist = 0.16;
} else if (surf === "rock") {
baseGrip = car.diffLocked ? 1.15 : 0.92;
rollingResist = 0.055;
}
w.grip = baseGrip;
if (w.grounded) {
activeWheels++;
avgGrip += baseGrip;
// Temas noktasının gövde merkezine göre vektörü (r)
const contactPt = {
x: mount.x + norm.nx * (car.wheelRadius * 0.5),
y: groundY
};
const rx = contactPt.x - car.x;
const ry = contactPt.y - car.y;
const vp = pointVelocity(rx, ry);
// Normal doğrultudaki hız (pozitifse zemine giriyor)
const vNormal = -(vp.vx * norm.nx + vp.vy * norm.ny);
const compRate = (w.compression - w.prevCompression) / dt;
// Yay + sönümleme normal kuvveti
const normalForce = Math.max(0, w.compression * springK + Math.max(vNormal, compRate) * damperC);
// Normal kuvveti gövdeye uygula
const fnX = norm.nx * normalForce;
const fnY = norm.ny * normalForce;
fx += fnX;
fy += fnY;
torque += rx * fnY - ry * fnX;
// Teğetsel hız (yokuş yukarı yön: tx, ty)
const vTangent = vp.vx * norm.tx + vp.vy * norm.ty;
// 4x4 motor torku ve frenleme kuvveti
let driveForce = 0;
const wheelShare = car.diffLocked ? 0.55 : (spec.key === "rear" ? 0.65 : 0.35);
const maxDrive = car.mass * 14.5 * wheelShare * baseGrip;
if (throttle) {
driveForce = maxDrive;
} else if (brakeOrRev) {
if (vTangent > 35) {
// Fren
driveForce = -Math.sign(vTangent) * Math.min(Math.abs(vTangent) * car.mass * 8, car.mass * 16);
} else {
// Geri vites
driveForce = -maxDrive * 0.72;
}
} else {
// Motor freni ve yuvarlanma direnci
driveForce = -vTangent * car.mass * rollingResist;
}
// Maksimum statik/kinetik sürtünme sınırı (patinaj ve kayma)
const maxFriction = normalForce * baseGrip * 0.85;
const desiredTangentialForce = driveForce - vTangent * car.mass * 1.4;
const clampedTangentialForce = Math.max(-maxFriction, Math.min(maxFriction, desiredTangentialForce));
w.slip = Math.min(1, Math.abs(desiredTangentialForce - clampedTangentialForce) / (car.mass * 6 + 1) +
(throttle && Math.abs(vTangent) < 40 && surf === "mud" ? 0.55 : 0));
const ftX = norm.tx * clampedTangentialForce;
const ftY = norm.ty * clampedTangentialForce;
fx += ftX;
fy += ftY;
torque += rx * ftY - ry * ftX;
// Tekerlek dönüş hızı
car.wheelSpin += (vTangent * 0.045 + throttle * (surf === "mud" ? 0.14 : 0.06)) * dt * 60;
// Zemin parçacıkları (çamur, toz veya kıvılcım)
if (Math.random() < (throttle || w.slip > 0.25 ? 0.32 : 0.04)) {
if (surf === "mud") {
spawnParticle(contactPt.x, contactPt.y - 2, 1, {
color: "rgba(92, 56, 24, 0.85)",
spread: 2.8,
upSpeed: 3.2,
minSize: 3.5,
maxSize: 5.5,
drag: 0.12
});
} else if (surf === "rock" && w.slip > 0.45 && throttle) {
spawnParticle(contactPt.x, contactPt.y - 2, 1, {
color: "rgba(251, 191, 36, 0.85)",
spread: 4,
upSpeed: 2.2,
minSize: 2,
maxSize: 3,
drag: 0.08
});
} else if (throttle && Math.abs(vTangent) > 45) {
spawnParticle(contactPt.x, contactPt.y - 2, 1, {
color: "rgba(190, 160, 120, 0.65)",
spread: 3,
upSpeed: 2.0,
minSize: 3,
maxSize: 5
});
}
}
} else {
w.compression = Math.max(0, w.compression - 120 * dt);
w.slip = 0;
}
}
// Havada veya yokuşta ağırlık/pitch kontrolü
const pitchTorqueInput = (tiltRight - tiltLeft) * (activeWheels > 0 ? 150000 : 340000);
torque += pitchTorqueInput;
// Havada tekerleklerin serbest dönüşü
if (activeWheels === 0) {
car.wheelSpin += (throttle - brakeOrRev) * 0.28;
}
// Rijit gövde ivmeleri (v = F/m, alpha = tau/I)
const ax = fx / car.mass;
const ay = fy / car.mass;
const alpha = torque / car.inertia;
car.vx += ax * dt;
car.vy += ay * dt;
car.omega += alpha * dt;
// Açısal sönümleme
car.omega *= Math.pow(0.45, dt);
car.x += car.vx * dt;
car.y += car.vy * dt;
car.angle += car.omega * dt;
// Açıyı [-PI, PI] aralığında tut
while (car.angle > Math.PI) car.angle -= Math.PI * 2;
while (car.angle < -Math.PI) car.angle += Math.PI * 2;
// Sol sınır
if (car.x < 90) {
car.x = 90;
car.vx = Math.max(0, car.vx);
}
// Gövde alt ve tavan çarpışma noktaları (takla / kaya çarpışması)
const hullPoints = [
localToWorld(-54, 12),
localToWorld(0, 14),
localToWorld(56, 10),
localToWorld(-24, -34),
localToWorld(18, -34)
];
for (const hp of hullPoints) {
const gy = getTerrainY(hp.x);
if (hp.y > gy) {
const penetration = hp.y - gy;
const norm = getTerrainNormal(hp.x);
car.x += norm.nx * penetration * 0.7;
car.y += norm.ny * penetration * 0.7;
const rx = hp.x - car.x;
const ry = hp.y - car.y;
const vp = pointVelocity(rx, ry);
const vn = vp.vx * norm.nx + vp.vy * norm.ny;
if (vn < 0) {
const restitution = 0.22;
const j = -(1 + restitution) * vn;
car.vx += norm.nx * j * 0.65;
car.vy += norm.ny * j * 0.65;
car.omega += (rx * norm.ny - ry * norm.nx) * j * 0.00018;
car.vx *= 0.94;
}
}
}
// Ters dönme kontrolü (3 saniye ters kalırsa otomatik kurtarma ipucu/yardımı)
if (Math.abs(car.angle) > Math.PI * 0.68 && speed < 45) {
car.rolloverTimer += dt;
if (car.rolloverTimer > 3.2) {
car.angle *= 0.2;
car.y -= 35;
car.omega = 0;
car.rolloverTimer = 0;
}
} else {
car.rolloverTimer = Math.max(0, car.rolloverTimer - dt);
}
// Zirve kontrolü
if (!reachedSummit && car.x >= 8800) {
reachedSummit = true;
document.getElementById("banner").style.display = "block";
}
// HUD bilgisi
const currentSurf = getSurfaceType(car.x);
const surfNames = { grass: "ÇİM / TOPRAK", rock: "KAYALIK", mud: "DERİN ÇAMUR" };
const gripPct = Math.round(((activeWheels > 0 ? avgGrip / activeWheels : car.wheels.rear.grip) / 1.15) * 100);
const surfEl = document.getElementById("surfVal");
surfEl.textContent = `${surfNames[currentSurf]} %${Math.min(100, gripPct)}`;
surfEl.className = "val " + (currentSurf === "mud" && gripPct < 60 ? "danger" : currentSurf === "mud" ? "warn" : "");
const diffEl = document.getElementById("diffVal");
diffEl.textContent = car.diffLocked ? "4L + DIFF KİLİT" : "4L AÇIK DIFF";
diffEl.className = "val " + (car.diffLocked ? "" : "warn");
}
function update(now) {
const frameDt = Math.min(0.05, (now - lastTime) / 1000);
lastTime = now;
accumulator += frameDt;
while (accumulator >= FIXED_DT) {
for (let s = 0; s < SUBSTEPS; s++) {
stepPhysicsSubstep(SUB_DT);
}
accumulator -= FIXED_DT;
}
// Parçacıklar
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.x += p.vx;
p.y += p.vy;
p.vy += p.drag;
p.size *= 1.025;
p.alpha -= 0.022;
if (p.alpha <= 0) particles.splice(i, 1);
}
// Kamera
const targetCamX = car.x - canvas.width * 0.28 + car.vx * 0.25;
const targetCamY = car.y - canvas.height * 0.56 + car.vy * 0.18;
camX += (targetCamX - camX) * 0.1;
camY += (targetCamY - camY) * 0.1;
// Sayısal göstergeler
const distMeters = Math.max(0, Math.round((car.x - 160) / 14));
const altMeters = Math.max(0, Math.round((520 - car.y) / 12));
const speedKmh = Math.round(Math.hypot(car.vx, car.vy) * 0.16);
document.getElementById("distVal").textContent = `${distMeters} m`;
document.getElementById("altVal").textContent = `${altMeters} m`;
document.getElementById("spdVal").textContent = `${speedKmh} km/s`;
}
function drawBackground() {
const sky = ctx.createLinearGradient(0, 0, 0, canvas.height);
sky.addColorStop(0, "#082f49");
sky.addColorStop(0.55, "#0284c7");
sky.addColorStop(1, "#bae6fd");
ctx.fillStyle = sky;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Uzak dağ silüeti
ctx.fillStyle = "rgba(15, 23, 42, 0.34)";
ctx.beginPath();
ctx.moveTo(0, canvas.height);
for (let sx = 0; sx <= canvas.width + 40; sx += 40) {
const wx = sx + camX * 0.22;
const y = 320 + Math.sin(wx * 0.0014) * 145 + Math.cos(wx * 0.0031) * 75 - camY * 0.14;
ctx.lineTo(sx, y);
}
ctx.lineTo(canvas.width, canvas.height);
ctx.closePath();
ctx.fill();
// Zirve bayrağı
const flagWorldX = 8800;
const flagWorldY = getTerrainY(flagWorldX);
const fx = flagWorldX - camX;
const fy = flagWorldY - camY;
if (fx > -80 && fx < canvas.width + 80) {
ctx.strokeStyle = "#f8fafc";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(fx, fy);
ctx.lineTo(fx, fy - 95);
ctx.stroke();
ctx.fillStyle = "#ef4444";
ctx.beginPath();
ctx.moveTo(fx, fy - 95);
ctx.lineTo(fx + 52, fy - 78);
ctx.lineTo(fx, fy - 60);
ctx.closePath();
ctx.fill();
}
}
function drawTerrain() {
const startWorldX = Math.floor((camX - 80) / TERRAIN_STEP) * TERRAIN_STEP;
const endWorldX = Math.ceil((camX + canvas.width + 80) / TERRAIN_STEP) * TERRAIN_STEP;
ctx.beginPath();
ctx.moveTo(startWorldX - camX, canvas.height + 240);
for (let x = startWorldX; x <= endWorldX; x += TERRAIN_STEP) {
ctx.lineTo(x - camX, getTerrainY(x) - camY);
}
ctx.lineTo(endWorldX - camX, canvas.height + 240);
ctx.closePath();
const earthGrad = ctx.createLinearGradient(0, 100, 0, canvas.height);
earthGrad.addColorStop(0, "#78350f");
earthGrad.addColorStop(0.4, "#451a03");
earthGrad.addColorStop(1, "#1c1917");
ctx.fillStyle = earthGrad;
ctx.fill();
// Yüzey segmentleri: çim, kaya ve çamur
for (let x = startWorldX; x < endWorldX; x += TERRAIN_STEP) {
const surf = getSurfaceType(x + TERRAIN_STEP * 0.5);
const x1 = x - camX;
const y1 = getTerrainY(x) - camY;
const x2 = x + TERRAIN_STEP - camX;
const y2 = getTerrainY(x + TERRAIN_STEP) - camY;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineWidth = 9;
if (surf === "mud") ctx.strokeStyle = "#5c3818";
else if (surf === "rock") ctx.strokeStyle = "#94a3b8";
else ctx.strokeStyle = "#65a30d";
ctx.stroke();
}
// Kaya ve çakıl detayları
ctx.fillStyle = "rgba(28, 25, 23, 0.55)";
for (let x = startWorldX; x <= endWorldX; x += 90) {
const hash = Math.abs(Math.sin(x * 12.9898) * 43758.5453) % 1;
if (hash > 0.42) {
const rx = x + (hash - 0.5) * 36 - camX;
const ry = getTerrainY(x + (hash - 0.5) * 36) - camY + 14 + hash * 28;
ctx.beginPath();
ctx.arc(rx, ry, 5 + hash * 8, 0, Math.PI * 2);
ctx.fill();
}
}
}
function drawWheel(wx, wy, radius, spin, grounded) {
ctx.save();
ctx.translate(wx - camX, wy - camY);
ctx.rotate(spin);
ctx.fillStyle = "#18181b";
ctx.beginPath();
ctx.arc(0, 0, radius, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = grounded ? "#3f3f46" : "#27272a";
const teeth = 10;
for (let i = 0; i < teeth; i++) {
const a = (i / teeth) * Math.PI * 2;
ctx.save();
ctx.rotate(a);
ctx.fillRect(-4, -radius - 2, 8, 5);
ctx.restore();
}
ctx.fillStyle = "#e2e8f0";
ctx.beginPath();
ctx.arc(0, 0, radius * 0.5, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = "#334155";
ctx.lineWidth = 2.5;
for (let i = 0; i < 4; i++) {
const a = (i / 4) * Math.PI;
ctx.beginPath();
ctx.moveTo(Math.cos(a) * radius * 0.44, Math.sin(a) * radius * 0.44);
ctx.lineTo(-Math.cos(a) * radius * 0.44, -Math.sin(a) * radius * 0.44);
ctx.stroke();
}
ctx.restore();
}
function drawCar() {
const rearMount = localToWorld(-car.halfWheelbase, car.attachOffsetY);
const frontMount = localToWorld(car.halfWheelbase, car.attachOffsetY);
const rearWheelPos = localToWorld(-car.halfWheelbase, car.attachOffsetY + 18 - car.wheels.rear.compression);
const frontWheelPos = localToWorld(car.halfWheelbase, car.attachOffsetY + 18 - car.wheels.front.compression);
// Süspansiyon kolları / amortisörler
ctx.strokeStyle = "#f59e0b";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(rearMount.x - camX, rearMount.y - camY);
ctx.lineTo(rearWheelPos.x - camX, rearWheelPos.y - camY);
ctx.moveTo(frontMount.x - camX, frontMount.y - camY);
ctx.lineTo(frontWheelPos.x - camX, frontWheelPos.y - camY);
ctx.stroke();
drawWheel(rearWheelPos.x, rearWheelPos.y, car.wheelRadius, car.wheelSpin, car.wheels.rear.grounded);
drawWheel(frontWheelPos.x, frontWheelPos.y, car.wheelRadius, car.wheelSpin, car.wheels.front.grounded);
// Araç gövdesi
ctx.save();
ctx.translate(car.x - camX, car.y - camY);
ctx.rotate(car.angle);
ctx.fillStyle = "#334155";
ctx.fillRect(-54, 8, 108, 9);
ctx.fillStyle = "#ea580c";
ctx.beginPath();
ctx.moveTo(-56, 10);
ctx.lineTo(-56, -8);
ctx.lineTo(-34, -12);
ctx.lineTo(-22, -32);
ctx.lineTo(18, -32);
ctx.lineTo(32, -12);
ctx.lineTo(58, -6);
ctx.lineTo(58, 10);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#0f172a";
ctx.beginPath();
ctx.moveTo(-18, -28);
ctx.lineTo(-2, -28);
ctx.lineTo(-2, -13);
ctx.lineTo(-27, -13);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.moveTo(3, -28);
ctx.lineTo(15, -28);
ctx.lineTo(25, -13);
ctx.lineTo(3, -13);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#1e293b";
ctx.fillRect(-26, -36, 46, 4);
ctx.fillStyle = "#fef08a";
ctx.fillRect(16, -37, 6, 3);
ctx.fillStyle = "#fef9c3";
ctx.fillRect(54, -4, 5, 7);
ctx.restore();
}
function drawParticles() {
for (const p of particles) {
ctx.save();
ctx.globalAlpha = Math.max(0, p.alpha);
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.x - camX, p.y - camY, p.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
function loop(now) {
update(now);
drawBackground();
drawTerrain();
drawParticles();
drawCar();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
</script>
</body>
</html>1
1
24KB
24KB
189.0ms
212.0ms
190.0ms