Click here to Skip to main content
16,012,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello !

I need to indicate in void FixedUpdate that I want only to use 1 frame / 20 (for the animation and the recording "writer").

Here what I try to do :

C#
void FixedUpdate() {
		
		delta_time += Time.deltaTime; 

		if (frame < nv_data [0].positions.Length) { 

						if ((frame % 20) == 0) {

								for (int k = 0; k < body.Length; ++k) {
				
								if (body [k] != null) { 

												body [k].transform.localPosition = nv_data [k].positions [frame] / 1000;

										} else
												continue;
								}
Debug.Log(frame); // Here I see what I want : 1 frame / 20 and the animation runs
												
								if (frame < forces_dup.Length) { 

										if (frame >= 1) {
						Debug.Log (frame); // I show only 20 then nothing, as though frame doesn't increment

												float delta_x = (((nv_data [59].positions [frame] - nv_data [59].positions [frame - 1]).z) * 0.001f);
												vitesse = delta_x / Time.deltaTime;
												acceleration = (vitesse - ancienne_vitesse) / Time.deltaTime;
												ancienne_vitesse = vitesse;

												indice_3 ++;

												StreamWriter writer = new StreamWriter ("Masse-fois-Accelerations_FR_5Hz", true); 
						
												using (writer) {

														writer.WriteLine (Time.time + "\t" + 80 * acceleration + "\t" + forces_dup [indice_3].x);
												}
										}	
									}
								}

								frame++; 
				}
		}



I try somethings :
- to increment frame for the second part where I have to record datas, but it doesn't run
- to modify conditions "if (...)" in this part, but it doesn't right
Maybe I don't do good things.

Thank you in advance for your help :)
Posted
Updated 21-Apr-15 2:11am
v11
Comments
anup.bhunia 20-Apr-15 7:42am    
Hi, though you tried to provide relevant code, but still I fail to understand the basic question.

Could you please elaborate question?
Coralie B 20-Apr-15 7:50am    
I'm going to try.

Actually :
- For void start : I want to take all lines of datas from the text file (for my part names //Processing to calculate the acceleration with "Moindres carrés"
- For void FixedUpdate : I need 1 line of data / 20 to play the animation and to store the datas

Do you see what I mean ?

Thank you to want to help me.
Coralie B 20-Apr-15 7:52am    
And after the solution made by @Sinisa Hajnal I add to my code a boolean named "Skip_lines" and now my animation is played too slowly.

You can see my update question with this changing.
Coralie B 20-Apr-15 9:33am    
Finally it doesn't run as I did that... !
It's like Skip_lines = true every time as soon as this part of the code :
if (Skip_lines = true && (counter % 19) != 1)
continue;

I put "Debug.Log (Skip_lines);" when I am at the end of void start and the response is "True" however I never indicate "Skip_lines = true". I indicated the opposite at the beginning of void start.

I think it can't run because I need to save all lines of datas for the first part, but for the second I use only 1 line / 20. But all lines are saved : and in this second part I indicate that I read all datas which saved in my array.

So I think I have to indicate that I take only 1 line / 20 in the second part and not before.

Is it right ?
I tried to do that before, but I didn't arrive.

Can you help me ?
Coralie B 20-Apr-15 9:52am    
Could you see my update question ?

It's more elaborate I think :)

Add parameter to your method that will mark how many lines you want to skip (or if it is always 20 simply add boolean shouldSkipLines)

Then you change the line

C#
if ((counter % 19) != 1) // To indicate that we stored only 1 line / 20
                   continue;


to (N is the number you supply from outside - and you probably should use % N == 0)
C#
if ((counter % N) != 1) // To indicate that we stored only 1 line / N
                   continue;


OR (in the case of bool flag)
C#
if (shouldSkipLines && (counter % 19) != 1) // To indicate that we stored only 1 line / 20
                   continue;
 
Share this answer
 
Comments
Coralie B 20-Apr-15 7:31am    
Thank you .

I need so :
if (shouldSkipLines && (counter % 19) != 1)
continue;

But how I indicate in "traitement pour calculer l'accélération" that I want all lines and in "FixedUpdate" that I want only 1 line / 20 ?
Coralie B 20-Apr-15 7:45am    
I improved my question with my new code.

Do you see where is it my problem ?

Thank you again.
Coralie B 20-Apr-15 8:02am    
Thank you for your help ! :)
Sinisa Hajnal 20-Apr-15 9:26am    
No problem, I'm glad to be of help.
Coralie B 20-Apr-15 9:33am    
Finally it doesn't run as I did that... !
It's like Skip_lines = true every time as soon as this part of the code :
if (Skip_lines = true && (counter % 19) != 1)
continue;

