4

I have parsed some data on grenade throws from the videogame Counter Strike. The sample data beloew reveals that I have positions on where the grenade is thrown from and where the grenade detonates and when the grenade is thrown.

df <- data.frame(pos_x = c(443.6699994744587,459.4566921116250, 443.5131582404877, 565.8823313012402, 725.3048665125078, 437.3428992800084, 475.7286794460795, 591.4138769182258),
             pos_y = c(595.8564633895517, 469.8560006170301, 558.8543552036199, 390.5840189222542, 674.7983854380914, 688.0909476552858, 468.4987145207733, 264.6016042780749), 
             plot_group = c(1, 1, 2, 2, 3, 3, 4, 4),
             round_throw_time = c(31.734375, 31.734375, 24.843750, 24.843750, 35.281250, 35.281250, 30.437500, 30.437500), 
             pos_type = c("Player position", "HE detonate", "Player position", "HE detonate", "Player position", "HE detonate", "Player position", "HE detonate"))

And using ggplot2 I can plot the static trajectories of the grenades like shown here enter image description here

But I would like to animate the grenade trajectories and iniate the animation of each trajectory in the order that round_throw_time prescribes it and moving from player position to detonate position. So far I have attempted this:

ggplot(df, aes(pos_x, pos_y, group = plot_group)) +
 annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height = 
 unit(1,"npc")), 0, w, 0, -h) + 
 scale_x_continuous(expand = c(0,0),limits = c(0,w)) + 
 scale_y_reverse(expand = c(0,0),limits = c(h,0)) + 
 geom_point(color = "red") +
 transition_states(states=pos_type, transition_length = 1, state_length = 1)

enter image description here

But I'm kinda lost when it comes to adding the trajectory lines and how to reset the animation instead of the point just moving back to their origin.

Any tips would be greatly appreciated!

The image I plot onto can be downloaded here http://simpleradar.com/downloads/infernoV2.zip

Bønding
  • 77
  • 6

1 Answers1

6

First of all, this is awesome.

First approach using shadow_wake and no other data prep

I commented out the pieces that weren't defined in the question. I added wrap = F to make the animation reset (rather than rewind) at the end, and shadow_wake to capture the trajectory.

# The factors of pos_type are backwards (b/c alphabetical), so R thinks the detonation 
#   comes before the player position. Here we reverse that.
df$pos_type <- forcats::fct_rev(df$pos_type)

ggplot(df, aes(pos_x, pos_y, group = plot_group)) +
  # annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height = 
  #                                      unit(1,"npc")), 0, w, 0, -h) + 
  scale_x_continuous(expand = c(0,0)) + # ,limits = c(0,w)) +
  scale_y_reverse(expand = c(0,0)) + # ,limits = c(h,0)) +
  geom_point(color = "red") +
  transition_states(states=pos_type, transition_length = 1, state_length = 1, wrap = F) +
  shadow_wake(wake_length = 1)

enter image description here

Second approach, adding initial position and geom_segment

We could also add the trajectory as a segment, if we give each frame a reference to the player position:

df %>%
  # Add reference to first coordinates for each plot_group
  left_join(by = "plot_group",
    df %>% 
      group_by(plot_group) %>%
      filter(pos_type == "Player position") %>%
      mutate(pos_x1 = pos_x, pos_y1 = pos_y) %>%
      select(plot_group, pos_x1, pos_y1)
  ) %>%
ggplot(aes(pos_x, pos_y, group = plot_group)) +
  # annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height = 
  #                                      unit(1,"npc")), 0, w, 0, -h) + 
  scale_x_continuous(expand = c(0,0)) + # ,limits = c(0,w)) +
  scale_y_reverse(expand = c(0,0)) + # ,limits = c(h,0)) +
  geom_point(color = "red") +
  geom_segment(color = "gray70", aes(xend = pos_x1, yend  = pos_y1)) +
  transition_states(states=pos_type, transition_length = 1, state_length = 1, wrap = F)

enter image description here

Third variation, showing trajectories by start time

Similar to 2nd, but I add each trajectory's distance, travel time, and start time. I assume here that the detonation is the end time, and work back to when the trajectory started.

(I first tried transition_time, but couldn't get it to work without buggy behavior after the first trajectory.)

# trajectory speed
dist_per_time = 50

df2 <- df %>%
  # Add reference to first coordinates for each plot_group
  left_join(by = "plot_group",
            df %>% 
              group_by(plot_group) %>%
              filter(pos_type == "Player position") %>%
              mutate(pos_x1 = pos_x, pos_y1 = pos_y) %>%
              select(plot_group, pos_x1, pos_y1)
  ) %>%
  left_join(by = c("plot_group", "pos_type"),
    df %>%
      group_by(plot_group) %>%
      mutate(x_d = (range(pos_x)[1] - range(pos_x)[2]),
             y_d = (range(pos_y)[1] - range(pos_y)[2]),
             dist = sqrt(x_d^2 + y_d^2),
             event_time = round_throw_time - if_else(pos_type == "Player position", 
                                                     dist / dist_per_time, 
                                                     0),
             event_time = round(event_time, 1)) %>%
      select(plot_group, pos_type, dist, event_time)
  ) %>%

  ### EDIT - added below to make timing explicit and fix code which 
  #          was broken in current version of gganimate @ 2019-11-15
  #          Thanks @Deep North for tip.
  group_by(plot_group) %>%
  mutate(event_time_per_grp = event_time - first(event_time)) %>%
  ungroup() %>%
  mutate(event_time_cuml = cumsum(event_time))


  ggplot(df2, aes(pos_x, pos_y, group = plot_group)) +
  # annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height = 
  #                                      unit(1,"npc")), 0, w, 0, -h) + 
  scale_x_continuous(expand = c(0,0)) + # ,limits = c(0,w)) +
  scale_y_reverse(expand = c(0,0)) + # ,limits = c(h,0)) +
  geom_point(color = "red") +
  geom_segment(color = "gray70", aes(xend = pos_x1, yend  = pos_y1)) +
  transition_reveal(event_time_cuml)  ### EDIT, see above

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • This is awesome, Jon! The last approach is exactly what I had in mind, but do you have any ideas for how to start each animation one at a time based on round_throw_time? Instead of all at once like now? – Bønding Oct 05 '18 at 20:09
  • I've added an addendum to my answer that is closer to that. I thought this should be done with `transition_time` but I couldn't get it to show the 2nd, 3rd, or 4th trajectory. So I used transition_reveal. In any case, one component you'll need to add is an assumption around start time. I added that by assuming a speed and working back in time based on the distance traveled. – Jon Spring Oct 05 '18 at 21:58
  • Knocked it out of the park! ;-) Thank you so much! – Bønding Oct 06 '18 at 07:12
  • The third method does not work anymore ,can you please have a check? – Deep North Nov 15 '19 at 08:39