mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-22 03:07:04 +02:00
feat: initial turret rotation
This commit is contained in:
1 parent
d408df8b4c
commit
c10060decc
5 files changed
+97
-36
No files matched your search
@@ -3,6 +3,7 @@ import { clamp } from 'lodash';
|
||||
import { RefObject, forwardRef, useImperativeHandle, useRef } from 'react';
|
||||
import { Group, Mesh, Object3D, Vector3 } from 'three';
|
||||
import {
|
||||
InitialTurretRotation,
|
||||
PitchLimits,
|
||||
YawLimits,
|
||||
} from '../../../../../core/blitzkrieg/modelDefinitions';
|
||||
@@ -20,6 +21,7 @@ interface GunContainerProps {
|
||||
pitchLimits: PitchLimits;
|
||||
onPitchStart: () => void;
|
||||
onPitchEnd: (pitch: number, yaw: number) => void;
|
||||
initialTurretRotation?: InitialTurretRotation;
|
||||
turretContainer: RefObject<Group>;
|
||||
}
|
||||
|
||||
@@ -29,6 +31,7 @@ export const GunContainer = forwardRef<Group, GunContainerProps>(
|
||||
gunOrigin,
|
||||
turretOrigin,
|
||||
pitch,
|
||||
initialTurretRotation,
|
||||
yaw,
|
||||
objects,
|
||||
yawLimits,
|
||||
@@ -163,9 +166,21 @@ export const GunContainer = forwardRef<Group, GunContainerProps>(
|
||||
turretContainer.current.position
|
||||
.set(0, 0, 0)
|
||||
.sub(turretOrigin)
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), draftYaw)
|
||||
.add(turretOrigin);
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), draftYaw);
|
||||
turretContainer.current.rotation.z = draftYaw;
|
||||
|
||||
if (initialTurretRotation) {
|
||||
const pitch = -initialTurretRotation.pitch * (Math.PI / 180);
|
||||
const yaw = -initialTurretRotation.yaw * (Math.PI / 180);
|
||||
const roll = -initialTurretRotation.roll * (Math.PI / 180);
|
||||
|
||||
turretContainer.current.position
|
||||
.applyAxisAngle(new Vector3(1, 0, 0), pitch)
|
||||
.applyAxisAngle(new Vector3(0, 1, 0), roll)
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), yaw);
|
||||
}
|
||||
|
||||
turretContainer.current.position.add(turretOrigin);
|
||||
}
|
||||
}
|
||||
function handlePointerUp() {
|
||||
|
||||
@@ -7,8 +7,9 @@ import {
|
||||
useImperativeHandle,
|
||||
useRef,
|
||||
} from 'react';
|
||||
import { Group, Mesh, Object3D, Vector3 } from 'three';
|
||||
import { Euler, Group, Mesh, Object3D, Vector3 } from 'three';
|
||||
import {
|
||||
InitialTurretRotation,
|
||||
PitchLimits,
|
||||
YawLimits,
|
||||
} from '../../../../../core/blitzkrieg/modelDefinitions';
|
||||
@@ -28,6 +29,7 @@ interface TurretContainerProps {
|
||||
onYawStart: () => void;
|
||||
onYawEnd: (pitch: number, yaw: number) => void;
|
||||
gunContainer: RefObject<Group>;
|
||||
initialTurretRotation?: InitialTurretRotation;
|
||||
}
|
||||
|
||||
export const TurretContainer = forwardRef(
|
||||
@@ -45,23 +47,37 @@ export const TurretContainer = forwardRef(
|
||||
gunOrigin,
|
||||
pitchLimits,
|
||||
onYawStart,
|
||||
initialTurretRotation,
|
||||
}: TurretContainerProps,
|
||||
ref,
|
||||
) => {
|
||||
const canvas = useThree((state) => state.gl.domElement);
|
||||
const turretContainer = useRef<Group>(null);
|
||||
const position = new Vector3()
|
||||
.sub(turretOrigin)
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), yaw);
|
||||
const rotation = new Euler(0, 0, yaw);
|
||||
|
||||
if (initialTurretRotation) {
|
||||
const pitch = -initialTurretRotation.pitch * (Math.PI / 180);
|
||||
const yaw = -initialTurretRotation.yaw * (Math.PI / 180);
|
||||
const roll = -initialTurretRotation.roll * (Math.PI / 180);
|
||||
|
||||
position
|
||||
.applyAxisAngle(new Vector3(1, 0, 0), pitch)
|
||||
.applyAxisAngle(new Vector3(0, 1, 0), roll)
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), yaw);
|
||||
rotation.x += pitch;
|
||||
rotation.y += roll;
|
||||
rotation.z += yaw;
|
||||
}
|
||||
|
||||
position.add(turretOrigin);
|
||||
|
||||
useImperativeHandle(ref, () => turretContainer.current!);
|
||||
|
||||
return (
|
||||
<group
|
||||
position={new Vector3()
|
||||
.sub(turretOrigin)
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), yaw)
|
||||
.add(turretOrigin)}
|
||||
rotation={[0, 0, yaw]}
|
||||
ref={turretContainer}
|
||||
>
|
||||
<group position={position} rotation={rotation} ref={turretContainer}>
|
||||
{objects.map((object) => {
|
||||
const isTurret = object.name.startsWith('turret_');
|
||||
const isCurrentTurret =
|
||||
@@ -167,9 +183,21 @@ export const TurretContainer = forwardRef(
|
||||
turretContainer.current.position
|
||||
.set(0, 0, 0)
|
||||
.sub(turretOrigin)
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), draftYaw)
|
||||
.add(turretOrigin);
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), draftYaw);
|
||||
turretContainer.current.rotation.z = draftYaw;
|
||||
|
||||
if (initialTurretRotation) {
|
||||
const pitch = -initialTurretRotation.pitch * (Math.PI / 180);
|
||||
const yaw = -initialTurretRotation.yaw * (Math.PI / 180);
|
||||
const roll = -initialTurretRotation.roll * (Math.PI / 180);
|
||||
|
||||
turretContainer.current.position
|
||||
.applyAxisAngle(new Vector3(1, 0, 0), pitch)
|
||||
.applyAxisAngle(new Vector3(0, 1, 0), roll)
|
||||
.applyAxisAngle(new Vector3(0, 0, 1), yaw);
|
||||
}
|
||||
|
||||
turretContainer.current.position.add(turretOrigin);
|
||||
}
|
||||
}
|
||||
function handlePointerUp() {
|
||||
|
||||
@@ -109,7 +109,9 @@ export default function Page({ params }: { params: { id: string } }) {
|
||||
function handlePointerMove(event: PointerEvent) {
|
||||
event.preventDefault();
|
||||
}
|
||||
function handlePointerUp() {
|
||||
function handlePointerUp(event: PointerEvent) {
|
||||
event.preventDefault();
|
||||
|
||||
window.removeEventListener('pointermove', handlePointerMove);
|
||||
window.removeEventListener('pointerup', handlePointerUp);
|
||||
}
|
||||
@@ -167,6 +169,7 @@ export default function Page({ params }: { params: { id: string } }) {
|
||||
onTrackEnd={() => setControlsEnabled(true)}
|
||||
>
|
||||
<TurretContainer
|
||||
initialTurretRotation={tankModelDefinition.turretRotation}
|
||||
gunOrigin={gunOrigin}
|
||||
ref={turretContainer}
|
||||
objects={gltf.scene.children[0].children}
|
||||
@@ -186,6 +189,9 @@ export default function Page({ params }: { params: { id: string } }) {
|
||||
>
|
||||
<GunContainer
|
||||
ref={gunContainer}
|
||||
initialTurretRotation={
|
||||
tankModelDefinition.turretRotation
|
||||
}
|
||||
onPitchStart={() => setControlsEnabled(false)}
|
||||
pitchLimits={gunModelDefinition.pitch}
|
||||
turretContainer={turretContainer}
|
||||
|
||||
@@ -4,8 +4,14 @@ import { asset } from './asset';
|
||||
export type ModelDefinitions = Record<number, ModelDefinition>;
|
||||
interface ModelDefinition {
|
||||
turretOrigin: Vector3Tuple;
|
||||
turretRotation?: InitialTurretRotation;
|
||||
turrets: Record<number, TurretModelDefinition>;
|
||||
}
|
||||
export interface InitialTurretRotation {
|
||||
yaw: number;
|
||||
pitch: number;
|
||||
roll: number;
|
||||
}
|
||||
interface TurretModelDefinition {
|
||||
model: number;
|
||||
gunOrigin: Vector3Tuple;
|
||||
|
||||
Reference in new issue
Block a user