Arduino to Cry Engine 3
Here's a simple example of how to connect Arduino to the (open source) Cry Engine 3 which is used for Crysis 2.
Arduino to Cry Engine 3 from baka on Vimeo.
Saw some guy on the crydev.net forum working on this and thought it was pretty interesting so I gave it a try.
Processing writes data from a potmeter (connected to Arduino Uno) to an .xml file every 50 miliseconds. Cry Engine then reads the .xml files and acts to whatever you've put in the flowgraph(s)
Assuming you already have some very basic knowledge with Arduino / processing / Cry Engine here's how to get it done:
- get the proxml library for processing from HERE and install it following the instructions
- get the processing library for Arduino HERE and install it following the instructions
- upload the standard firmata sketch to Arduino (File > Examples > Firmata > Standard Firmata)
- connect potmeter to Arduino ( analog port 0 if you want to use the same code as below)
- Save the following sketch in the game folder inside the Engine Map. Mind that the sketch saves the .xml file inside the “data” folder which is inside the sketch folder!
- import cc.arduino.*;
- import processing.serial.*;
- import proxml.*;
- Arduino arduino; // make Arduino object
- XMLInOut io = new XMLInOut(this); // new object "io" of datatype XMLInOut
- void setup(){
- size(200, 200);
-
- arduino = new Arduino(this, Arduino.list()[0], 57600); // find connected Arduino
- arduino.pinMode(0, Arduino.INPUT); // declare analog pin 0 as input
- }
- void draw(){
-
- proxml.XMLElement root = new proxml.XMLElement("root"); // create element (root)
- proxml.XMLElement king = new proxml.XMLElement("king"); // create element (king)
-
- float r = arduino.analogRead(0); // read data from pin 0
- int value = int(map(r, 0, 1024, 0, 24)); // translate data to int 0 - 24
-
- println(value); // print in processing for potmeter testing
-
- delay(50);
- root.addChild(king); // add child to "root element" in xml file
- king.addAttribute("size", value); // add attribute to king element (= child)
- io.saveElement(root, "xmltest.xml"); // saves object "io" to xml file on disk
- }
The XML file will look like this:
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <root>
- <king size=" *pot meter position* "/>
- </root>
The pot meter position will be a number somewhere between 0 and 24 since the Cry Engine usually works with a 24 hour system.
Now you'll want to create a flowgraph in Cry Engine which reads this .xml file and does something - in this case change the Time Of Day setting in-game. If you've never worked with the engine's flowgraph editor I suggest you follow some tutorials on YouTube (there's a ton of them) untill you understand the basics. Then you can simply build the following flowgraph (and of course edit it to your own needs)
You can place the sketch folder anywhere you like, just be sure to put in the right path on the OpenDocument node. This can be done by clicking on the node and editing its contents somewhere on the right (in my case the sketch folder is called “XML_WIN” – as you can see in the picture)
Main credits go to this guy at the crydev forums who posted the initial idea and code, I wanted to try it myself and show it to the processing community ;)