Skip to content

Commit

Permalink
Merge pull request #1 from curds01/add_example
Browse files Browse the repository at this point in the history
Initial commit with initial project
  • Loading branch information
MengeCrowdSim authored Feb 21, 2017
2 parents fa8e030 + 6a40c0f commit 99bf5e8
Show file tree
Hide file tree
Showing 41 changed files with 369 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Local, custom ignores
log.css

## Github's default Unity ignores

/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
Expand Down
9 changes: 9 additions & 0 deletions Assets/Materials.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Materials/Pedestrian.mat
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Materials/Pedestrian.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Pedestrian.prefab
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Pedestrian.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Plugins.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Plugins/x86.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Plugins/x86/MengeCore.dll
Binary file not shown.
58 changes: 58 additions & 0 deletions Assets/Plugins/x86/MengeCore.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Plugins/x86_64.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Plugins/x86_64/MengeCore.dll
Binary file not shown.
58 changes: 58 additions & 0 deletions Assets/Plugins/x86_64/MengeCore.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Scripts/MengeCS.dll
Binary file not shown.
24 changes: 24 additions & 0 deletions Assets/Scripts/MengeCS.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions Assets/Scripts/SimController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using MengeCS;
using System;

public class SimController : MonoBehaviour {

public GameObject PedestrianModel;

private MengeCS.Simulator _sim;
private List<GameObject> _objects = new List<GameObject>();

private List<Color> classColors = new List<Color> () {
Color.red,
Color.blue,
Color.green,
Color.gray,
Color.magenta
};

// Use this for initialization
void Start () {
Debug.Log ("Starting simulation...");

string demo = "4square";
string mengeRoot = @"E:\work\projects\menge_fork\";
string behavior = String.Format(@"{0}examples\core\{1}\{1}B.xml", mengeRoot, demo);
string scene = String.Format(@"{0}examples\core\{1}\{1}S.xml", mengeRoot, demo);
Debug.Log ("\tInitialzing sim");
Debug.Log ("\t\tBehavior: " + behavior);
Debug.Log ("\t\tScene: " + scene);

_sim = new MengeCS.Simulator ();
bool result = _sim.Initialize (behavior, scene, "orca");

int COUNT = _sim.AgentCount;
Debug.Log (string.Format ("Simulator initialized with {0} agents", COUNT));
for (int i = 0; i < COUNT; ++i) {
MengeCS.Agent a = _sim.GetAgent (i);
UnityEngine.Vector3 pos = new UnityEngine.Vector3 (a.Position.X, a.Position.Y, a.Position.Z);
GameObject o = Instantiate (PedestrianModel, pos, Quaternion.identity) as GameObject;
if (o != null) {
o.transform.GetComponentInChildren<Renderer> ().material.color = classColors [a.Class % classColors.Count];
o.transform.GetChild(0).gameObject.transform.localScale = new UnityEngine.Vector3 (a.Radius * 2, 0.85f, a.Radius * 2);
_objects.Add (o);
}
}
}

// Update is called once per frame
void Update () {
_sim.DoStep ();
UnityEngine.Vector3 newPos = new UnityEngine.Vector3 ();
for (int i = 0; i < _sim.AgentCount; ++i) {
MengeCS.Vector3 pos3d = _sim.GetAgent (i).Position;
newPos.Set (pos3d.X, pos3d.Y, pos3d.Z);
_objects [i].transform.position = newPos;
}
}
}
15 changes: 15 additions & 0 deletions Assets/Scripts/SimController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/_Scenes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/_Scenes/scene_01.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/_Scenes/scene_01.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added ProjectSettings/AudioManager.asset
Binary file not shown.
Binary file added ProjectSettings/ClusterInputManager.asset
Binary file not shown.
Binary file added ProjectSettings/DynamicsManager.asset
Binary file not shown.
Binary file added ProjectSettings/EditorBuildSettings.asset
Binary file not shown.
Binary file added ProjectSettings/EditorSettings.asset
Binary file not shown.
Binary file added ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/InputManager.asset
Binary file not shown.
Binary file added ProjectSettings/NavMeshAreas.asset
Binary file not shown.
Binary file added ProjectSettings/NetworkManager.asset
Binary file not shown.
Binary file added ProjectSettings/Physics2DSettings.asset
Binary file not shown.
Binary file added ProjectSettings/ProjectSettings.asset
Binary file not shown.
2 changes: 2 additions & 0 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
m_EditorVersion: 5.4.3f1
m_StandardAssetsVersion: 0
Binary file added ProjectSettings/QualitySettings.asset
Binary file not shown.
Binary file added ProjectSettings/TagManager.asset
Binary file not shown.
Binary file added ProjectSettings/TimeManager.asset
Binary file not shown.
Binary file added ProjectSettings/UnityAdsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/UnityConnectSettings.asset
Binary file not shown.
70 changes: 68 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
# MengeUnity
An example for integrating Menge with the Unity game engine
# Menge Unity

