feat: initial turret rotation

This commit is contained in:
TrèsAbhi committed 2024-01-03 18:52:22 -06:00
1 parent d408df8b4c
commit c10060decc
5 files changed
+97 -36

No files matched your search

+28 -22
View File
@@ -97,9 +97,8 @@ type ShellDefinitionsList = Record<
interface VehicleDefinitions {
invisibility: { moving: number; still: number; firePenalty: number };
hull: {
turretPositions: {
turret: string;
};
turretPositions: { turret: string };
turretInitialRotation?: { yaw: 0; pitch: 6.5; roll: 0 };
};
turrets0: {
[key: string]: {
@@ -250,6 +249,13 @@ if (allTargets || targets?.includes('definitions')) {
modelDefinitions[tankId] = {
turretOrigin,
turretRotation: tankDefinition.root.hull.turretInitialRotation
? {
yaw: tankDefinition.root.hull.turretInitialRotation.yaw,
pitch: tankDefinition.root.hull.turretInitialRotation.pitch,
roll: tankDefinition.root.hull.turretInitialRotation.roll,
}
: undefined,
turrets: {},
};
@@ -409,25 +415,25 @@ if (allTargets || targets?.includes('definitions')) {
);
console.log('Committing definitions...');
// await commitMultipleFiles(
// 'tresabhi',
// 'blitzkrieg-assets',
// 'main',
// 'definitions',
// [
// {
// content: JSON.stringify(tankDefinitions),
// encoding: 'utf-8',
// path: 'definitions/tanks.json',
// },
// {
// content: JSON.stringify(modelDefinitions),
// encoding: 'utf-8',
// path: 'definitions/models.json',
// },
// ],
// true,
// );
await commitMultipleFiles(
'tresabhi',
'blitzkrieg-assets',
'main',
'definitions',
[
{
content: JSON.stringify(tankDefinitions),
encoding: 'utf-8',
path: 'definitions/tanks.json',
},
{
content: JSON.stringify(modelDefinitions),
encoding: 'utf-8',
path: 'definitions/models.json',
},
],
true,
);
}
if (
@@ -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() {
+7 -1
View File
@@ -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}
+6
View File
@@ -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;