gpt4 book ai didi

r - gganimate 根据时间对多条路径进行动画处理

转载 作者:行者123 更新时间:2023-12-02 04:03:38 25 4
gpt4 key购买 nike

我已经解析了电子游戏《反恐精英》中手榴弹 throw 的一些数据。下面的示例数据显示我有关于手榴弹从哪里 throw 、手榴弹爆炸的位置以及何时 throw 手榴弹的位置。

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"))

使用 ggplot2 我可以绘制手榴弹的静态轨迹,如下所示 enter image description here

但我想对手榴弹轨迹进行动画处理,并按照 round_ throw_time 规定的顺序启动每个轨迹的动画,并从玩家位置移动到引爆位置。到目前为止我已经尝试过:

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

但是当谈到添加轨迹线以及如何重置动画而不是只是将点移回原点时,我有点迷失了。

任何提示将不胜感激!

我绘制的图像可以在这里下载 http://simpleradar.com/downloads/infernoV2.zip

最佳答案

首先,这太棒了。

第一种方法使用shadow_wake并且没有其他数据准备

我注释掉了问题中未定义的部分。我添加了 wrap = F 以使动画在最后重置(而不是倒带),并添加 shadow_wake 来捕获轨迹。

# 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

第二种方法,添加初始位置和geom_segment

如果我们为每个帧提供对玩家位置的引用,我们还可以将轨迹添加为一段:

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

第三种变体,按开始时间显示轨迹

与第二个类似,但我添加了每个轨迹的距离、行进时间和开始时间。我在这里假设爆炸是结束时间,并回溯到轨迹开始的时间。

(我第一次尝试了 transition_time,但在第一个轨迹之后无法让它正常工作而不会出现错误行为。)

# 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

关于r - gganimate 根据时间对多条路径进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52671755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com