inhance move

This commit is contained in:
math-pixel
2026-05-19 15:50:11 +02:00
parent 96d7ec7fc0
commit ce0eb90321
3 changed files with 91 additions and 24 deletions
+26 -12
View File
@@ -27,6 +27,7 @@ import { InteractionManager } from "@/managers/InteractionManager";
import { useGameStore } from "@/managers/stores/useGameStore";
import { useSettingsStore } from "@/managers/stores/useSettingsStore";
import type { Vector3Tuple } from "@/types/three/three";
import { EBIKE_CAMERA_TRANSFORM } from "@/components/ebike/Ebike";
type Keys = {
forward: boolean;
@@ -111,6 +112,7 @@ export function PlayerController({
const movementMode = useGameStore((state) => state.player.movementMode);
const movementModeRef = useRef(movementMode);
const prevMovementModeRef = useRef(movementMode);
const ebikeAngle = useRef(0);
useEffect(() => {
movementModeRef.current = movementMode;
@@ -130,6 +132,9 @@ export function PlayerController({
onFloor.current = false;
wantsJump.current = false;
// Initialize ebikeAngle to the bike's visual orientation (0 by default)
ebikeAngle.current = 0;
// Position the camera exactly at the EBIKE_CAMERA_TRANSFORM offset [-3, 8, 0]
const cameraOffset = new THREE.Vector3(-3, 8, 0);
const camPos = new THREE.Vector3()
@@ -266,18 +271,23 @@ export function PlayerController({
if (movementModeRef.current === "ebike") {
const turnSpeed = 1.8; // radians per second
if (keys.current.left) {
camera.rotateOnWorldAxis(_up, turnSpeed * dt);
ebikeAngle.current += turnSpeed * dt;
}
if (keys.current.right) {
camera.rotateOnWorldAxis(_up, -turnSpeed * dt);
ebikeAngle.current -= turnSpeed * dt;
}
}
camera.getWorldDirection(_forward);
_forward.setY(0);
if (_forward.lengthSq() > 0) {
_forward.normalize();
if (movementModeRef.current === "ebike") {
_forward.set(Math.sin(ebikeAngle.current), 0, Math.cos(ebikeAngle.current)).normalize();
_right.crossVectors(_forward, _up).normalize();
} else {
camera.getWorldDirection(_forward);
_forward.setY(0);
if (_forward.lengthSq() > 0) {
_forward.normalize();
_right.crossVectors(_forward, _up).normalize();
}
}
_wishDir.set(0, 0, 0);
@@ -337,24 +347,28 @@ export function PlayerController({
}
}
const euler = new THREE.Euler().setFromQuaternion(camera.quaternion, "YXZ");
if (movementModeRef.current === "ebike") {
// Offset of [-3, 8, 0] rotated by the camera's actual yaw (euler.y)
const cameraOffset = new THREE.Vector3(-3, 8, 0);
cameraOffset.applyAxisAngle(_up, euler.y);
// Offset of position rotated by e-bike angle
const cameraOffset = new THREE.Vector3(...EBIKE_CAMERA_TRANSFORM.position);
cameraOffset.applyAxisAngle(_up, ebikeAngle.current);
const camPos = new THREE.Vector3()
.copy(capsule.current.end)
.add(cameraOffset);
camera.position.copy(camPos);
// Set camera rotation strictly to EBIKE_CAMERA_TRANSFORM.rotation + ebikeAngle.current
const pitchRad = THREE.MathUtils.degToRad(EBIKE_CAMERA_TRANSFORM.rotation[0]);
const yawRad = THREE.MathUtils.degToRad(EBIKE_CAMERA_TRANSFORM.rotation[1]) + ebikeAngle.current;
const rollRad = THREE.MathUtils.degToRad(EBIKE_CAMERA_TRANSFORM.rotation[2]);
camera.rotation.set(pitchRad, yawRad, rollRad, "YXZ");
} else {
camera.position.copy(capsule.current.end);
}
// Save player capsule end position and camera yaw globally so other components (like Ebike) can access it
(window as any).playerPos = [capsule.current.end.x, capsule.current.end.y, capsule.current.end.z];
(window as any).ebikeAngle = euler.y;
(window as any).ebikeAngle = ebikeAngle.current;
});
return null;