Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
fixes player location
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaillandier committed Jul 12, 2024
1 parent 62e6f46 commit 8b9baf9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,72 +1,85 @@
/**
* Name: SendStaticdata
* Show how to send dynamic geometries/agents to Unity. It works with the Scene "Assets/Scenes/Code Example/Receive Dynamic Data" from the Unity Template
* Name: SendDEM
* Show how to send a grid/matrix to Unity and modify it on the fly. It works with the Scene "Assets/Scenes/Code Example/Receive DEM Data" from the Unity Template.
* In this model, a DEM is loaded from GAMA as soon as a player logs on. The player, who is in the centre of a crater at the start of the game, can then move around
* this DEM and catch/throw a ball whose physics are managed by Unity. From GAMA, you can dynamically increase the height of a cell by pressing 'R'.
* The cell whose height is increased will be the one where the mouse is located. Similarly, you can decrease the height of a cell by pressing 'T'.
* The player in the VR headset will automatically see the DEM changed in the game.
*
* Author: Patrick Taillandier
* Tags: Unity, dynamic geometries/agents
* Tags: Unity, DEM, grid, field, Terrain
*/


model SendDEM

global {

//the dem used for this model
grid_file mnt_grid_file <- grid_file("../../includes/dem.asc");

geometry shape <- envelope(mnt_grid_file);

unity_property up_sphere ;

//propoerty used for the ball
unity_property up_sphere ;

//height increased/drecreased with the R/T events
float added_height <- 2.0 #m;

//max height for the ground
float max_value <- 100.0;


init {
//create a sphere agent (ball)
create sphere_ag with:(location:{20,20,70});
}


//action to increase/decrease the height of a cell
action change_height(bool increase) {
cell c <- cell(#user_location) ;
if (c != nil) {
//increase/decrease the height of the cell located at user_location (mouse pointer)
ask c {

grid_value <-grid_value + (increase ? added_height : -added_height);
grid_value <- max(0, min(max_value,grid_value));
write sample(grid_value);
}

//ask the unity linker to send the modification of the dem to Unity
ask unity_linker {
do set_terrain_values(
player:last(unity_player),
id:"dem",
matrix: {1,1} matrix_with c.grid_value,
index_x : c.grid_x,
index_y : c.grid_y
player:last(unity_player), //player concerned
id:"dem", //name of the Terrain in Unity
matrix: {1,1} matrix_with c.grid_value, //matrix containing the new values - in this example only the height of one cell (c) is modified
index_x : c.grid_x, //index x (column) of the matrix in the total grid
index_y : c.grid_y //index y (row) of the matrix in the total grid
);
}
}
}



}

//sphere agent represented by a sphere in GAMA
species sphere_ag {
aspect default {
draw sphere(1) color: #magenta;
}
}

//grid initiliazed by the mnt_grid_file
grid cell file: mnt_grid_file ;

//Species that will make the link between GAMA and Unity. It has to inherit from the built-in species asbtract_unity_linker
species unity_linker parent: abstract_unity_linker {
//name of the species used to represent a Unity player
string player_species <- string(unity_player);

//in this model, the agents location and heading will be sent to the Players at every step, so we set do_info_world to true
//in this model, the agents location and heading will not be sent to the Players at every step, so we set do_info_world to false
bool do_send_world <- false;

//initial location of the player
//initial location of the player - center of the world
list<point> init_locations <- [world.location];


Expand All @@ -75,34 +88,27 @@ species unity_linker parent: abstract_unity_linker {
//define the unity properties
do define_properties;

//add the static_geometry agents as static agents/geometries to send to unity with the up_geom unity properties.
//add the sphere_ag agent as static geometry to send to unity with the up_sphere unity properties.
do add_background_geometries(sphere_ag,up_sphere);

}


//action that defines the different unity properties
action define_properties {
//define a unity_aspect called tree_aspect that will display in Unity the agents with the SM_arbres_001 prefab, with a scale of 2.0, no y-offset,
//define a unity_aspect called sphere_aspect that will display in Unity the agents with the SphereRigidBody prefab, with a scale of 1.0, no y-offset,
//a rotation coefficient of 1.0 (no change of rotation from the prefab), no rotation offset, and we use the default precision.
unity_aspect sphere_aspect <- prefab_aspect("Prefabs/Visual Prefabs/Basic shape/SphereRigidBody",1.0,0.0,0.0,0.0, precision);
unity_aspect sphere_aspect <- prefab_aspect("Prefabs/Visual Prefabs/Basic shape/SphereRigidBody",1.0,0.0,1.0,0.0, precision);

//define the up_car unity property, with the name "car", no specific layer, the car_aspect unity aspect, no interaction, and the agents location are not sent back
//define the up_sphere unity property, with the name "sphere_ag", no specific layer, the sphere_aspect unity aspect, grabable, and the agent location is sent back
//to GAMA.
up_sphere<- geometry_properties("sphere_ag", nil, sphere_aspect, #grabable, true);

// add the up_tree unity_property to the list of unity_properties
// add the up_sphere unity_property to the list of unity_properties
unity_properties << up_sphere;


}
reflex change_mnt {
ask cell {
//grid_value <- grid_value * 0.95;
}
}


}

//species used to represent an unity player, with the default attributes. It has to inherit from the built-in species asbtract_unity_player
Expand All @@ -125,15 +131,17 @@ species unity_player parent: abstract_unity_player {
//display the player
bool to_display <- true;

//offset added to the player vizualisation.
float z_offset <- 10.0;

//default aspect to display the player as a circle with its cone of vision
aspect default {
if to_display {
if selected {
draw circle(player_size) at: location + {0, 0, 4.9} color: rgb(#blue, 0.5);
draw circle(player_size) at: location + {0, 0, z_offset} color: rgb(#blue, 0.5);
}
draw circle(player_size/2.0) at: location + {0, 0, 5} color: color ;
draw player_perception_cone() color: rgb(color, 0.5);
draw circle(player_size/2.0) color: color at: location + {0, 0,z_offset} ;
draw player_perception_cone() color: rgb(color, 0.5) ;
}
}
}
Expand All @@ -160,19 +168,19 @@ experiment vr_xp parent:main autorun: false type: unity {




//action called by the middleware when a player connects to the simulation
action create_player(string id) {
field f <- field(matrix(cell));
ask unity_linker {
do create_player(id);

//after creating the player, GAMA sends to the player the initial value of the DEM
do update_terrain (
player:last(unity_player),
id:"dem",
field:f,
resolution:65,
max_value:max_value
player:last(unity_player), //player concerned
id:"dem", //name of the Terrain in Unity
field:f, //it is possible to send the grid either as a field or as a matrix
resolution:65, //resolution of the target Terrain in Unity. Ideally, the resolution of the field/matrix should be the same as this one
max_value:max_value //optional : max possible of the grid - if not defined, GAMA will set it with the max value in the field/matrix
);
}

Expand All @@ -192,14 +200,19 @@ experiment vr_xp parent:main autorun: false type: unity {


output {
//In addition to the layers in the map display, display the unity_player and let the possibility to the user to move players by clicking on it.
//In addition to the layers in the map display, display the unity_player .
display displayVR parent: map {
species unity_player;

//increase the height of a cell when cliking on "R"
event "r" {
ask world {
do change_height(true);
}
}


//decrease the height of a cell when cliking on "T"
event "t" {
ask world {
do change_height(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ public String playerStr() {
modelPlayer.append("\tfloat cone_amplitude <- 90.0;\n");
modelPlayer.append("\tfloat player_rotation <- 90.0;\n");
modelPlayer.append("\tbool to_display <- true;\n");
modelPlayer.append("\tfloat z_offset <- 2.0;\n");

modelPlayer.append("\taspect default {\n");
modelPlayer.append("\t\tif to_display {\n");
modelPlayer.append("\t\t\tif selected {\n");
modelPlayer.append("\t\t\t\t draw circle(player_size) at: location + {0, 0, 4.9} color: rgb(#blue, 0.5);\n");
modelPlayer.append("\t\t\t\t draw circle(player_size) at: location + {0, 0, z_offset} color: rgb(#blue, 0.5);\n");

modelPlayer.append("\t\t\t}\n");

modelPlayer.append("\t\t\tdraw circle(player_size/2.0) at: location + {0, 0, 5} color: color ;\n");
modelPlayer.append("\t\t\tdraw circle(player_size/2.0) at: location + {0, 0, z_offset} color: color ;\n");
modelPlayer.append("\t\t\tdraw player_perception_cone() color: rgb(color, 0.5);");
modelPlayer.append("\n\t\t}");
modelPlayer.append("\n\t}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
doc = { @doc ("display or not the agent in GAMA")}),
@variable(name = AbstractUnityPlayer.SELECTED, type = IType.BOOL,
doc = { @doc ("is the agent selected")}),
@variable(name = AbstractUnityPlayer.ZOFFSET, type = IType.FLOAT,init = "2.0",
doc = { @doc ("offset along the z-axis for the the display of the agent in GAMA")}),

@variable(name = AbstractUnityPlayer.CONE_DISTANCE, type = IType.FLOAT,
doc = { @doc ("distance of the cone for the display of the agent in GAMA")}),
@variable(name = AbstractUnityPlayer.CONE_AMPLITUDE, type = IType.FLOAT,
Expand All @@ -68,6 +71,7 @@ public class AbstractUnityPlayer extends GamlAgent{
public static final String PLAYER_AGENTS_MIN_DIST = "player_agents_min_dist";
public static final String PLAYER_SIZE = "player_size";
public static final String PLAYER_ROTATION = "player_rotation";
public static final String ZOFFSET = "z_offset";


public AbstractUnityPlayer(IPopulation<? extends IAgent> s, int index) {
Expand Down Expand Up @@ -108,6 +112,15 @@ public static void setColor(final IAgent agent, final GamaColor val) {
agent.setAttribute(IKeyword.COLOR, val);
}

@getter (ZOFFSET)
public static Double getZOffset(final IAgent agent) {
return (Double) agent.getAttribute(ZOFFSET);
}
@setter(ZOFFSET)
public static void setZOffset(final IAgent agent, final Double val) {
agent.setAttribute(ZOFFSET, val);
}

@getter (CONE_DISTANCE)
public static Double getConeDistance(final IAgent agent) {
return (Double) agent.getAttribute(CONE_DISTANCE);
Expand Down Expand Up @@ -175,7 +188,7 @@ private static IShape getCone(IScope scope, IAgent agent) {
Double cone_amplitude = getConeAmplitude(agent);
IShape g = SpatialCreation.cone(scope, (int)(rotation - cone_amplitude/2),(int)(rotation + cone_amplitude/2));
g = SpatialOperators.inter(scope, g, SpatialCreation.circle(scope, getConeDistance(agent)));
g = SpatialTransformations.translated_by(scope, g, new GamaPoint(0,0,4.9));
g = SpatialTransformations.translated_by(scope, g, new GamaPoint(0,0,getZOffset(agent)));
return g;

}
Expand Down

0 comments on commit 8b9baf9

Please sign in to comment.