Untitled

mail@pastecode.io avatar
unknown
r
a year ago
1.2 kB
1
Indexable
Never
MIN_VELOCITY <- 5 
MAX_VELOCITY <- 15
DAMPING <- 0.90
GRAVITY <- 5

particles <- list()

create_particle <- function(x, y){
  list(
    x = x,
    y = y,
    angle = runif(1, 0, 2*pi),
    vel = runif(1, MIN_VELOCITY, MAX_VELOCITY)
  )
}

explosion <- function(x,y){
    particles <- list()
    for (i in 1:50){
      particles[[i]] <- create_particle(x,y)
    }
    return(particles)
}

update_particles <- function(particles){
  new_particles <- list()
  for (i in 1:length(particles)){
    p <- particles[[i]]
    dx <- cos(p$angle) * p$vel
    dy <- sin(p$angle) * p$vel
    p$x <- p$x + dx
    p$y <- p$y + dy - GRAVITY
    p$vel <- p$vel * DAMPING
    new_particles[[i]] <- p
  }
  return(new_particles)
}

draw_particles <- function(particles){
  xs <- c()
  ys <- c()
  for (p in particles){
    xs <- c(xs, p$x)
    ys <- c(ys, p$y)
  }
  plot(xs, 
       ys, 
       col = "white",
       pch = 20,
       ylim = c(-250, 750),
       xlim = c(-250, 750)
  )
}

par(bg = "black")
while (T){
  particles <- explosion(runif(1, 0, 500), runif(1, 0, 500))
  for (i in 1:100){
    draw_particles(particles)
    particles <- update_particles(particles)
    Sys.sleep(0.05)
  }
}