corrected steering rotation for vehicle

This commit is contained in:
Poovizhi99 2025-05-08 13:42:05 +05:30
parent c5bf98a534
commit d329fe3eef
2 changed files with 19 additions and 45 deletions

View File

@ -85,8 +85,6 @@ const VehicleUI = () => {
); );
if (selectedVehicle) { if (selectedVehicle) {
setSelectedVechicleData({ position: selectedVehicle.position, rotation: selectedVehicle.rotation }); setSelectedVechicleData({ position: selectedVehicle.position, rotation: selectedVehicle.rotation });
setPoint(selectedVehicle.point.position) setPoint(selectedVehicle.point.position)
} }
@ -105,10 +103,7 @@ const VehicleUI = () => {
const localPosition = outerGroup.current.worldToLocal(worldPos.clone()); const localPosition = outerGroup.current.worldToLocal(worldPos.clone());
setStartPosition([ setStartPosition([localPosition.x, selectedVehicle.point.position[1], localPosition.z,
localPosition.x,
selectedVehicle.point.position[1],
localPosition.z,
]); ]);
setStartRotation([pickUpPoint.rotation.x, pickUpPoint.rotation.y, pickUpPoint.rotation.z,]) setStartRotation([pickUpPoint.rotation.x, pickUpPoint.rotation.y, pickUpPoint.rotation.z,])
} else { } else {
@ -121,10 +116,7 @@ const VehicleUI = () => {
const localPosition = outerGroup.current.worldToLocal(worldPos); const localPosition = outerGroup.current.worldToLocal(worldPos);
setEndPosition([localPosition.x, selectedVehicle.point.position[1], localPosition.z]); setEndPosition([localPosition.x, selectedVehicle.point.position[1], localPosition.z]);
setEndRotation([ setEndRotation([unLoadPoint.rotation.x, unLoadPoint.rotation.y, unLoadPoint.rotation.z,
unLoadPoint.rotation.x,
unLoadPoint.rotation.y,
unLoadPoint.rotation.z,
]); ]);
} else { } else {
setEndPosition([0, selectedVehicle.point.position[1] + 0.1, -1.5]); setEndPosition([0, selectedVehicle.point.position[1] + 0.1, -1.5]);

View File

@ -25,6 +25,7 @@ function VehicleAnimator({ path, handleCallBack, currentPhase, agvUuid, agvDetai
const completedRef = useRef<boolean>(false); const completedRef = useRef<boolean>(false);
const isPausedRef = useRef<boolean>(false); const isPausedRef = useRef<boolean>(false);
const pauseTimeRef = useRef<number | null>(null); const pauseTimeRef = useRef<number | null>(null);
const [objectRotation, setObjectRotation] = useState<{ x: number; y: number; z: number } | undefined>(agvDetail.point?.action?.pickUpPoint?.rotation || { x: 0, y: 0, z: 0 })
const [progress, setProgress] = useState<number>(0); const [progress, setProgress] = useState<number>(0);
const [restRotation, setRestingRotation] = useState<boolean>(true); const [restRotation, setRestingRotation] = useState<boolean>(true);
const [currentPath, setCurrentPath] = useState<[number, number, number][]>([]); const [currentPath, setCurrentPath] = useState<[number, number, number][]>([]);
@ -32,21 +33,19 @@ function VehicleAnimator({ path, handleCallBack, currentPhase, agvUuid, agvDetai
let startTime: number; let startTime: number;
let fixedInterval: number; let fixedInterval: number;
let coveredDistance = progressRef.current; let coveredDistance = progressRef.current;
let objectRotation = (agvDetail.point?.action?.pickUpPoint?.rotation || { x: 0, y: 0, z: 0 }) as { x: number; y: number; z: number } | undefined;
const [rotatingToSteering, setRotatingToSteering] = useState(false);
useEffect(() => { useEffect(() => {
if (currentPhase === 'stationed-pickup' && path.length > 0) { if (currentPhase === 'stationed-pickup' && path.length > 0) {
setCurrentPath(path); setCurrentPath(path);
objectRotation = agvDetail.point.action?.pickUpPoint?.rotation setObjectRotation(agvDetail.point.action?.pickUpPoint?.rotation)
} else if (currentPhase === 'pickup-drop' && path.length > 0) { } else if (currentPhase === 'pickup-drop' && path.length > 0) {
objectRotation = agvDetail.point.action?.unLoadPoint?.rotation setObjectRotation(agvDetail.point.action?.unLoadPoint?.rotation)
setCurrentPath(path); setCurrentPath(path);
} else if (currentPhase === 'drop-pickup' && path.length > 0) { } else if (currentPhase === 'drop-pickup' && path.length > 0) {
objectRotation = agvDetail.point.action?.pickUpPoint?.rotation setObjectRotation(agvDetail.point.action?.pickUpPoint?.rotation)
setCurrentPath(path); setCurrentPath(path);
} }
}, [currentPhase, path]); }, [currentPhase, path, objectRotation]);
useEffect(() => { useEffect(() => {
setProgress(0); setProgress(0);
@ -125,43 +124,26 @@ function VehicleAnimator({ path, handleCallBack, currentPhase, agvUuid, agvDetai
if (isAligned) { if (isAligned) {
progressRef.current += delta * (speed * agvDetail.speed); progressRef.current += delta * (speed * agvDetail.speed);
coveredDistance = progressRef.current; coveredDistance = progressRef.current;
const t = (coveredDistance - accumulatedDistance) / segmentDistance; const t = (coveredDistance - accumulatedDistance) / segmentDistance;
const position = start.clone().lerp(end, t); const position = start.clone().lerp(end, t);
object.position.copy(position); object.position.copy(position);
} }
} }
if (progressRef.current >= totalDistance) { if (progressRef.current >= totalDistance) {
const restQuaternion = objectRotation ? if (restRotation && objectRotation) {
new THREE.Quaternion().setFromEuler(new THREE.Euler(objectRotation.x, objectRotation.y, objectRotation.z)) : const targetEuler = new THREE.Euler(
object.quaternion.clone(); objectRotation.x,
objectRotation.y - (agvDetail.point.action.steeringAngle),
const steeringQuaternion = new THREE.Quaternion().setFromEuler( objectRotation.z
new THREE.Euler(objectRotation ? objectRotation.x : object.rotation.x,
agvDetail.point.action.steeringAngle,
objectRotation ? objectRotation.z : object.rotation.z)
); );
const targetQuaternion = new THREE.Quaternion().setFromEuler(targetEuler);
if (restRotation) { object.quaternion.slerp(targetQuaternion, delta * 2);
object.quaternion.slerp(restQuaternion, delta * 2); if (object.quaternion.angleTo(targetQuaternion) < 0.01) {
const restAngleDiff = object.quaternion.angleTo(restQuaternion); object.quaternion.copy(targetQuaternion);
if (restAngleDiff < 0.01) { object.rotation.copy(targetEuler);
setRestingRotation(false); setRestingRotation(false);
setRotatingToSteering(true);
}
return;
}
else if (rotatingToSteering) {
object.quaternion.slerp(steeringQuaternion, delta * 2);
const steeringAngleDiff = object.quaternion.angleTo(steeringQuaternion);
if (steeringAngleDiff < 0.01) {
object.rotation.set(
objectRotation ? objectRotation.x : object.rotation.x,
agvDetail.point.action.steeringAngle,
objectRotation ? objectRotation.z : object.rotation.z
);
setRotatingToSteering(false);
} }
return; return;
} }