// Update is called once per frame void Update() { // create the color for the square new_color = new Color(color_factor, color_factor, color_factor, 1f); // put it on the square tracking_square.GetComponent <Renderer>().material.SetColor("_Color", new_color); // Define the color for the next iteration (switch it) if (color_factor > 0.0f) { color_factor = 0.0f; } else { color_factor = 1.0f; } // Get the status of the rigid body OptitrackRigidBodyState rbState = StreamingClient.GetLatestRigidBodyState(RigidBodyId); if (rbState != null) { // get the position of the mouse RB Mouse_Position = rbState.Pose.Position; // change the transform position of the game object this.transform.localPosition = Mouse_Position; // also change its rotation this.transform.localRotation = rbState.Pose.Orientation; // turn the angles into Euler (for later printing) Mouse_Orientation = this.transform.eulerAngles; // get the timestamp Time_stamp = rbState.DeliveryTimestamp.SecondsSince(reference); } // get the position of single trackable points in the arena, used for labeled real crickets List <OptitrackMarkerState> markerStates = StreamingClient.GetLatestMarkerStates(); // for each detected marker foreach (OptitrackMarkerState marker in markerStates) { // if it's not part of a rigid body, save the position as cricket if (marker.Labeled == false) { Cricket = marker.Position; } else { // if not, ignore it Cricket = new Vector3(10.0f, 10.0f, 10.0f); } } // Write the line of data writer.WriteLine(string.Concat(Time_stamp.ToString(), ',', Mouse_Position.x.ToString(), ',', Mouse_Position.y.ToString(), ',', Mouse_Position.z.ToString(), ',', Mouse_Orientation.x.ToString(), ',', Mouse_Orientation.y.ToString(), ',', Mouse_Orientation.z.ToString(), ',', Cricket.x.ToString(), ',', Cricket.y.ToString(), ',', Cricket.z.ToString(), ',', color_factor.ToString())); }
// Update is called once per frame void Update() { List <OptitrackMarkerState> markerStates = streamingClient.GetLatestMarkerStates(); foreach (OptitrackMarkerState markerState in markerStates) { String log = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff"), logIndex, markerState.Id, markerState.Position.x, markerState.Position.y, markerState.Position.z); PrintTextToFile(log); } }
// Update is called once per frame void Update() { List <OptitrackMarkerState> markerStates = StreamingClient.GetLatestMarkerStates(); foreach (OptitrackMarkerState marker in markerStates) { if (marker.Labeled == false) { // Debug.Log (marker.Position); this.transform.localPosition = marker.Position; } else { this.transform.localPosition = new Vector3(10.0f, 10.0f, 10.0f); } } }
/* * Method to update the position of the cue rigidbody every by reading the data from the Optitrack client */ void UpdatePose() { #region Cue State Variables OptitrackRigidBodyState rbState = StreamingClient.GetLatestRigidBodyState(RigidBodyId); //access rigidbody Vector3 OfrontPos = new Vector3(); //optitrack front position Vector3 ObackPos = new Vector3(); //optitrack back position List <OptitrackMarkerState> markerStates = new List <OptitrackMarkerState>(); //initialize list of marker objects from rigidbody List <Vector3> markerPositions = new List <Vector3>(); //list of all cue marker positions #endregion if (rbState != null) { //Get marker positions markerStates = StreamingClient.GetLatestMarkerStates(); //get objects of all markers from rigidbody #region Check if valid Optitrack markers //if not all markers are tracked, make cue stick invisible if (markerStates.Count < 4) { Experiment.cuestickMesh.enabled = false; //Debug.Log(markerStates.Count); cuePos = prevPos; return; } Experiment.cuestickMesh.enabled = true; #endregion #region Access & store marker poitions //Loop through markers for (int idx = 0; idx < markerStates.Count; idx++) { OptitrackMarkerState marker = markerStates[idx]; int markerID = marker.Id; Vector3 pos = marker.Position; markerPositions.Add(marker.Position); } #endregion #region Derive Unity cue position IEnumerable <Vector3> sorted = markerPositions.OrderBy(v => v.z); //sort marker positions by z position OfrontPos = (sorted.ElementAt(3) + sorted.ElementAt(2)) / 2; Vector3 dif = OfrontPos - sorted.ElementAt(1); ObackPos = sorted.ElementAt(0) + dif; //Translate optitrack positions to unity using helper methods ( [Xo, Zo, 1] * M = [Xu, Zu, 1]) float[,] frontMatrix = new float[, ] { { OfrontPos.x }, { OfrontPos.z }, { 1 } }; float[,] backMatrix = new float[, ] { { ObackPos.x }, { ObackPos.z }, { 1 } }; float[,] frontU = Experiment.MultiplyMatrix(Experiment.M, frontMatrix); float[,] backU = Experiment.MultiplyMatrix(Experiment.M, backMatrix); Vector3 backPos = new Vector3(backU[0, 0], ObackPos.y * Experiment.yratio, backU[1, 0]); Vector3 frontPos = new Vector3(frontU[0, 0], OfrontPos.y * Experiment.yratio, frontU[1, 0]); //cue position is front position plus projected vector Vector3 direction = (frontPos - backPos).normalized; cuePos = frontPos + (direction * PlayerPrefs.GetFloat("tip_marker1") * PlayerPrefs.GetFloat("cmToUnity")); //limit cuetip height to table surface height if (cuePos.y < Experiment.corner1.position.y) { cuePos = new Vector3(cuePos.x, Experiment.corner1.position.y, cuePos.z); } #endregion #region Calculate cue velocity //calculate velocity cueVelocity = (cuePos - prevPos) / Time.fixedDeltaTime; if (cueVelocity.magnitude > 3) { cueVelocity = 3f * cueVelocity.normalized; } VelocityList.Add(cueVelocity); //avgVelocity = AverageVelocity(VelocityList, Experiment.numVelocitiesAverage); prevPos = cuePos; #endregion //move cue rigidbody to updated position Experiment.cueFront.transform.position = cuePos; //get forward direction by lookng at forwrd position from actual optitrack position (could also do with transformed positions) Experiment.cueFront.transform.rotation = Quaternion.LookRotation(frontPos - backPos) * Quaternion.Euler(0f, 0f, 0f); } }