2014-09-29 47 views
0

繼承人從我試圖做一個2D粒子SIMC#時差實施

static long lastTime = 0; 
static double GetDeltaTime() 
{ 
    long now = DateTime.Now.Millisecond; 
    double dT = (now - lastTime); ///1000 
    lastTime = now; 
    Console.WriteLine(dT); 
    return dT; 
} 

這應該是很明顯的,它會返回時間(毫秒),因爲該方法被稱爲最後一次的代碼片段。唯一的問題,這就是它打印的內容

393 
1 
0 
0 
0 
0 
0 
0 
0 
0 
0 
... 

好吧,也許這就是因爲每次通過都少於一毫秒。所以我改成了

static long lastTime = 0; 
    static double GetDeltaTime() 
    { 
     long now = DateTime.Now.Ticks; // Changed this to ticks 
     double dT = (now - lastTime); ///1000 
     lastTime = now; 
     Console.WriteLine(dT); 
     return dT; 
    } 

但仍然打印

6.35476136625848E+17 
20023 
0 
0 
0 
0 
0 
0 
... 

,如果「顆粒模擬器」心不是我的計劃是多麼複雜的一個足夠好的指標,我只想說,它需要大量的長於0滴答完成通過!

那麼這裏發生了什麼?

-------代碼參考------ 繼承人的全班僅供參考

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Threading; 

namespace _2D_Particle_Sim 
{ 
    static class Program 
    { 
     public static Particle2DSim pSim; 
     static Form1 form; 
     public static Thread update = new Thread(new ThreadStart(Update)); 

     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 

     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 

      form = new Form1(); 

      pSim = new Particle2DSim(form); 
      pSim.AddParticle(new Vector2(-80, -7), 5); 
      pSim.AddParticle(new Vector2(8, 7), 3); 

      Console.WriteLine("Opening Thread"); 

      Program.update.Start(); 

      Application.Run(form); 

      // System.Threading.Timer timer; 
      // timer = new System.Threading.Timer(new TimerCallback(Update), null, 0, 30); 
     } 

     static void Update() 
     { 
      GetDeltaTime(); 
      while (true) 
      { 
       pSim.Update(GetDeltaTime()); 
      } 
     } 

     static long lastTime = 0; 
     static double GetDeltaTime() 
     { 
      long now = DateTime.Now.Ticks; 
      double dT = (now - lastTime); ///1000 
      lastTime = now; 
      Console.WriteLine(dT); 
      return dT; 
     } 
    } 
} 

而且,如果我的我的代碼的複雜程度仍然不夠wasnt,類比繼承人從Particle2DSim類

public void Update(double deltaTime) 
     { 
       foreach (Particle2D particle in particles) 
       { 
        List<Particle2D> collidedWith = new List<Particle2D>(); 

        Vector2 acceleration = new Vector2(); 
        double influenceSum = 0; 

        // Calculate acceleration due to Gravity 
        #region Gravity 
        foreach (Particle2D particle2 in particles) 
        { 
         double dist2 = particle.position.Distance2(particle.position); 
         double influence = dist2 != 0 ? particle2.mass/dist2 : 0; 
         acceleration.Add(particle.position.LookAt(particle2.position) * influence); 
         influenceSum += influence; 

         if (dist2 < ((particle.radius + particle2.radius) * (particle.radius + particle2.radius)) && dist2 != 0) 
         { 
          collidedWith.Add(particle2); 
         } 
        } 
        acceleration.Divide(influenceSum); 
        #endregion 

        particle.Update(deltaTime); 

        // Handle Collisions 
        #region Collisions 
        if (collidedWith.Count > 0) 
        { 
         Console.WriteLine("Crash!"); 

         double newMass = 0; 
         double newRadius = 0; 

         Vector2 newPosition = new Vector2(); 
         Vector2 newVelocity = new Vector2(); 

         newMass += particle.mass; 
         newRadius += Math.Sqrt(particle.radius); 

         newPosition += particle.position; 

         newVelocity += particle.velocity * particle.mass; 

         particles.Remove(particle); 

         foreach (Particle2D particle2 in collidedWith) 
         { 
          newMass += particle2.mass; 
          newRadius += Math.Sqrt(particle2.radius); 

          newPosition += particle2.position; 

          newVelocity += particle2.velocity * particle2.mass; 

          particles.Remove(particle2); 
         } 

         newPosition.Divide(collidedWith.Count + 1); 
         newVelocity.Divide(newMass); 

         AddParticle(newPosition, newVelocity, newMass, newRadius); 
        } 
        #endregion 
       } 
     } 
+0

你試過沒有使用'而(真)'和睡眠有點埃裏克利珀的博客?無論如何,你最終都會想要這樣做。 – BradleyDotNET 2014-09-29 23:00:34

+1

請注意,「毫秒」是從「0」到「999」。 (因爲你使用'long',所以你可以預計它總的毫秒數。) – AlexD 2014-09-29 23:08:40

回答

4

問題methord的更新是你使用DateTime嘗試測量時間的流逝。 DateTime用於表示日期和時間,但不用於測量已用時間。

使用stopwatch類測量時間:

Stopwatch sw = new Stopwatch(); 

sw.Start(); 

// Do something here 

sw.Stop(); 

Console.WriteLine(sw.ElapsedMilliseconds); 
// or sw.ElapsedTicks 

更多細節上的差異,看看HERE

+0

秒錶可能很難在三角洲的情況下使用。否則不是一個壞建議,它可以工作。 – BradleyDotNET 2014-09-29 23:02:35