I put "Debug.Log (Skip_lines);" when I am at the end of void start and the response is "True" however I never indicate "Skip_lines = true". I indicated the opposite at the beginning of void start.

I think it can't run because I need to save all lines of datas for the first part, but for the second I use only 1 line / 20. But all lines are saved : and in this second part I indicate that I read all datas which saved in my array.

So I think I have to indicate that I take only 1 line / 20 in the second part and not before.

Is it right ?
I tried to do that before, but I didn't arrive.

Can you help me ?
C#
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using System;
 

public class Recup_donnees_7 : MonoBehaviour {
	
	private class Data_struct {
		
		public String marker_name ; 
		public Vector3 [] positions ;
		
	}
	
	private Data_struct [] nv_data ;
	
	private bool Skip_lines;
	
	private Vector3[] forces;
	private Vector3[] forces_dup;
	private Vector3[] forces_virtual;
	private Vector3[] forces_real;
	
	private GameObject [] body  = new GameObject[55]; 
	
	string[][] datas;  
	string [][] datas_F;
	string [] forces_virtuelles;
	string [] forces_reelles;
	
	int cpt =0;
	int cpt_F = 0;
	int frame ; 
	
	float delta_time;
	float acceleration;
	float ancienne_vitesse;
	float vitesse;
	
	float ancienne_Energie_Cinetique;
	
	public Moindre_carre m1;
 
	Moindre_carre m2 = new Moindre_carre ();
	
	const int valurs_nb = 20;
	float coef_index_acc = 0.2f;
	
	double[] Y;
	double[] t;
	double[] sol;
 
	double [] acc;
	double [] vit;
	double [] pos;
	double pos_moindre = 0;
 
