Packed particle simulation
Save as particles.atom. All particles have the same nested data layout. A loop binding is a live record view.
Advance the particles by one time step
Each p is a view into the packed array. Updating its coordinates or lifetime changes the particle in place.
dt = 1.0 / 60.0
for frame in 0..60:
for p in particles:
p.position.x += dt * p.velocity.x
p.position.y += dt * p.velocity.y
p.life -= dt
Show full source 22 lines
use binary
vector = binary.layout({x = f32, y = f32})
particle = binary.layout({position = vector, velocity = vector, life = f32})
particles = create(particle, 256)
for index, p in particles:
p.position.x = index
p.position.y = 0
p.velocity.x = 1.0
p.velocity.y = 2.0
p.life = 1.0
dt = 1.0 / 60.0
for frame in 0..60:
for p in particles:
p.position.x += dt * p.velocity.x
p.position.y += dt * p.velocity.y
p.life -= dt
assert(particles[0].position.x > 0.99 and particles[0].position.x < 1.01)
print(count(particles), binary.stride(particle), binary.size(particles))