Mobil Dokunmatik, Sanal Gamepad (Joystick & D-Pad) ve 100dvh Çoklu Ekran Uyum Motoru
Masaüstü klavye (WASD / Ok Tuşları), fare sürüklemesi ve çoklu parmak dokunuşunu (sol başparmak ile Sanal Analog Joystick / D-Pad yönetirken sağ başparmak ile Atılım & Ateş tetikleme) tek bir normalize vektör veriyolunda birleştiren; 100dvh, env(safe-area-inset-*) ve devicePixelRatio ölçeklemesiyle her ekranda jilet keskinliğinde 60 FPS çalışan kontrol mimarisi.
1. Çoklu Giriş Mimarisi Karşılaştırması: Neden Birleşik PointerEvent Veriyolu?
Yalnızca keydown veya tekil mousedown dinleyen oyunlar mobil tarayıcılarda kilitlenir; touch-action: none unutulduğunda ise parmak sürüklemesi sayfayı aşağı kaydırır veya tarayıcı sekmesini yeniler (pull-to-refresh). Aşağıdaki tablo endüstriyel standartları özetler:
| Katman | Kırılgan Yaklaşım (Anti-Pattern) | _db_core Endüstriyel Standart |
|---|---|---|
| Çoklu Dokunuş | Tek global isMouseDown bayrağı (2. parmak 1. parmağı bozar) |
e.pointerId tabanlı ayrık izleme: Sol başparmak analog kolu tutarken sağ başparmak ateş eder |
| Tarayıcı Jest Koruması | Sürüklerken sayfanın kayması ve çift dokunuşta yakınlaştırma | Oyun sahnesinde touch-action: none; user-select: none; overscroll-behavior: none; |
| Mobil Ekran Yüksekliği | 100vh yüzünden adres çubuğu altında kalan alt butonlar |
100dvh + env(safe-area-inset-bottom) ile çentik ve alt çubuk koruması |
| Çapraz Hız Dengesi | W + D basılınca 1.414x hızlı gitme hatası |
Birim çember normalizasyonu (Math.hypot(vx, vy) > 1 ise bölme) ve radyal ölü bölge |
2. Radyal Ölü Bölge (Deadzone) Ölçeklemeli Sanal Analog Joystick Matematiği
Ham eşik kesmesi (if (mag < deadzone) mag = 0) başparmak ölü bölgeyi geçtiği anda hızın 0.00'dan aniden 0.15'e sıçramasına yol açar. Doğru mühendislik formülü, kalan aralığı (norm - deadzone) / (1 - deadzone) ile yeniden [0..1] aralığına pürüzsüzce yaymaktır:
function computeAnalogStick(originX, originY, pointerX, pointerY, maxRadius = 52, deadzone = 0.14) {
const dx = pointerX - originX;
const dy = pointerY - originY;
const rawDist = Math.hypot(dx, dy);
const angle = Math.atan2(dy, dx);
// Topuz (knob) konumunu maksimum yarıçap çemberine kilitle
const clampedDist = Math.min(rawDist, maxRadius);
const knobX = originX + Math.cos(angle) * clampedDist;
const knobY = originY + Math.sin(angle) * clampedDist;
// [0..1] normalize büyüklük ve kesintisiz (smooth) radyal deadzone ölçeklemesi
const rawNorm = clampedDist / maxRadius;
const scaledMag = rawNorm <= deadzone
? 0
: (rawNorm - deadzone) / (1 - deadzone);
return {
vx: Math.cos(angle) * scaledMag,
vy: Math.sin(angle) * scaledMag,
magnitude: scaledMag,
angleDeg: Math.round((angle * 180) / Math.PI),
knobX,
knobY
};
}
3. Birleşik Klavye + Çoklu Dokunuş (pointerId) ve Kaydırma (Swipe) Giriş Veriyolu
Oyun döngüsü (update(dt)) girişin klavyeden mi, D-Pad'den mi yoksa dokunmatik analog koldan mı geldiğini bilmemelidir. Tek bir InputBus.getState() çağrısı tüm aktif kaynakları birleştirip normalize edilmiş { vx, vy, fire, dash } durumunu döndürür:
function createUnifiedInputBus(stageEl) {
const keys = new Set();
const virtualButtons = { up: false, down: false, left: false, right: false, fire: false, dash: false };
let stick = { active: false, pointerId: null, ox: 86, oy: 0, vx: 0, vy: 0 };
let lastSwipe = null;
const swipeTrack = new Map();
window.addEventListener('keydown', (e) => keys.add(e.code));
window.addEventListener('keyup', (e) => keys.delete(e.code));
stageEl.addEventListener('pointerdown', (e) => {
const rect = stageEl.getBoundingClientRect();
const x = e.clientX - rect.left, y = e.clientY - rect.top;
stageEl.setPointerCapture(e.pointerId);
// Sol yarı ekran: Analog başparmak kolunu bu pointerId'ye tahsis et
if (!stick.active && x < rect.width * 0.55) {
stick = { active: true, pointerId: e.pointerId, ox: x, oy: y, vx: 0, vy: 0 };
} else {
swipeTrack.set(e.pointerId, { x0: x, y0: y, t0: performance.now() });
}
});
stageEl.addEventListener('pointermove', (e) => {
if (stick.active && e.pointerId === stick.pointerId) {
const rect = stageEl.getBoundingClientRect();
const res = computeAnalogStick(stick.ox, stick.oy, e.clientX - rect.left, e.clientY - rect.top);
stick.vx = res.vx;
stick.vy = res.vy;
}
});
stageEl.addEventListener('pointerup', (e) => {
if (stick.active && e.pointerId === stick.pointerId) {
stick.active = false; stick.pointerId = null; stick.vx = 0; stick.vy = 0;
}
const sw = swipeTrack.get(e.pointerId);
if (sw) {
swipeTrack.delete(e.pointerId);
const rect = stageEl.getBoundingClientRect();
const dx = (e.clientX - rect.left) - sw.x0, dy = (e.clientY - rect.top) - sw.y0;
const dt = performance.now() - sw.t0;
if (Math.hypot(dx, dy) > 38 && dt < 420) {
lastSwipe = { angle: Math.atan2(dy, dx), speed: Math.hypot(dx, dy) / Math.max(1, dt) };
}
}
});
return {
setVirtualButton(name, isDown) { virtualButtons[name] = isDown; },
getState() {
let kx = (keys.has('KeyD') || keys.has('ArrowRight') || virtualButtons.right ? 1 : 0)
- (keys.has('KeyA') || keys.has('ArrowLeft') || virtualButtons.left ? 1 : 0);
let ky = (keys.has('KeyS') || keys.has('ArrowDown') || virtualButtons.down ? 1 : 0)
- (keys.has('KeyW') || keys.has('ArrowUp') || virtualButtons.up ? 1 : 0);
if (kx !== 0 && ky !== 0) { const inv = 1 / Math.SQRT2; kx *= inv; ky *= inv; }
const vx = stick.active ? stick.vx : kx;
const vy = stick.active ? stick.vy : ky;
return {
vx, vy,
fire: keys.has('Space') || virtualButtons.fire,
dash: keys.has('ShiftLeft') || keys.has('ShiftRight') || virtualButtons.dash,
lastSwipe
};
}
};
}
4. 100dvh, env(safe-area-inset-*) ve Retina devicePixelRatio Sahne Mimarisi
Tam ekran web oyunlarında ve etkileşimli stüdyolarda sanal kontrol butonlarının iPhone Home çubuğu veya kamera çentiğiyle çakışmaması için CSS güvenli bölge değişkenleri ile ResizeObserver tabanlı DPR ölçeklemesi birlikte kullanılır:
/* CSS: 100dvh + Çentik/Alt Çubuk Güvenli Bölge Yastıklaması */
/*
.game-shell {
width: 100%;
height: 100vh;
height: 100dvh;
padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
touch-action: none;
overscroll-behavior: none;
}
*/
function mountCrispRetinaCanvas(canvas, container, onResize) {
const ctx = canvas.getContext('2d');
function syncSize() {
const rect = container.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2); // Pil dostu 2x Retina tavanı
const logicalW = Math.max(300, Math.floor(rect.width));
const logicalH = Math.max(240, Math.floor(rect.height));
canvas.width = Math.floor(logicalW * dpr);
canvas.height = Math.floor(logicalH * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0); // Çizim koordinatlarını CSS pikseline eşitle
if (onResize) onResize(logicalW, logicalH, dpr);
}
const ro = new ResizeObserver(syncSize);
ro.observe(container);
syncSize();
return { ctx, destroy: () => ro.disconnect() };
}
78px, aksiyon kümesini sağ alttan 72px içeride konumlandırın.
pointercancel olayını da pointerup ile aynı temizleyiciye bağlayarak takılı kalan tuşları sıfırlayın.
Yorumlar
Yorum Gönder