	int index_acc = 0;
	int indice_2 = -1;
	int indice_3 = -1;
	
 
	void Start() {
 
				body = new GameObject[55];
 
				body [0] = GameObject.Find ("Right_Hip");
				body [1] = GameObject.Find ("Left_Hip");
				body [4] = GameObject.Find ("Spine");
				body [6] = GameObject.Find ("Right_Shoulder");
				body [7] = GameObject.Find ("Left_Shoulder");
				body [8] = GameObject.Find ("Throat");
				body [12] = GameObject.Find ("Spine0");
				body [13] = GameObject.Find ("Head");
				body [17] = GameObject.Find ("Right_Elbow");
				body [21] = GameObject.Find ("Right_Hand");
				body [26] = GameObject.Find ("Left_Elbow");
				body [30] = GameObject.Find ("Left_Hand");
				body [35] = GameObject.Find ("Right_Knee");
				body [44] = GameObject.Find ("Right_Foot");
				body [45] = GameObject.Find ("Left_Knee");
				body [54] = GameObject.Find ("Left_Foot");
		
				delta_time = 0;
				acceleration = 0;
				ancienne_vitesse = 0;
	
				m2.degre = 3;
				m2.nombre_data = valurs_nb;
 
		
				Skip_lines = false;
				frame = 0;
		
		StreamReader reader = new StreamReader ("Suj01_PL_DP_C20_3.txt"); 
		
				using (reader) { 
			
						string line = " "; 
			
						
						int lineNumber = 10;
 
						for (int n = 0; n < lineNumber; n++) { 
								line = reader.ReadLine (); 
								if (line == null)
										return; 
						}
			
						string line_10;
						line_10 = line; 
						string[] names = line_10.Split (new String[] {",",",,,"}, StringSplitOptions.RemoveEmptyEntries); 
			
						nv_data = new Data_struct[names.Length];
			
						for (int x =0; x< names.Length; ++x) {
								nv_data [x] = new Data_struct (); 
								nv_data [x].marker_name = names [x]; 
						}
			
						line = reader.ReadLine (); 
			
						datas = new string[4000][]; 
			
						int counter = 0;
			
						while (line != null) { 
 
								counter++;
								line = reader.ReadLine (); 
				
								if (line == "ANALOG") 
										break;
 				
								if (Skip_lines = true && (counter % 19) != 1) 
										continue;
 
								string lines_datas; 
								lines_datas = line;
								datas [cpt] = lines_datas.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
 
				
								cpt ++;
						}
			
						for (int j = 1; j < names.Length+1; j++) {
				
								nv_data [j - 1].positions = new Vector3[cpt]; 
				
						}
 
			for (int i = 0; i < cpt-1; ++i) { 
				for (int j = 1; j < names.Length+1; j++) { 
 
					if (((float.Parse(datas [i] [j * 3 - 2]).CompareTo(float.NaN)) == 0) && ((float.Parse(datas [i] [j * 3 - 1]).CompareTo(float.NaN)) == 0) 
					    && ((float.Parse(datas [i] [j * 3]).CompareTo(float.NaN)) == 0)) 
						continue;
 
					 else 
 
					{
 
						nv_data [j - 1].positions [i].x = float.Parse (datas [i] [j * 3 - 2]); 
						nv_data [j - 1].positions [i].z = float.Parse (datas [i] [j * 3 - 1]);
						nv_data [j - 1].positions [i].y = float.Parse (datas [i] [j * 3]);
 
					}
				}
			}
 
						for (int m = 0; m < 3; m++) { 
								line = reader.ReadLine (); 
						}
			
						datas_F = new string[8000][]; 
			
						float Somme_x = 0;
						float Somme_y = 0;
						float Somme_z = 0;
			
						int counter_F2 = 0;
						while (line != null) { 
								counter_F2++;
								line = reader.ReadLine (); 
 
								if (line == null) 
										break;
				
								if (line != null && line.Length < 5)
										break;
				
								if ((counter_F2 % 200) != 0) { 
					
										string [] split_datas_F;
										split_datas_F = line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
					
										if (split_datas_F [3].Equals ("NaN"))
												continue;
					
										Somme_x += float.Parse (line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries) [3]);
										Somme_y += float.Parse (line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries) [4]);
										Somme_z += float.Parse (line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries) [5]);
 
								} else {
 
										datas_F [cpt_F] = line.Split (new string[] {","}, StringSplitOptions.RemoveEmptyEntries); 
					
										Somme_x /= 199;
										Somme_y /= 199;
										Somme_z /= 199;
					
										datas_F [cpt_F] [3] = Somme_x.ToString ();
										datas_F [cpt_F] [4] = Somme_y.ToString ();
										datas_F [cpt_F] [5] = Somme_z.ToString ();
					
										Somme_x = 0;
										Somme_y = 0;
										Somme_z = 0;
 
										cpt_F ++;
								}
						}
			
						Vector3[] forces = new Vector3[cpt_F];
			
						for (int o = 0; o < cpt_F-1; ++o) {
				
								forces [o].x = float.Parse (datas_F [o] [3]); 
								forces [o].y = float.Parse (datas_F [o] [4]); 
								forces [o].z = float.Parse (datas_F [o] [5]);
						}
			
						forces_dup = forces;
				}
 
				
		// Processing to calculate the acceleration with "Moindres carrés" 

		acc = new double[(nv_data [59].positions.Length / m2.nombre_data)];
		 vit = new double[(nv_data [59].positions.Length / m2.nombre_data)];
		 pos = new double[(nv_data [59].positions.Length / m2.nombre_data)];
 
		Y = new double[m2.nombre_data]; 
		t = new double[m2.nombre_data]; 
		sol = new double[m2.degre];
 
		while (index_acc + valurs_nb < nv_data[59].positions.Length) {
			
			Y [index_acc % valurs_nb] = (double)nv_data [59].positions [index_acc].z / 1000;
			t [index_acc % valurs_nb] = index_acc * 0.01;
			
			index_acc++;
			
			if (index_acc % valurs_nb == 0) {
				
				indice_2 ++;
				pos_moindre = ((acc [indice_2] / 2) * Math.Pow (index_acc * coef_index_acc, 2)) + vit [indice_2] * index_acc * coef_index_acc + pos [indice_2];
				
				if (Math.Abs (pos_moindre - nv_data [59].positions [index_acc].z / 1000) > 0.01) {
					
					m2.y = Y;
					m2.t = t;
					sol = m2.resolution ();
					acc [indice_2] = 2 * sol [0];
					vit [indice_2] = sol [1];
					pos [indice_2] = sol [2];
					
				} else {
					
					if (indice_2 > 1) {
						
						acc [indice_2] = acc [indice_2 - 1];
						vit [indice_2] = vit [indice_2 - 1];
						pos [indice_2] = pos [indice_2 - 1];
					}
					
				}
				
			}
		}
		
		StreamWriter writer_1 = new StreamWriter ("Masse-fois-Acceleration_prediction__Time", true); 
		
		using (writer_1) {
			for (int i = 0; i < indice_2; i++) 
				writer_1.WriteLine ((80*acc[i]) + "\t" + (i*coef_index_acc));
		}
 
	}
	
	void FixedUpdate() {
		
		delta_time += Time.deltaTime; 
		Skip_lines = true;
 
		if (frame < nv_data[0].positions.Length) { 
 
			for (int k = 0; k < body.Length; ++k) {
				
				if (body[k] != null) { 
 
					body[k].transform.localPosition = nv_data[k].positions[frame] / 1000;
 
				} 
 
				else
					continue;
			}
								
			if (frame < forces_dup.Length) { 
 
				if (frame >= 1) {
 
						float delta_x = (((nv_data[59].positions[frame] - nv_data[59].positions[frame-1]).z)*0.001f);
					vitesse = delta_x/Time.deltaTime;
					acceleration = (vitesse-ancienne_vitesse)/Time.deltaTime;
					ancienne_vitesse = vitesse;
 
						indice_3 ++;
 
						StreamWriter writer = new StreamWriter ("Masse-fois-Accelerations_FR_5Hz", true); 
						
						using (writer) {
 
							writer.WriteLine(Time.time + "\t" + 80*acceleration + "\t" + forces_dup[indice_3].x);
					}
				}	
 
			}
 
				frame++; 	
		}
 

	}
}
 
