|
| 1 | +// Velocity System - Updates entity positions based on velocity |
| 2 | +import { Quaternion, Vector3 } from 'three'; |
| 3 | + |
| 4 | +import { Transform, Velocity, velocityQuery, world } from '@core/lib/ecs'; |
| 5 | + |
| 6 | +// Temporary vectors to avoid garbage collection |
| 7 | +const linearVelocity = new Vector3(); |
| 8 | +const angularVelocity = new Vector3(); |
| 9 | +const tempQuat = new Quaternion(); |
| 10 | + |
| 11 | +/** |
| 12 | + * System that applies velocity to transform positions |
| 13 | + * |
| 14 | + * @param deltaTime Time since last frame in seconds |
| 15 | + * @returns Number of entities processed |
| 16 | + */ |
| 17 | +export function runVelocitySystem(deltaTime: number): number { |
| 18 | + try { |
| 19 | + // Find all entities with Transform and Velocity components |
| 20 | + const entities = velocityQuery(world); |
| 21 | + |
| 22 | + for (let i = 0; i < entities.length; i++) { |
| 23 | + const eid = entities[i]; |
| 24 | + |
| 25 | + // Get linear velocity |
| 26 | + linearVelocity.set(Velocity.linear[eid][0], Velocity.linear[eid][1], Velocity.linear[eid][2]); |
| 27 | + |
| 28 | + // Skip if velocity is zero |
| 29 | + if (linearVelocity.lengthSq() > 0.00001) { |
| 30 | + // Apply linear damping |
| 31 | + const linearDamping = Velocity.linearDamping[eid]; |
| 32 | + if (linearDamping > 0) { |
| 33 | + linearVelocity.multiplyScalar(Math.pow(1 - linearDamping, deltaTime)); |
| 34 | + |
| 35 | + // Update the component values |
| 36 | + Velocity.linear[eid][0] = linearVelocity.x; |
| 37 | + Velocity.linear[eid][1] = linearVelocity.y; |
| 38 | + Velocity.linear[eid][2] = linearVelocity.z; |
| 39 | + } |
| 40 | + |
| 41 | + // Scale by delta time |
| 42 | + linearVelocity.multiplyScalar(deltaTime); |
| 43 | + |
| 44 | + // Apply to position |
| 45 | + Transform.position[eid][0] += linearVelocity.x; |
| 46 | + Transform.position[eid][1] += linearVelocity.y; |
| 47 | + Transform.position[eid][2] += linearVelocity.z; |
| 48 | + |
| 49 | + // Mark for update |
| 50 | + Transform.needsUpdate[eid] = 1; |
| 51 | + } |
| 52 | + |
| 53 | + // Get angular velocity |
| 54 | + angularVelocity.set( |
| 55 | + Velocity.angular[eid][0], |
| 56 | + Velocity.angular[eid][1], |
| 57 | + Velocity.angular[eid][2], |
| 58 | + ); |
| 59 | + |
| 60 | + // Skip if angular velocity is zero |
| 61 | + if (angularVelocity.lengthSq() > 0.00001) { |
| 62 | + // Apply angular damping |
| 63 | + const angularDamping = Velocity.angularDamping[eid]; |
| 64 | + if (angularDamping > 0) { |
| 65 | + angularVelocity.multiplyScalar(Math.pow(1 - angularDamping, deltaTime)); |
| 66 | + |
| 67 | + // Update the component values |
| 68 | + Velocity.angular[eid][0] = angularVelocity.x; |
| 69 | + Velocity.angular[eid][1] = angularVelocity.y; |
| 70 | + Velocity.angular[eid][2] = angularVelocity.z; |
| 71 | + } |
| 72 | + |
| 73 | + // Scale by delta time |
| 74 | + angularVelocity.multiplyScalar(deltaTime); |
| 75 | + |
| 76 | + // Create a rotation quaternion from the angular velocity |
| 77 | + // This is a simplified approach - in a real physics system, we'd use more accurate integration |
| 78 | + tempQuat.setFromAxisAngle(angularVelocity.normalize(), angularVelocity.length()); |
| 79 | + |
| 80 | + // Current rotation |
| 81 | + const currentQuat = new Quaternion( |
| 82 | + Transform.rotation[eid][0], |
| 83 | + Transform.rotation[eid][1], |
| 84 | + Transform.rotation[eid][2], |
| 85 | + Transform.rotation[eid][3], |
| 86 | + ); |
| 87 | + |
| 88 | + // Apply rotation (multiply quaternions) |
| 89 | + currentQuat.premultiply(tempQuat); |
| 90 | + currentQuat.normalize(); // Normalize to prevent floating point errors accumulating |
| 91 | + |
| 92 | + // Update the transform rotation |
| 93 | + Transform.rotation[eid][0] = currentQuat.x; |
| 94 | + Transform.rotation[eid][1] = currentQuat.y; |
| 95 | + Transform.rotation[eid][2] = currentQuat.z; |
| 96 | + Transform.rotation[eid][3] = currentQuat.w; |
| 97 | + |
| 98 | + // Mark for update |
| 99 | + Transform.needsUpdate[eid] = 1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return entities.length; |
| 104 | + } catch (error) { |
| 105 | + console.error('Error in velocity system:', error); |
| 106 | + return 0; |
| 107 | + } |
| 108 | +} |
0 commit comments