1

I am currently working on a school project where I'll be using motion capture and unity. It's made for the elderly to improve their cognitive and motoric functions. I want unity to be able to record their movements into a csv file to see how well they are doing. I want the x, y and z co-ordinates recorded against time in excel.

I'm using perception neuron for my motion capture which have a total of 32 sensors. The 3D model in unity has 32 different parts/limbs that move, including the fingers. I added a picture of it here:

This is what the 3D model for perception neuron looks like

I tried to use this example but it extracts data as a text file.

using UnityEngine;
using System.Collections;
using System.IO;
using System;

    public class fileMaker : MonoBehaviour
    {
        public static void putBytes(ref byte[] output, int index, float value)
        {
            //turns a float into its 4 bytes and then puts them into the output array
            //at the given index
            byte[] data = BitConverter.GetBytes(value);
            output[index] = data[0];
            output[index + 1] = data[1];
            output[index + 2] = data[2];
            output[index + 3] = data[3];
        }
        public static void makeFile(Vector3 position)
        {
            //each float is 4 bytes.
            //3 floats in a vector 3(x,y,z) and 3x4 =12!
            byte[] output = new byte[12];
            //get bytes for each part of our lil vector3
            putBytes(ref output, 0, position.x);
            putBytes(ref output, 4, position.y);
            putBytes(ref output, 8, position.z);
            File.WriteAllBytes(Application.dataPath + "/log.txt", output);
        }
        public static void loadFile()
        {
            //converts it all back into pretty print
            if (File.Exists(Application.dataPath + "/log.txt"))
            {
                byte[] input = File.ReadAllBytes(Application.dataPath + "/log.txt");
                int length = input.Length;
                if (length == 12)
                {
                    Vector3 ourVector3 = new Vector3();
                    ourVector3.x = (float)BitConverter.ToSingle(input, 0);
                    ourVector3.y = (float)BitConverter.ToSingle(input, 4);
                    ourVector3.z = (float)BitConverter.ToSingle(input, 8);
                    print("Position saved in file (" + Application.dataPath + "/log.txt): " + ourVector3.ToString());
                }

            }
        }
    }

I want unity to record position data for each part. Does this mean I have to write a script for each limb (which is a game object) or can I write one single script and connect to all the limbs?

I also want to know if I can change this code above to store data as a csv file instead of a text file; can I change a few lines or do I have to make a new script? I'm very new to unity and computer programming.

code11
  • 1,986
  • 5
  • 29
  • 37
pepefrog69
  • 27
  • 1
  • 7
  • It is really up to you how you store data for your application. There can be various considerations (speed, editability, etc.) that only you know. There is no good or bad solution, only those that meet your requirements and those that don't. – PepitoSh Jul 13 '18 at 05:23

1 Answers1

2

If all your limbs have the same behaviour you could declare an Interface. Something like:

public interface I3DPosition{
    string Get3DCoordinates();
}

and you link you limb object to this Interface

public LimbObject : I3DPosition

Maybe you could also do some inheritance. These are pretty Basic for object oriented programming. For writing the csv it should be really easy with:

File.WriteAllText(filePath, "x,y,z");

and/or

File.AppendAllText(filePath, "x,y,z");

see here

Kiroul
  • 465
  • 2
  • 9