Custom API using UDP for a game on Unity  Topic is solved solved with post #22175

Here you can talk about creating profiles, discuss your profile settings and share problems with games and plugins.
Please note that you first have to read the the fitting game section in the manual!

Custom API using UDP for a game on Unity

Postby Rishya » Sun 21. Jan 2018, 22:57

Greetings,

I have a 2DOF simulation chair using 2 wiper motors and I want to make a game on Unity which movements will be simulated with X-Sim.

After making some searches on the forums, I found that I need to have (correct me if I'm wrong):
a- A plugin with UDP: I will be using the plugin of DiRT 2 and add a method to receive UDP packets.
b- Send Telemetry Data from the game via UDP

My questions are:
1/ Is there any other thing needed to make the whole thing work?
2/ What are the variables that I need to send via UDP from the game?

Any help would be greatly appreciated!
Rishya
 
Posts: 4
Joined: Sun 21. Jan 2018, 22:33
Has thanked: 0 time
Been thanked: 0 time

Re: Custom API using UDP for a game on Unity

Postby sirnoname » Mon 22. Jan 2018, 00:08

You are right because memory mapped files suffer with win10.
Download and rebuild the live for speed telemetry source code output (outgauge).
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: Custom API using UDP for a game on Unity

Postby Rishya » Mon 22. Jan 2018, 19:23

Thank you for your reply.
So I was able to send UDP packets from my game.

The problem is that "outsim" is not displaying the values sent and the "game paused" remains checked.
I made a UDP server to catch packets and checked that my game and Live for Speed are sending the same variables.
Some other tests on Wireshark show that the packets are properly being received by outsim.

I'm assuming that either:
- I can't use a ready plugin (Live for Speed plugin) for my game. In this case is there any other way?
- Or I'm missing some variables that must be sent in the UDP packet
The variables that I'm sending currently are:
Code: Select all
uintTime
sngAcceleration0
sngAcceleration1
sngAcceleration2
sngAVelocity0
sngAVelocity1
sngAVelocity2
sngVelocity0
sngVelocity1
sngVelocity2
Rishya
 
Posts: 4
Joined: Sun 21. Jan 2018, 22:33
Has thanked: 0 time
Been thanked: 0 time

Re: Custom API using UDP for a game on Unity

Postby sirnoname » Mon 22. Jan 2018, 23:25

Simple wire shark the life for speed output and compare to your one.
Game state (lap, pitstop etc. ...) are indicator for pause.
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: Custom API using UDP for a game on Unity  Topic is solved

Postby Rishya » Tue 23. Jan 2018, 00:25

I finally made it work so I'll share some stuff that may help in case someone gets the same problem.

First, the variables that need to be sent with Live for Speed Plugin are the following (I used a struct in C#, the order is important):
Code: Select all
public struct Vector
    {
        public Single X;
        public Single Y;
        public Single Z;
    }
    public struct Vec
    {
        public Int32 X;
        public Int32 Y;
        public Int32 Z;
    }
    public struct Si
    {
        public UInt32 Time;
        public Vector AngVel;
        public Single Heading;
        public Single Pitch;
        public Single Roll;
        public Vector Accel;
        public Vector Vel;
        public Vec Pos;
    }


I used QuplaySimTools (on github as reference).
For my case I simply edited the classes into structures since I'm using Marshalling for UDP packets. Here is the rest of the code to properly send a UDP packet with the struct above:
Code: Select all
    IPEndPoint remoteEndPoint;
    String IP = "127.0.0.1";
    int    port = 4123;
    UdpClient client;
byte[] getBytes(Si str)
    {
        int size = Marshal.SizeOf(str);
        byte[] arr = new byte[size];

        IntPtr ptr = Marshal.AllocHGlobal(size);
        Marshal.StructureToPtr(str, ptr, true);
        Marshal.Copy(ptr, arr, 0, size);
        Marshal.FreeHGlobal(ptr);
        return arr;
    }

    // sendData
    public  void sendString(Si sim)
    {
        try
        {
            client.Send(getBytes(sim), getBytes(sim).Length, remoteEndPoint);
        }
        catch (Exception err)
        {
            Console.WriteLine(err.ToString());
        }
    }
    // Use this for initialization
    void Start () {
        remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
        client = new UdpClient(); 
        InvokeRepeating("sendData", 0.2f, 0.001f);
    }

    uint c = 0;
   void sendData()
    {
        Si sim = new Si();

        //Velocity
        sim.Vel.X = 0; //get the velocity of your object for example
        sim.Vel.Y = 0;
        sim.Vel.Z = 0;

        //AngularVelocity
        sim.AngVel.X = 0;
        sim.AngVel.Y = 0;
        sim.AngVel.Z = 0;

        //Acceleration
        sim.Accel.X = 0;
        sim.Accel.Y = 0;
        sim.Accel.Z = 0;

        //Time
        c += 10;
        sim.Time = c;

        //Position
        sim.Pos.X = 0;
        sim.Pos.Y = 0;
        sim.Pos.Z = 0;

        //Singles
        sim.Heading = 0;
        sim.Roll = 0;
        sim.Pitch = 0;

        sendString(sim);
    }
   // Update is called once per frame
   void Update () {
      //No need to put the method in Update since we are using InvokeRepeating in Start()
   }


Now simply check "outsim" and you'll get your values.
Rishya
 
Posts: 4
Joined: Sun 21. Jan 2018, 22:33
Has thanked: 0 time
Been thanked: 0 time

Re: Custom API using UDP for a game on Unity

Postby anto1192 » Mon 28. May 2018, 19:21

Hi, I'm trying to use X Sim in a Unity game and I used your indications to send data to X Sim. What I want to know, if you already did, is how to compute the telemetry data like orientation, acceleration, angular velocity, velocity, position etc. needed for the communication. In particular I'm having troubles with the computation of orientation angles: roll, pitch and heading.
anto1192
 
Posts: 1
Joined: Mon 28. May 2018, 13:38
Has thanked: 0 time
Been thanked: 0 time


Return to Game profile & plugin discussions

Who is online

Users browsing this forum: No registered users and 1 guest