如何跳过void FixedUpdate中的某些数据? [英] How can I skip some datas in void FixedUpdate ?

查看:76
本文介绍了如何跳过void FixedUpdate中的某些数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我需要在 void FixedUpdate 中指明我只想使用1帧/ 20(用于动画和录音作家)。



这里我尝试做的事:



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 :

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++; 
				}
		}







我尝试一下:

- 为我必须记录数据的第二部分增加帧,但它不运行

- 修改条件if(...)在这部分,但它不对。

也许我不做好事。



提前感谢你的帮助:)




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 :)

推荐答案

在您的方法中添加参数,该参数将标记您要跳过多少行(或者如果它总是20,则只需添加boolean shouldSkipLines)



然后你换行



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

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





to(N是数字y你从外面供应 - 你可能应该使用%N == 0)



to (N is the number you supply from outside - and you probably should use % N == 0)

if ((counter % N) != 1) // To indicate that we stored only 1 line / N
                   continue;





OR(在bool标志的情况下)



OR (in the case of bool flag)

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


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++; 	
		}
 

	}
}


这篇关于如何跳过void FixedUpdate中的某些数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