Skip to content

Commit 79e5a3d

Browse files
three.js platformer: fixed-rate camera smoothing, derive start pos from offset
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 673cd59 commit 79e5a3d

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

examples/threejsPlatformer/game.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ function threeRender()
7878
if (threeCanvas.style.cssText != LJS.mainCanvas.style.cssText)
7979
threeCanvas.style.cssText = LJS.mainCanvas.style.cssText;
8080

81-
// fixed offset chase camera, smoothly following the player
82-
const eye = new THREE.Vector3(player.pos.x, player.pos.y, player.zPos).add(cameraOffset);
83-
camera.position.lerp(eye, cameraLerp);
81+
// aim at the player and render
8482
camera.lookAt(player.pos.x, player.pos.y, player.zPos + 1);
8583
renderer.render(scene, camera);
8684
}
@@ -272,11 +270,15 @@ function gameInit()
272270
}
273271

274272
// start with the camera behind the player
275-
camera.position.set(player.pos.x, player.pos.y - 9, 6);
273+
camera.position.set(player.pos.x, player.pos.y, player.zPos).add(cameraOffset);
276274
}
277275

278276
function gameUpdate()
279277
{
278+
// fixed offset chase camera, smoothly following the player
279+
// updated at the fixed 60 fps rate so the feel is refresh rate independent
280+
const eye = new THREE.Vector3(player.pos.x, player.pos.y, player.zPos).add(cameraOffset);
281+
camera.position.lerp(eye, cameraLerp);
280282
}
281283

282284
function gameUpdatePost()

0 commit comments

Comments
 (0)