I get the data(x,y,z position and x,y,z rotation) from realtime simulation.
It is communicated by UDP/IP.
I used the code by la1n.(simple udp implementation (send/read via mono/c#) - Unity Engine - Unity Discussions)
/*
source : http://forum.unity3d.com/threads/simple-udp-implementation-send-read-via-mono-c.15900/
-----------------------
UDP-Receive (send to)
-----------------------
// [url]http://msdn.microsoft.com/de-de/library/bb979228.aspx#ID0E3BAC[/url]
// > receive
// 127.0.0.1 : 8051
// send
// nc -u 127.0.0.1 8051
*/
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPReceive : MonoBehaviour
{
// receiving Thread
Thread receiveThread;
// udpclient object
UdpClient client;
// public
// public string IP = "127.0.0.1"; default local
public int port; // define > init
// infos
public string lastReceivedUDPPacket = "";
public string allReceivedUDPPackets = ""; // clean up this from time to time!
// start from shell
private static void Main()
{
UDPReceive receiveObj = new UDPReceive();
receiveObj.init();
string text = "";
do
{
text = Console.ReadLine();
}
while (!text.Equals("exit"));
}
// start from unity3d
public void Start()
{
init();
}
// OnGUI
void OnGUI()
{
Rect rectObj = new Rect(40, 10, 200, 400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj, "# UDPReceive\n127.0.0.1 " + port + " #\n"
+ "shell> nc -u 127.0.0.1 : " + port + " \n"
+ "\nLast Packet: \n" + lastReceivedUDPPacket
+ "\n\nAll Messages: \n" + allReceivedUDPPackets
, style);
}
// init
private void init()
{
// Endpunkt definieren, von dem die Nachrichten gesendet werden.
print("UDPSend.init()");
// define port
port = 8051;
// status
print("Sending to 127.0.0.1 : " + port);
print("Test-Sending to this Port: nc -u 127.0.0.1 " + port + "");
// ----------------------------
// Abhören
// ----------------------------
// Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
// Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// receive thread
private void ReceiveData()
{
client = new UdpClient(port);
while (true)
{
try
{
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
// Bytes mit der UTF8-Kodierung in das Textformat kodieren.
string text = Encoding.UTF8.GetString(data);
// Den abgerufenen Text anzeigen.
print(">> " + text);
// latest UDPpacket
lastReceivedUDPPacket = text;
// ....
allReceivedUDPPackets = allReceivedUDPPackets + text;
}
catch (Exception err)
{
print(err.ToString());
}
}
}
// getLatestUDPPacket
// cleans up the rest
public string getLatestUDPPacket()
{
allReceivedUDPPackets = "";
return lastReceivedUDPPacket;
}
}
This code is simulation code and It is written by Lua Script.
socket = require("socket")
print(socket._VERSION)
function dataListener:post( t )
local XPos = ship.rb:getPosition():x()
local YPos = ship.rb:getPosition():y()
local ZPos = ship.rb:getPosition():z()
local XXPos = math.floor( XPos * 1000 + 0.5 ) / 1000
local YYPos = math.floor( YPos * 1000 + 0.5 ) / 1000
local ZZPos = math.floor( ZPos * 1000 + 0.5 ) / 1000
local XRot = ship.rb:getRotation():x()
local YRot = ship.rb:getRotation():y()
local ZRot = ship.rb:getRotation():z()
local XXRot = math.floor( XPos * 1000 + 0.5 ) / 1000
local YYRot = math.floor( YPos * 1000 + 0.5 ) / 1000
local ZZRot = math.floor( ZPos * 1000 + 0.5 ) / 1000
udp=socket.udp();
udp:setpeername("127.0.0.1",8051)
udp:send(string.format("%f; %f; %f; %f; %f; %f", XXPos, YYPos, ZZPos, XXRot, YYRot, ZZRot));
end
I have worked for 3 hours. But Whenever I debug, Errors occur and It doesn’t work…