public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force, decimal deltaTime = -1m) { if (deltaTime < 0.0m) { deltaTime = (decimal)Time.deltaTime; } //Debug.Log($"deltaTime: {deltaTime}, force: {force}"); switch (mode) { case ForceMode.Acceleration: vel += force * (decimal)deltaTime; return; case ForceMode.Force: vel += (force / (decimal)mass) * (decimal)deltaTime; return; case ForceMode.Impulse: vel += force / mass; return; case ForceMode.VelocityChange: vel += force; return; } }
void Update() { for (int i = 0; i < bodies.Count - 1; i++) { for (int j = i + 1; j < bodies.Count; j++) { VirtualPhysicsTransform bodyA = bodies[i].PhysicsTransform, bodyB = bodies[j].PhysicsTransform; decimal squardDist = Vector3.DistanceSquared(bodyA.Position, bodyB.Position); decimal forceMagnitude = 0m; if (squardDist > 0.001m) { forceMagnitude = (G * bodyA.Mass * bodyB.Mass) / squardDist; } Vector3 force = (bodyA.Position - bodyB.Position).Normalized * forceMagnitude; //Two birds with one stone bodyA.AddForce(-force); Debug.DrawRay(bodyA.Position, -force / bodyA.Mass, Color.green, Time.deltaTime, false); bodyB.AddForce(force); Debug.DrawRay(bodyB.Position, force / bodyB.Mass, Color.yellow, Time.deltaTime, false); } } }
public virtual void Tick(decimal deltaTime) { prevPos = pos; prevVel = vel; prevRot = rot; prevRotVel = rotVel; pos += vel * deltaTime; rot *= Quaternion.Lerp(Quaternion.Identity, RotationalVelocity, deltaTime); }
void Start() { GravitySim.Instance.Add(this); if (orbitTarget != null) { physicsTransform.Velocity = Vector3.Cross((physicsTransform.Position - orbitTarget.PhysicsTransform.Position), Vector3.Up).Normalized * MathHelper.DSqrt((GravitySim.G * orbitTarget.PhysicsTransform.Mass) / Vector3.Distance(physicsTransform.Position, orbitTarget.PhysicsTransform.Position)); } Debug.Log(physicsTransform.Velocity); }
public void Initialise(Vector3 initPos, Vector3 initVel, Quaternion initRot, Quaternion initRotVel) { pos = initPos; prevPos = initPos; vel = initVel; prevVel = initVel; rot = initRot; prevRot = initRot; rotVel = initRotVel; prevRotVel = initRotVel; }
public void Translate(Vector3 dist) => Position += dist;
//public void LookAt(Vector3 pos) => LookAt(pos, Vector3.Up); //public void LookAt(Vector3 pos, Vector3 up) => Rotation = Quaternion.LookRotation(pos - Position, up); public void RotateAround(Vector3 axis, decimal degrees) => rot *= Quaternion.CreateFromAxisAngle(axis, (decimal)degrees);
public VirtualPhysicsTransform(Vector3 initPos, Vector3 initVel, Quaternion initRot, Quaternion initRotVel) { Initialise(initPos, initVel, initRot, initRotVel); }