This project serves as an example for integrating the Menge crowd simulation framework with the
Unity game engine.

## Running the Demo

Make sure you have already cloned the [main Menge repository](https://github.com/MengeCrowdSim/Menge).
This Unity project will reference simulation specifications in the examples directory.

These instructions assume the 64-bit version of the Unity Editor. If you're using the 32-bit
version, read the instructions below on 32/64-bit issues.

1. Clone this repository (or better yet, fork it so you can submit improvements back as a pull
request).
2. Start the Unity Editor.
3. Open a new project and select the root directory of your clone as the containing folder.
4. The Unity 3D view should show four blocks. These are the obstacles for the 4square example in
`$MengeRoot$\examples\core\4square`.
![Unity File Open](https://github.com/MengeCrowdSim/MengeUnity/blob/master/doc/images/4_blocks.PNG)
5. Open the `SimController` script for editing.
6. Edit line 27 so the `mengeRoot` variable points to the path on your system where the Menge
source is located.
7. Save `SimController.cs` and return to the editor.
8. Hit the `play` button.
![Unity File Open](https://github.com/MengeCrowdSim/MengeUnity/blob/master/doc/images/4_blocks_sim.PNG)

## Dependencies for Making Changes

The contents of this repository are sufficiently self-contained to run a demo (assuming you can
provide scenario specification files). However, it may be necessary for you make modifications on
what this simple example can do; changes that go beyond just Unity. The integration of Menge into
Unity is based on two things:

1. Menge itself (specifically, the `MengeCore.dll`). [Clone from here](https://github.com/MengeCrowdSim/Menge)
2. Menge C-Sharp Wrapper. [Clone from here](https://github.com/MengeCrowdSim/MengeCS)

Both of these projects will produce dlls for you to include in the Unity project. The `MengeCore.dll`
produced by the Menge project should go in `$MengeUnity$\assets\Plugins` (see note below for details
on 32/64-bit issues). The `MengeCS.dll` produced by the second project should go in the
`$MengeUnity$\assets\Scripts` folder.

## 32/64-bit issues

When using external dlls in Unity, it is important to make sure the dlls are built to the same
build environment as the application. For a 32-bit application, you need 32-bit dlls. For a 64-bit
application, you need a 64-bit dll. This _seems_ straightforward, but there is a wrinkle that might
catch you. The Unity Editor is one of the applications you develop and, on modern machines, it is
most likely a 64-bit application. So, even if your final application you're using Unity to produce
is a 32-bit application, to develop it you'll need both a 64-bit and 32-bit version of the
`MengeCore.dll`.

If you look in the `Plugins` folder, you'll note there are _two_ subfolders containing, apparently
identical files: `x86` and `x86_64` both contain copies of `MengeCore.dll`. As the directory names
suggeset, they are not actually identical. The former is a 32-bit dll and the latter is a 64-bit
dll. We provide both and then configure them _inside_ Unity so that it uses the right one in the
right context. For this to work, they _must_ be named the same.

If you make changes to Menge, you'll need to build Menge twice: one as 32-bit and one as 64-bit.
In order for the magic to work, the files must have identical names. This can lead to confusion and
requires care that the right build ends up in the right directory. If you end up putting the wrong
dll in the wrong directory, it will become immediately apparent -- the dll will fail to load.

## Contributing

Please feel free to contribute to this example. The first major step is to replace cylinders with
interesting pedestrians. We would really appreciate it if those better versed in Unity than we are
could help us flesh this out into a more fully-featured visualization of Menge simulations.
Binary file added doc/images/4_blocks.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/4_blocks_sim.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 99bf5e8

Please sign in to comment.