From 43d21a522c6da72e05727f9f34c6c41991a36dfc Mon Sep 17 00:00:00 2001 From: SreeNath14 <153710861+SreeNath14@users.noreply.github.com> Date: Wed, 16 Apr 2025 12:09:18 +0530 Subject: [PATCH] bug fix --- .../simulation/process/processAnimator.tsx | 173 ++++++++++++------ 1 file changed, 118 insertions(+), 55 deletions(-) diff --git a/app/src/modules/simulation/process/processAnimator.tsx b/app/src/modules/simulation/process/processAnimator.tsx index 19e55b8..f6cda0b 100644 --- a/app/src/modules/simulation/process/processAnimator.tsx +++ b/app/src/modules/simulation/process/processAnimator.tsx @@ -9,7 +9,6 @@ import { useProcessAnimation } from "./useProcessAnimations"; import ProcessObject from "./processObject"; import { ProcessData } from "./types"; - interface ArmBotState { uuid: string; position: [number, number, number]; @@ -21,7 +20,12 @@ interface ArmBotState { source: { modelUUID: string; pointUUID: string }; targets: { modelUUID: string; pointUUID: string }[]; }; - actions: { uuid: string; name: string; speed: number; processes: { triggerId: string; startPoint: string; endPoint: string }[]; }; + actions: { + uuid: string; + name: string; + speed: number; + processes: { triggerId: string; startPoint: string; endPoint: string }[]; + }; isActive?: boolean; } interface ProcessContainerProps { @@ -39,7 +43,7 @@ const ProcessAnimator: React.FC = ({ agvRef, MaterialRef, armBots, - setArmBots + setArmBots, }) => { const gltf = useLoader(GLTFLoader, crate) as GLTF; const groupRef = useRef(null); @@ -114,7 +118,6 @@ const ProcessAnimator: React.FC = ({ // In processAnimator.tsx - only the relevant spawn logic part that needs fixes - // Add this function to ProcessAnimator component const isConnectedToActiveArmBot = useCallback( (processId: any) => { @@ -123,7 +126,7 @@ const ProcessAnimator: React.FC = ({ if (!armbot.isActive) return false; // Check if this armbot is connected to the process - return armbot.connections?.targets?.some((connection: any) => { + return armbot.connections?.targets?.some((connection) => { // Find the process that owns this modelUUID const connectedProcess = processes.find((p) => p.paths?.some((path) => path.modeluuid === connection.modelUUID) @@ -135,8 +138,7 @@ const ProcessAnimator: React.FC = ({ [armBots, processes] ); - // In processAnimator.tsx - only the relevant spawn logic part that needs fixes - + // First useFrame for spawn logic useFrame(() => { // Spawn logic frame const currentTime = @@ -149,12 +151,15 @@ const ProcessAnimator: React.FC = ({ const processState = newStates[process.id]; if (!processState) return; + // Check connection status + const isConnected = isConnectedToActiveArmBot(process.id); + if (processState.isProcessDelaying) { // Existing delay handling logic... return; } - if (isConnectedToActiveArmBot(process.id)) { + if (isConnected) { newStates[process.id] = { ...processState, nextSpawnTime: Infinity, // Prevent future spawns @@ -163,17 +168,24 @@ const ProcessAnimator: React.FC = ({ } const spawnPoint = findSpawnPoint(process); - if (!spawnPoint || !spawnPoint.actions) return; + if (!spawnPoint || !spawnPoint.actions) { + console.log( + `Process ${process.id} has no valid spawn point or actions` + ); + return; + } const spawnAction = spawnPoint.actions.find( (a) => a.isUsed && a.type === "Spawn" ); - if (!spawnAction) return; + if (!spawnAction) { + return; + } const spawnInterval = typeof spawnAction.spawnInterval === "number" ? spawnAction.spawnInterval - : parseFloat(spawnAction.spawnInterval as string) || 0; + : parseFloat(spawnAction.spawnInterval || "0") || 0; // Check if this is a zero interval spawn and we already spawned an object if ( @@ -195,6 +207,15 @@ const ProcessAnimator: React.FC = ({ baseMaterials ); + // Initialize state properly to ensure animation + newObject.state = { + ...newObject.state, + isAnimating: true, + isDelaying: false, + delayComplete: false, + progress: 0.005, // Start with tiny progress to ensure animation begins + }; + // Update state with the new object and flag for zero interval newStates[process.id] = { ...processState, @@ -217,6 +238,7 @@ const ProcessAnimator: React.FC = ({ }); }); + // Second useFrame for animation logic useFrame((_, delta) => { // Animation logic frame const currentTime = @@ -227,9 +249,19 @@ const ProcessAnimator: React.FC = ({ processedProcesses.forEach((process) => { const processState = newStates[process.id]; - if (!processState) return; + if (!processState) { + return; + } - if (isConnectedToActiveArmBot(process.id)) { + // Check connection status with debugging + const isConnected = isConnectedToActiveArmBot(process.id); + console.log( + `Process ${process.id} animation - connected:`, + isConnected + ); + + if (isConnected) { + // Stop all animations when connected to active arm bot newStates[process.id] = { ...processState, spawnedObjects: Object.entries(processState.spawnedObjects).reduce( @@ -252,6 +284,7 @@ const ProcessAnimator: React.FC = ({ return; } + // Process delay handling if (processState.isProcessDelaying) { const effectiveDelayTime = processState.processDelayDuration / speedRef.current; @@ -260,6 +293,9 @@ const ProcessAnimator: React.FC = ({ currentTime - processState.processDelayStartTime >= effectiveDelayTime ) { + console.log( + `Process ${process.id} delay completed, resuming animation` + ); newStates[process.id] = { ...processState, isProcessDelaying: false, @@ -283,26 +319,42 @@ const ProcessAnimator: React.FC = ({ {} ), }; - return newStates; + return; } else { - return newStates; + return; } } + // Ensure we have a valid path to follow const path = process.animationPath?.map((p) => new THREE.Vector3(p.x, p.y, p.z)) || []; - if (path.length < 2) return; + + if (path.length < 2) { + console.log( + `Process ${process.id} has insufficient path points: ${path.length}` + ); + return; + } const updatedObjects = { ...processState.spawnedObjects }; + let animationOccurring = false; // Track if any animation is happening Object.entries(processState.spawnedObjects).forEach( ([objectId, obj]) => { - if (!obj.visible) return; + if (!obj.visible) { + return; + } const currentRef = gltf?.scene ? obj.ref.current : obj.ref.current; - if (!currentRef) return; + if (!currentRef) { + console.log( + `No reference for object ${objectId}, skipping animation` + ); + return; + } + // Initialize position for new objects if ( obj.position && obj.state.currentIndex === 0 && @@ -313,11 +365,22 @@ const ProcessAnimator: React.FC = ({ const stateRef = obj.state; + // Ensure animation state is properly set for objects + if (!stateRef.isAnimating && !stateRef.isDelaying && !isConnected) { + stateRef.isAnimating = true; + stateRef.progress = + stateRef.progress > 0 ? stateRef.progress : 0.005; + } + + // Handle delay logic if (stateRef.isDelaying) { const effectiveDelayTime = stateRef.currentDelayDuration / speedRef.current; if (currentTime - stateRef.delayStartTime >= effectiveDelayTime) { + console.log( + `Delay complete for object ${objectId}, resuming animation` + ); stateRef.isDelaying = false; stateRef.delayComplete = true; stateRef.isAnimating = true; @@ -343,8 +406,17 @@ const ProcessAnimator: React.FC = ({ } } - if (!stateRef.isAnimating) return; + // Skip non-animating objects + if (!stateRef.isAnimating) { + console.log( + `Object ${objectId} not animating, skipping animation updates` + ); + return; + } + animationOccurring = true; // Mark that animation is happening + + // Handle point actions const currentPointData = getPointDataForAnimationIndex( process, stateRef.currentIndex @@ -369,42 +441,7 @@ const ProcessAnimator: React.FC = ({ const nextPointIdx = stateRef.currentIndex + 1; const isLastPoint = nextPointIdx >= path.length; - // if (isLastPoint) { - // if (currentPointData?.actions) { - // const shouldStop = !hasNonInheritActions( - // currentPointData.actions - // ); - // if (shouldStop) { - // return; - // } - // } - // } - - // if (isLastPoint) { - // if (currentPointData?.actions) { - // const hasNonInherit = hasNonInheritActions( - // currentPointData.actions - // ); - // if (!hasNonInherit) { - // // Remove the object if all actions are inherit - // updatedObjects[objectId] = { - // ...obj, - // visible: false, - // state: { ...stateRef, isAnimating: false }, - // }; - // return; - // } - // } else { - // // No actions at last point - remove the object - // updatedObjects[objectId] = { - // ...obj, - // visible: false, - // state: { ...stateRef, isAnimating: false }, - // }; - // return; - // } - // } - + // Handle objects at the last point if (isLastPoint) { const isAgvPicking = agvRef.current.some( (agv: any) => @@ -417,6 +454,9 @@ const ProcessAnimator: React.FC = ({ if (shouldHide) { if (isAgvPicking) { + console.log( + `AGV picking at last point for object ${objectId}, hiding object` + ); updatedObjects[objectId] = { ...obj, visible: false, @@ -442,6 +482,7 @@ const ProcessAnimator: React.FC = ({ } } + // Handle stacked objects when AGV picks if (tempStackedObjectsRef.current[objectId]) { const isAgvPicking = agvRef.current.some( (agv: any) => @@ -459,9 +500,12 @@ const ProcessAnimator: React.FC = ({ isAnimating: false, }, }; + + return; } } + // Handle normal animation progress for objects not at last point if (!isLastPoint) { const nextPoint = path[nextPointIdx]; const distance = @@ -469,13 +513,21 @@ const ProcessAnimator: React.FC = ({ const effectiveSpeed = stateRef.speed * speedRef.current; const movement = effectiveSpeed * delta; + // Ensure progress is always moving forward if (stateRef.delayComplete && stateRef.progress < 0.01) { stateRef.progress = 0.05; stateRef.delayComplete = false; + console.log( + `Boosting progress for object ${objectId} after delay` + ); } else { stateRef.progress += movement / distance; + console.log( + `Object ${objectId} progress: ${stateRef.progress.toFixed(3)}` + ); } + // Handle point transition if (stateRef.progress >= 1) { stateRef.currentIndex = nextPointIdx; stateRef.progress = 0; @@ -494,7 +546,10 @@ const ProcessAnimator: React.FC = ({ process, stateRef.currentIndex ); + + // No action needed with newPointData here - will be handled in next frame } else { + // Update position with lerp currentRef.position.lerpVectors( path[stateRef.currentIndex], nextPoint, @@ -507,6 +562,13 @@ const ProcessAnimator: React.FC = ({ } ); + // Log if no animation is occurring when it should + if (!animationOccurring && !isConnected) { + console.log( + `Warning: No animation occurring for process ${process.id} despite not being connected` + ); + } + newStates[process.id] = { ...processState, spawnedObjects: updatedObjects, @@ -516,6 +578,7 @@ const ProcessAnimator: React.FC = ({ return newStates; }); }); + if (!processedProcesses || processedProcesses.length === 0) { return null; }