-
Notifications
You must be signed in to change notification settings - Fork 2
Tutorial ‐ Step 3
The aim here is to enable simple interactions from the VR headset. In particular, we want to be able to block roads by selecting them with the interaction ray. We'll then look at other types of interaction, with the example of using a button on one of the controllers to change the brightness.
First, we will modify the "Traffic and Pollution.gaml" file to integrate the notion of blocked roads. In the road species, we will add a boolean blocked attribute (initial value: false), and change the color of blocked road from white to red.
//Species to represent the roads
species road {
//Capacity of the road considering its perimeter
float capacity <- 1 + shape.perimeter / 30;
//Number of people on the road
int nb_people <- 0 update: length(people at_distance 1);
//Speed coefficient computed using the number of people on the road and the capicity of the road
float speed_coeff <- 1.0 update: exp(-nb_people / capacity) min: 0.1;
int buffer <- 10;
bool blocked <- false;
aspect default {
draw (shape + 5) color:blocked ? #red: #white;
}
}
We will then modify the reflex update_road_speed of the global section to take into account the fact that speed on a blocked road will be close to 0.
//Reflex to update the speed of the roads according to the weights
reflex update_road_speed {
road_weights <- road as_map (each::(each.shape.perimeter / each.speed_coeff * (each.blocked ? 100000.0 : 1.0)));
road_network <- road_network with_weights road_weights;
}
We're going to add an action to block a specific road (based on its name) in the Unity Linker species. In order to make a direct link between roads and their names, we've defined a map called "roads" that allows you to obtain the right road directly from a road name. This action will be called directly from Unity. If the route is not null, if the route is already blocked, the action will unblock it, and block it otherwise.
map<string,road> roads <- road as_map (each.name :: each);
action block_road(string id) {
road b <- roads[id];
if (b != nil) {
ask b {
if (not blocked) {
blocked <- true;
} else {
blocked <- false;
}
}
}
}