继承人从我试图做一个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
}
}
你试过没有使用'而(真)'和睡眠有点埃里克利珀的博客?无论如何,你最终都会想要这样做。 – BradleyDotNET 2014-09-29 23:00:34
请注意,“毫秒”是从“0”到“999”。 (因为你使用'long',所以你可以预计它总的毫秒数。) – AlexD 2014-09-29 23:08:40