Your question is Colliding Spheres Function. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Magic Leap's spatial computing runtime tracks moving objects in 3D. Given spherical objects with constant velocities, return the earliest collision during a time window without simulating discrete frames.
For every unordered pair of spheres, determine the smallest t in [0, duration] at which the distance between their centers is less than or equal to the sum of their radii. Return [t, i, j] for the earliest collision, where i < j are the sphere indices. If multiple pairs collide at the same time, return the lexicographically smallest pair. Return None if no collision occurs.
An initially overlapping or touching pair collides at time 0. Treat floating-point comparisons within 1e-12 as equal.
spheres is a list of spheres represented as [x, y, z, vx, vy, vz, radius], where all values are real numbers. duration is a nonnegative real number. Return either a list [collision_time, i, j] or None.
Do not use frame-by-frame simulation. Sphere centers follow linear trajectories: position(t) = position(0) + velocity * t.
def earliest_sphere_collision(spheres, duration):