Question by
Jiman An · Jun 08, 2016 at 05:44 PM ·
rotationquaternioneuler angles
I can get Quaternion data(x,y,z,w) from simulation program. But, how to assign it in rotation?
I receive data(xPos, yPos, zPos, xQ, yQ, zQ, wQ) from other simulation program.
xPos, yPos, zPos are Position Data.
xQ, yQ, zQ, wQ are Rotation Data.
and I assign these data in Box Object. I insert position data into box. But, I have some troubles with rotation.
This is my code. Which part I have to modify?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
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;
// port
public int port = 8051; // define > init
// infos
public string lastReceivedUDPPacket = "";
float XPos;
float YPos;
float ZPos;
float XRot;
float YRot;
float ZRot;
Quaternion a;
public Transform box;
//game object
//public GameObject tmp;
//GameObject tmp = GameObject.Find("Cube");
private static void Main()
{
UDPReceive receiveObj = new UDPReceive();
receiveObj.init();
string text = "";
do
{
text = Console.ReadLine();
}
while (!text.Equals("exit"));
}
public void Start()
{
init();
}
void Update()
{
parseDataToBox(lastReceivedUDPPacket);
//GameObject tmp = GameObject.Find("Cube");
box.transform.position = box.position;
box.transform.rotation = Quaternion a;
//box.transform.eulerAngles = box.eulerAngles;
}
void OnGUI()
{
Rect rectObj = new Rect(40, 10, 200, 400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj, "\nLast Packet: \n" + lastReceivedUDPPacket, style);
}
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 + "");
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// Unity Application Quit Function
void OnApplicationQuit()
{
stopThread();
}
// Stop reading UDP messages
private void stopThread()
{
if (receiveThread.IsAlive)
{
receiveThread.Abort();
}
client.Close();
}
// 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);
// Data Received from simulation
print("Received Data >> " + text);
// latest UDPpacket
lastReceivedUDPPacket = text;
}
catch (Exception err)
{
print(err.ToString());
}
}
}
private void parseDataToBox(string text)
{
try
{
string[] AgxData = text.Split(';');
//float[] floatData = Array.ConvertAll(text.Split(';'), float.Parse);
float[] UnityData = new float[7];
//float[] RotData = new float[3];
UnityData[0] = float.Parse(AgxData[0]); //x Position
UnityData[1] = float.Parse(AgxData[1]); //y Position
UnityData[2] = float.Parse(AgxData[2]); //z Position
UnityData[3] = float.Parse(AgxData[3]); //x Quaternion
UnityData[4] = float.Parse(AgxData[4]); //y Quaternion
UnityData[5] = float.Parse(AgxData[5]); //z Quaternion
UnityData[6] = float.Parse(AgxData[6]); //w Quaternion
box.position = new Vector3(UnityData[0], UnityData[1], UnityData[2]);
Quaternion quaternion = (UnityData[3], UnityData[4], UnityData[5], UnityData[6]);
//box.eulerAngles = new Vector3(UnityData[3], UnityData[4], UnityData[5]);
}
catch (Exception err)
{
print(err.ToString());
}
}
}
Comment
Answer by FortisVenaliter · Jun 08, 2016 at 05:52 PM
Your code looks correct, you just don't set the rotation, such as:
box.rotation = quaternion;