public static Vector3[] GetAs(MyPlanet p1, MyPlanet p2, float step_time, float G = 1f) { Vector3 a1 = p1.GetA(p2, step_time, G); Vector3 a2 = p2.GetA(p1, step_time, G); Vector3[] all = new Vector3[2] { a1, a2 }; return(all); }
public bool AddPlanet(MyPlanet p) { Vector3 position = p.GetPosition(); if (__NextID > 0) { foreach (MyPlanet old in Environment.Values) { if (Vector3.Equals(old.GetPosition(), position)) { return(false); } } } Environment.Add(__NextID, p); __NextID += 1; return(true); }
public Dictionary <int, Vector3>[] StepMove(float step_time = 0.1f, float G = 1f) { Dictionary <int, Vector3> id_to_accelerations = new Dictionary <int, Vector3>(); foreach (int akey in Environment.Keys) { MyPlanet aplanet = Environment[akey]; for (int bkey = akey + 1; bkey < __NextID; bkey++) { MyPlanet bplanet = Environment[bkey]; Vector3[] accelerations = MyPlanet.GetAs(aplanet, bplanet, step_time, G); if (id_to_accelerations.ContainsKey(akey)) { id_to_accelerations[akey] += accelerations[0]; } else { id_to_accelerations.Add(akey, accelerations[0]); } if (id_to_accelerations.ContainsKey(bkey)) { id_to_accelerations[akey] += accelerations[1]; } else { id_to_accelerations.Add(bkey, accelerations[1]); } } } Dictionary <int, Vector3> id_to_position = new Dictionary <int, Vector3>(); Dictionary <int, Vector3> id_to_speed = new Dictionary <int, Vector3>(); foreach (int akey in Environment.Keys) { MyPlanet aplanet = Environment[akey]; Vector3 p = aplanet.Step(id_to_accelerations[akey], step_time); id_to_position.Add(akey, p); id_to_speed.Add(akey, aplanet.GetSpeed()); } return(new Dictionary <int, Vector3>[] { id_to_position, id_to_speed, id_to_accelerations }); }
public bool AddPlanet(Vector3 x, Vector3 v, float mass = 0, float size = 0.5f) { var position = x; var speed = v; if (__NextID > 0) { foreach (MyPlanet old in Environment.Values) { if (Vector3.Equals(old.GetPosition(), position)) { return(false); } } } MyPlanet planet = new MyPlanet(position, speed, mass, size); Environment.Add(__NextID, planet); __NextID += 1; return(true); }
public bool AddPlanet(float x = 0, float y = 0, float z = 0, float vx = 0, float vy = 0, float vz = 0, float mass = 0, float size = 0.5f) { var position = new Vector3(x, y, z); var speed = new Vector3(vx, vy, vz); if (__NextID > 0) { foreach (MyPlanet old in Environment.Values) { if (Vector3.Equals(old.GetPosition(), position)) { return(false); } } } MyPlanet planet = new MyPlanet(position, speed, mass, size); Environment.Add(__NextID, planet); __NextID += 1; return(true); }
public Vector3 GetA(MyPlanet another, float step_time = 1f, float G = 1f) { Vector3 distance = Position - another.GetPosition(); var direction = -Vector3.Normalize(distance); float distanceLength = Vector3.DistanceSquared(Position, another.GetPosition()); Vector3 a; if (distanceLength > Size + another.Size) { a = (G * another.GetMass() * direction) / distanceLength; } else { //这是完全弹性碰撞公式 var m1 = Mass; var m2 = another.Mass; var v1 = Speed; var v2 = another.Speed; var new_v1 = ((m1 - m2) * v1 + 2 * m2 * v2) / ((m1 + m2)); a = (new_v1 - v1) / step_time; } return(a); }