Share this answer
 
Comments
Sinisa Hajnal 21-Apr-15 2:32am    
if (Skip_lines = true && (counter % 19) != 1) continue;

You have to change this part to == true or it is an assignment and not a comparison
if (Skip_lines == true && (counter % 19) != 1)
continue;
Coralie B 21-Apr-15 2:43am    
I tried already that.

The void start is called only once time, when the code is launched (only at the beginning) so even though I indicate Skip_lines = true in void FixedUpdate, it will change nothing because we don't read again void start.

Do you see what I mean ?
Sinisa Hajnal 21-Apr-15 4:05am    
Yes, but you're not supposed to set skip_lines in Start...but in the method for line parsing...if that is start then you don't call it once, but twice (once for all frames and once of 1/20 frames).

Setting skip_lines to true in FixedUpdate means nothing if you don't call Start again (and even then, you're setting skip_lines to false within Start

You have to do this: void Start (bool skipLines) and call Start with a parameter. If Start cannot have parameters you have to separate the code for parsing into separate method (that is a good practice anyhow).
Coralie B 21-Apr-15 4:09am    
I don't understand what I have to do exactly...

Can you explain please ?
Sinisa Hajnal 21-Apr-15 4:29am    
The problem is, you asked initially to skip lines...but what you actually need is all lines from the file and then skipping part of them later in FixedUpdate, right? Since you don't want to parse the file twice then you have to use every 20th frame our of all those parsed.

What I initially thought is that you call Start from FixedUpdate. I've answered this in your other question - add parameter to FixedUpdate and call FixedUpdate(true) to skip lines or FixedUpdate(false) not to.

Good practice would be: separate data parsing into one method, processing into another and writing out the lines into third. Your start should look something like this:
Start() {
Init(); // set body
FileParse(); // parse the file
CalculateAcceleration(); // processing
WriteOutResult(); // write into the log
}

In all cases, add parameters as needed. Skip_lines shouldn't be member variable, but method parameter in fixed update.

I hope that helps. Sorry for misunderstanding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900